如何在不评估命令的情况下从 shell 脚本向 shell 脚本写入命令
How can I write a command to shell script from a shell script without evaluating the command
我正在努力传递 shell 命令。具体来说,我写了一个 shell 用户会 运行 的文件。其中,另一个 shell 文件是根据用户输入编写的。然后,此文件将传递给提交作业的命令。
现在,在这个内部 shell 文件中,我有一个包含函数的变量。但是,当我 运行 用户 shell 脚本时,我无法让此函数以内部 shell 文件可以使用它的方式传递。
我不能分享我的工作,但我会尝试做一个例子
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var=`grep examples input.txt`
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
当我 运行 和 'User Shell Script' 时,它打印出 grep 找不到文件,但我不想评估 grep。我需要按原样编写它,以便在读取行 '/bin/localfoo2 /test.txt' 时,评估 grep。
我已经尝试了很多东西。我试过双回勾,我试过使用 'echo $(eval $var)'。但是 none 我通过谷歌搜索找到的方法已经成功地以一种能够完成我想要的方式传递了这个 var。
非常感谢您的帮助。
您可以尝试使用单引号 (')。
您必须在 grep 命令之前和 grep 命令的结尾加上单引号,如下所示。
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var='`grep examples input.txt`'
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
我不明白你必须在哪里执行那个 grep 命令。
如果你想在localfoo脚本中执行grep命令,希望这个方法对你有所帮助。
我正在努力传递 shell 命令。具体来说,我写了一个 shell 用户会 运行 的文件。其中,另一个 shell 文件是根据用户输入编写的。然后,此文件将传递给提交作业的命令。
现在,在这个内部 shell 文件中,我有一个包含函数的变量。但是,当我 运行 用户 shell 脚本时,我无法让此函数以内部 shell 文件可以使用它的方式传递。
我不能分享我的工作,但我会尝试做一个例子
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var=`grep examples input.txt`
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
当我 运行 和 'User Shell Script' 时,它打印出 grep 找不到文件,但我不想评估 grep。我需要按原样编写它,以便在读取行 '/bin/localfoo2 /test.txt' 时,评估 grep。
我已经尝试了很多东西。我试过双回勾,我试过使用 'echo $(eval $var)'。但是 none 我通过谷歌搜索找到的方法已经成功地以一种能够完成我想要的方式传递了这个 var。
非常感谢您的帮助。
您可以尝试使用单引号 (')。 您必须在 grep 命令之前和 grep 命令的结尾加上单引号,如下所示。
#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var='`grep examples input.txt`'
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt
我不明白你必须在哪里执行那个 grep 命令。 如果你想在localfoo脚本中执行grep命令,希望这个方法对你有所帮助。