简体   繁体   English

如何调用已运行的程序从Perl或bash脚本中打开新文件?

[英]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. 我正在使用Perl脚本来查找处理来自另一个进程的错误所需的数据。 In OSX, these lines in the Perl script open the needed sites and files in my browser and text editor. 在OSX中,Perl脚本中的这些行在浏览器和文本编辑器中打开所需的站点和文件。 Here's the code that works in OSX: 这是在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. 但是,我需要在Linux上运行相同的工具,但我还没有找到任何方法。 Here's how I've tried to adapt it to Linux. 以下是我尝试将其改编为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): 我已经调整了Perl脚本,以便它输出一个shell,我可以从命令行运行(根据@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, 我收集了@Grant McLean的建议,如果我将它集成到Perl脚本中,那么行就是,例如,

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. 但是由于shell挂起了系统,我假设从Perl脚本向系统传递命令也会挂起系统。

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. 我希望能够让浏览器和文本编辑器从shell打开文件,而不必在每个应用程序中单独打开文件。 Any ideas? 有任何想法吗?

The solution was a variation of what @grantmclean suggested. 解决方案是@grantmclean建议的变体。 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. 当我尝试它时,他的解决方案无法正常工作,因为我试图xdg-open多个文件或站点。 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! 谢谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在执行bash脚本期间如何更改用户身份,并继续以新用户身份运行命令? - How can I change users in during my bash script excecution and continue running commands with the new user? 如何阻止服务器执行的程序删除文件? - How can I stop programs executed by a server from deleting files? 如何启动2个需要从bash脚本分离终端的程序? - How to start 2 programs that need separate terminals from a bash script? 我怎样才能知道Linux脚本(ash(不是bash))正在“来源”运行? - How can I tell a Linux Script (ash, not bash) is running “sourced”? 如何从C程序调用bash shell脚本中定义的函数 - How can I call a function defined in a bash shell script from a C program 使用 subprocess.call 运行 bash 脚本时,如何访问 anaconda3/bin 目录中的函数? - How can I access functions inside the anaconda3/bin directory when running a bash script with subprocess.call? 如果它已经在运行,如何在 bash 中杀死另一个脚本 - How to kill another script in bash if it's already running 我怎么知道我在Perl脚本中是在64位还是32位Linux上运行? - How can I know if I am running in a 64bits or a 32bits linux from within a perl script? 如何使用Bash脚本仅在perl脚本中插入一行新代码 - How to insert a new line of code in only perl scripts with Bash script 我可以在 bash 中调用交互式脚本吗? - Can I call an interactive script in orther bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM