无法打印 $1 的相同参数
#!/bin/bash
if [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
display_help; exit 1
else
while [[ $# -gt 0 ]]; do
opt="$1"
shift;
current_arg="$1"
case "$opt" in
"run_command" ) RUN_COMMAND="$1"; shift;;
"cmd1" ) CMD1="$1"; shift;;
* ) echo "ERROR: Invalid option: \""$opt"\"" >&2
exit 1;;
esac
done
fi
if [[ "$RUN_COMMAND" == "Custom" ]];then
export command1=`echo $CMD1`
echo $command1
else
echo "Invalid arguments"
fi
结果输出
./test_non.sh run_command Custom cmd1 "date|awk '{print \\$1}'"
date|awk '{print \}'
你应该使用
./script run_command Custom cmd1 "date|awk '{print \\$1}'"
为什么?我真的不知道那个 double 的原因\
,但可能它也需要转义特殊字符$
和参数号(一个\
用于$
和1
)
以下是一些结果:
好的一个:
damadam@Pc:~$ export command1=`echo "date|awk '{print \\$1}'"`
damadam@Pc:~$ echo $command1
date|awk '{print $1}'
测试:
damadam@Pc:~$ export command1=`echo "date|awk '{print \$\1}'"`
damadam@Pc:~$ echo $command1
date|awk '{print $\1}'
damadam@Pc:~$ export command1=`echo "date|awk '{print $\1}'"`
damadam@Pc:~$ echo $command1
date|awk '{print $\1}'
OP测试:
damadam@Pc:~$ export command1=`echo "date|awk '{print \$1}'"`
damadam@Pc:~$ echo $command1
date|awk '{print }'
编辑 1:使用 OP 给出的示例脚本代码(我将它命名test.sh
为亲自测试),只需要一个\
来转义$1
参数
damadam@Pc:~$ ./test.sh run_command Custom cmd1 "date|awk '{print \$1}'"
date|awk '{print $1}'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句