简体   繁体   English

Linux如何从父目录执行bash脚本?

[英]Linux how do I execute a bash script from the parent directory?

I have a script called GenerateDTIPubSub and if I call it from its file location using the terminal (which I opened in the same file location): 我有一个名为GenerateDTIPubSub的脚本,如果我使用终端(在同一文件位置中打开)从其文件位置调用该脚本,请执行以下操作:

./GenerateDTIPubSub --DTIFramePubSub ./GenerateDTIPubSub --DTIFramePubSub

works fine. 工作正常。

How do I now execute the script while in the folder above? 现在在上面的文件夹中时如何执行脚本? Example: ./cfg/GenerateDTIPubSub --DTIFramePubSub 示例: ./cfg/GenerateDTIPubSub --DTIFramePubSub / ./cfg/GenerateDTIPubSub --DTIFramePubSub

Which does not work(reminding you that I've also opened the terminal in the folder above) 哪个不起作用(提醒您我也在上面的文件夹中打开了终端)

edit: It executes but the bash script but can not find the perl script that it executes unless the bash script is executed from its directory (if that makes sense) 编辑:它执行但bash脚本,但无法找到它执行的perl脚本,除非从其目录执行bash脚本(如果这样)

./GenerateDTIPubSub --DTIFramePubSub GenerateDTIPubSub then calls the DTIFramePubSub function that calls a perl script, this path works fine ./GenerateDTIPubSub --DTIFramePubSub然后,GenerateDTIPubSub调用DTIFramePubSub函数,该函数调用perl脚本,此路径可以正常工作

./cfg/GenerateDTIPubSub --DTIFramePubSub this does not work, it can't find the perl script ./cfg/GenerateDTIPubSub --DTIFramePubSub这不起作用,找不到perl脚本

Can't open perl script "Scripts/DTIPubSub.pl": No such file or directory 无法打开perl脚本“ Scripts / DTIPubSub.pl”:没有这样的文件或目录

but the path for the perl script does not change only the bash file path 但是perl脚本的路径不会仅更改bash文件路径

To work around the problem, you can use the following: 要变通解决此问题,您可以使用以下方法:

( cd cfg; ./GenerateDTIPubSub --DTIFramePubSub )

The parens create a subshell to avoid changing the current work directory for anything but the perl process. parens创建一个子shell,以避免为perl进程以外的任何内容更改当前工作目录。


The underlying problem is that GenerateDTIPubSub assumes the current work directory is the script's directory. 潜在的问题是GenerateDTIPubSub假定当前工作目录是脚本的目录。 What follows are instructions on how to fix that. 以下是有关解决方法的说明。

  1. The quick and dirty approach: 快速而肮脏的方法:

    Add the following: 添加以下内容:

     use FindBin qw( $RealBin ); BEGIN { chdir($RealBin); } 
  2. The more sensible approach: 更明智的方法是:

    First, add the following: 首先,添加以下内容:

     use FindBin qw( $RealBin ); 

    Then replace 然后更换

     "Scripts/DTIPubSub.pl" 

    with

     "$RealBin/Scripts/DTIPubSub.pl" 

    You may need to do multiple similar fixes in the code. 您可能需要在代码中进行多个类似的修复。

It seems you must (temporarily) change the current working directory before calling your script. 看来您必须(临时)更改当前工作目录,然后再调用脚本。 You can do that with the following technique 您可以使用以下技术来做到这一点

cd cfg && ./GenerateDTIPubSub --DTIFramePubSub

问题是基本脚本中的Perl脚本的路径需要更改为包含/cgf

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

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