为什么不退回Shell脚本中的引号会帮助我将cd转到目录 [英] Why didn't back quotes in a shell script help me cd to a directory

查看:83
本文介绍了为什么不退回Shell脚本中的引号会帮助我将cd转到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下代码的shell脚本:

I have a shell script with the following code:

dir=sample
`mkdir $dir`
`cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`
`cd $dir`

在反引号中使用cd命令的最后一行中,我无法cd进入相应的目录. 但是,一旦我删除了反引号,我就可以cd. 我想知道的是为什么cd不能与反引号一起使用?

In the last line with the cd command in the back quotes, I was not able to cd into the corresponding directory. But once I removed the back quotes I was able to cd. What I was wondering is why didn't the cd work with the back quote?

推荐答案

运行时:

`mkdir $dir`

shell首先在子shell中运行命令mkdir $dir,捕获其(标准)输出,然后将捕获的字符串作为命令运行.幸运的是,输出为空,因此第二步不执行任何操作.

the shell first ran the command mkdir $dir in a subshell, capturing its (standard) output, and then ran the captured string as a command. Fortunately, the output was empty, so the second step executed nothing.

随后运行时:

`cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`

该副本在子shell中执行,并且捕获并执行了输出.同样,输出为空,因此执行的第二阶段什么也没做.

the copy was executed in a subshell, and the output was captured and executed. Again, the output was empty, so the second phase of execution did nothing.

然后您运行:

`cd $dir`

再一次,cd操作在子shell中运行,该子shell在更改其自己的当前工作目录后退出,但不影响父shell(这是Unix,而不是DOS .bat命令文件).和以前一样,cd命令的输出被捕获并执行,但是输出为空,因此没有什么要执行的.

Once more, the cd operation was run in a subshell, which exited after changing its own current working directory, but without affecting the parent shell (this is Unix, not a DOS .bat command file). As before, the output of the cd command was captured, and executed, but the output was empty so there was nothing to execute.

本质上,您不会像现在那样广泛使用反引号.

Essentially, you don't use back-quotes as extensively as you are doing.

这样写就足够了:

dir=sample
mkdir $dir
cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt
cd $dir
...other activity in the new directory...

请注意,如果这在脚本中,则执行脚本的常规方法仍会将父外壳保留在原始目录中.有多种方法可以使它影响原始外壳程序—查找.命令(或者在bash中是source命令;这更容易搜索).

Note that if this is in a script, then the normal ways of executing a script would still leave the parent shell in the original directory. There are ways to make it affect the original shell — find out about the . command (or, in bash, the source command; that's easier to search for).

通常使用反引号(或更佳的是$(...)表示法)来捕获数据.例如:

You normally use back quotes (or, better, the $(...) notation) to capture data. For example:

gcc_lib_dir=$(dirname $(dirname $(which gcc)))/lib

最里面的命令是which gcc;它可能会产生/usr/gcc/v4.7.1/bin/gcc;内部dirname然后产生/usr/gcc/v4.7.1/bin;外部目录名产生/usr/gcc/v4.7.1;随附的/lib给出

The innermost command is which gcc; it might yield /usr/gcc/v4.7.1/bin/gcc; the inner dirname then yields /usr/gcc/v4.7.1/bin; the outer dirname yields /usr/gcc/v4.7.1; the appended /lib gives

gcc_lib_dir=/usr/gcc/v4.7.1/lib

这也说明了为什么$(...)优于反引号表示法:

That also shows why $(...) is superior to the back-quote notation:

gcc_lib_dir=`dirname \`dirname \\\`which gcc\\\`\``/lib

这很难弄对,而且很难打字!

That's harder to get right, and harder to type!

这篇关于为什么不退回Shell脚本中的引号会帮助我将cd转到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆