以下 Bash 脚本在第一read -p
行后退出。
#!/bin/bash
clear
echo Put me next to your targeted .sh file!
read -p "Press [Enter] key when placed next to target..."
echo Type your .sh filename (target.sh)...
read target
chmod +x $target
echo Target Converted!
read -p "Press [Enter] key to Exit..."
有人可以解释一下脚本退出的原因以及我该如何阻止它吗?
答案1
脚本由于以下错误而退出:
a.sh: line 7: syntax error near unexpected token `('
a.sh: line 7: `echo Type your .sh filename (target.sh)...'
在 Bash 中,以及更普遍的在命令行解释器中,有些字符是特殊的并且具有含义。(
和)
就是其中两个字符。
当使用echo
,read -p
或其他类似命令时,请始终将字符串括在引号中,如下所示:
echo "Put me next to your targeted .sh file!"
[...]
echo "Type your .sh filename (target.sh)..."
[...]
echo "Target Converted!"