简体   繁体   中英

How can I call programs that are already running to open new files from Perl or a bash script?

I am using a Perl script to find data needed to deal with errors from another process. In OSX, these lines in the Perl script open the needed sites and files in my browser and text editor. Here's the code that works in OSX:

 if ($^O eq "darwin") {
      system `open -a /Applications/Firefox.app https://site`;
      system `open -a /Apopen -a /Applications/Komodo.appplications/Komodo.app file1.md file2.md`;
      system `open -a /Applications/Komodo.app file3.md`;}

I can run this script repeatedly and the apps will add tabs with the new open files.

However, I will need to run the same tool on Linux, and I haven't found any way to do it. Here's how I've tried to adapt it to Linux.

 if ($^O eq "linux") {
      system `firefox local_file.html & disown`;
      system `firefox https://site1 https://sitex & disown`;
      system `komodo  file1 file2 filex & disown`;
      }

I have since adjusted the Perl script so that it outputs a shell that I can run from the command line (latest version as per suggestion by @Grant McLean):

xdg-open https://www.site1 & disown
xdg-open https://www.site2 & disown
xdg-open /home/.../file1.html & disown
xdg-open /home/.../file2.md & disown
xdg-open /home/.../file3.md & disown

I gather from @Grant McLean's suggestion that if I were to integrate it into the Perl script the lines would be, eg,

system "xdg-open { file | site } & disown";

But since the shell hangs the system, I'm assuming that passing the commands to the system from the Perl script would also hang the system.

I want to be able to have the browser and the text editor open files from a shell and not have to open files individually in each app. Any ideas?

The solution was a variation of what @grantmclean suggested. His solution didn't work when I tried it because I was trying to xdg-open more than one file or site at a time. Opening one at a time works:

if ($^O eq "linux") {
    system "xdg-open https://site1 &";
    system "xdg-open https://site2 &";
    my @tWs = split / /,$tW_files;
    foreach (@tWs) {
        system "xdg-open $_ &" # opens a series of local files
    }
    system "xdg-open file_y &";
    system "xdg-open file_z &";
}

My thanks to you all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM