将bash脚本从一个执行到另一个目录?

时间:2022-04-20 01:46:48

I have 3 directories: /A/B/C and 1 bash script in C directory. How can I execute this bash script from A into in C directory.

我在C目录中有3个目录:/A/B/C和1个bash脚本。如何在C目录中执行这个bash脚本。

4 个解决方案

#1


3  

I understand from your comments that you want your script to have its current working directory to be in A/B/C when it executes. Ok, so you go into directory A:

我从您的评论中了解到,您希望您的脚本在执行时将其当前工作目录放在A/B/C中。进入目录A

cd A

and then execute script.sh from there:

然后执行脚本。上海从那里:

(cd B/C; ./script.sh)

What this does is start a subshell in which you first change to the directory you want your script to execute in and then executes the script. Putting it as a subshell prevents it from messing up the current directory of your interactive session.

它所做的是启动一个子shell,首先在其中更改要执行脚本的目录,然后执行脚本。将它作为子shell,可以防止它破坏交互式会话的当前目录。

If it is a long running script that you want to keep in the background you can also just add & at the end like any other command.

如果你想在后台保存一个长时间运行的脚本,你也可以像其他命令一样在末尾添加&。

#2


0  

Assuming /A/B/C/script.sh is the script, if you're in /A, then you'd type ./B/C/script.sh or bash B/C/script.sh or a full path, /A/B/C/script.sh. If what you mean is that you want that script always to work, then you'll want to add it to your PATH variable.

假设/ A / B / C /脚本。sh是脚本,如果你在/A,那么你就输入。/B/C/脚本。sh或bash B / C /脚本。sh或a full path, / a /B/C/ scriptp .sh。如果您的意思是希望该脚本始终工作,那么您将希望将其添加到路径变量中。

#3


0  

Go to A, then run your script by providing the path to it:

转到A,然后通过提供路径来运行脚本:

cd /A
bash B/C/somescript.sh

You could also add C to your PATH variable, making it runnable from anywhere

您还可以将C添加到路径变量中,使它可以从任何地方运行

(i.e. somescript.sh, without the path)

(即somescript。sh,没有路径)

If you want to access the directory the script is stored in, within the script, you can use this one-liner:

如果您想要访问脚本存储在脚本中的目录,您可以使用这一行:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

or simply dirname. Taken from this thread. There are many more suggestions there.

或者简单的目录名。从这个线程。还有更多的建议。

The easy way to make your script execute within C is to simply be in that folder.

让脚本在C中执行的简单方法是在那个文件夹中。

#4


0  

Whenever I want to make sure that a script can access files in its local folder, I throw this in near the top of the script:

每当我想要确保脚本可以访问其本地文件夹中的文件时,我就把这个放在脚本的顶部:

# Ensure working directory is local to this script
cd "$(dirname "$0")"

It sounds like this is exactly what you're looking for!

听起来这正是你要找的!

#1


3  

I understand from your comments that you want your script to have its current working directory to be in A/B/C when it executes. Ok, so you go into directory A:

我从您的评论中了解到,您希望您的脚本在执行时将其当前工作目录放在A/B/C中。进入目录A

cd A

and then execute script.sh from there:

然后执行脚本。上海从那里:

(cd B/C; ./script.sh)

What this does is start a subshell in which you first change to the directory you want your script to execute in and then executes the script. Putting it as a subshell prevents it from messing up the current directory of your interactive session.

它所做的是启动一个子shell,首先在其中更改要执行脚本的目录,然后执行脚本。将它作为子shell,可以防止它破坏交互式会话的当前目录。

If it is a long running script that you want to keep in the background you can also just add & at the end like any other command.

如果你想在后台保存一个长时间运行的脚本,你也可以像其他命令一样在末尾添加&。

#2


0  

Assuming /A/B/C/script.sh is the script, if you're in /A, then you'd type ./B/C/script.sh or bash B/C/script.sh or a full path, /A/B/C/script.sh. If what you mean is that you want that script always to work, then you'll want to add it to your PATH variable.

假设/ A / B / C /脚本。sh是脚本,如果你在/A,那么你就输入。/B/C/脚本。sh或bash B / C /脚本。sh或a full path, / a /B/C/ scriptp .sh。如果您的意思是希望该脚本始终工作,那么您将希望将其添加到路径变量中。

#3


0  

Go to A, then run your script by providing the path to it:

转到A,然后通过提供路径来运行脚本:

cd /A
bash B/C/somescript.sh

You could also add C to your PATH variable, making it runnable from anywhere

您还可以将C添加到路径变量中,使它可以从任何地方运行

(i.e. somescript.sh, without the path)

(即somescript。sh,没有路径)

If you want to access the directory the script is stored in, within the script, you can use this one-liner:

如果您想要访问脚本存储在脚本中的目录,您可以使用这一行:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

or simply dirname. Taken from this thread. There are many more suggestions there.

或者简单的目录名。从这个线程。还有更多的建议。

The easy way to make your script execute within C is to simply be in that folder.

让脚本在C中执行的简单方法是在那个文件夹中。

#4


0  

Whenever I want to make sure that a script can access files in its local folder, I throw this in near the top of the script:

每当我想要确保脚本可以访问其本地文件夹中的文件时,我就把这个放在脚本的顶部:

# Ensure working directory is local to this script
cd "$(dirname "$0")"

It sounds like this is exactly what you're looking for!

听起来这正是你要找的!