如何将剪切命令的输出带入变量?
How to bring the output of a cut-command into a variable?
这是 Linux bash-shell-脚本的问题。
我想将“剪切命令”的输出放入一个变量中。但它不起作用。变量保持为空。
这是我所做的:
MyName@MyName:~
$ fixedFilePath=aa.zz
MyName@MyName:~
$ echo $fixedFilePath
aa.zz
MyName@MyName:~
$ EP=$fixedFilePath | rev | cut -d '.' -f 1 | rev
MyName@MyName:~
$ echo $EP
MyName@MyName:~
$
如您所见:现在变量 $EP 中没有任何内容。我的期望是,$EP 现在是“zz”。
当您分配给 EP
时,第一部分 $fixedFilePath
不会在 stdout 上留下任何内容供管道使用。它所做的是执行该变量的内容。你需要 echo
.
echo $fixedFilePath | rev | cut -d '.' -f 1 | rev
现在要捕获该输出,您需要将其作为作业的一部分执行。有多种方法可以解决这个问题,但我发现在你的案例中有效的是 backticks:
EP=`echo $fixedFilePath | rev | cut -d '.' -f 1 | rev`
这是 Linux bash-shell-脚本的问题。
我想将“剪切命令”的输出放入一个变量中。但它不起作用。变量保持为空。
这是我所做的:
MyName@MyName:~
$ fixedFilePath=aa.zz
MyName@MyName:~
$ echo $fixedFilePath
aa.zz
MyName@MyName:~
$ EP=$fixedFilePath | rev | cut -d '.' -f 1 | rev
MyName@MyName:~
$ echo $EP
MyName@MyName:~
$
如您所见:现在变量 $EP 中没有任何内容。我的期望是,$EP 现在是“zz”。
当您分配给 EP
时,第一部分 $fixedFilePath
不会在 stdout 上留下任何内容供管道使用。它所做的是执行该变量的内容。你需要 echo
.
echo $fixedFilePath | rev | cut -d '.' -f 1 | rev
现在要捕获该输出,您需要将其作为作业的一部分执行。有多种方法可以解决这个问题,但我发现在你的案例中有效的是 backticks:
EP=`echo $fixedFilePath | rev | cut -d '.' -f 1 | rev`