执行错误的 shell 命令卡住如何恢复?

How to recover when stuck by executing wrong shell commands?

我在 win7 64 位中使用 Git Bash 2.9.0-64 位。 这是我的 shell 代码

function sum1_x(){
a=
while [ $a -ge 1 ]; do
    sum=$[$sum + $a]
    a=$[$a - 1]
done

echo $sum
}

sum 

在GitBash中,我的打字记录如下:

wen@PC-WEN MINGW64 /d/git/ (dev)
$sh sum1_x.sh
exit
:q

quit
quit()
exit()

在我输入 sh sum1_x.sh 后,我无法再执行任何 shell command.I 尝试了很多命令,因为 shows.I 知道我的脚本中有错误,但是怎么能我回去执行 shell 脚本遇到这种问题?我现在无能为力了。

您的脚本有几个问题:

  1. 你有功能 sum1_x 但你正在调用 sum
  2. 您没有检查输入值是否存在,因此您陷入了无限循环

这是检查输入参数是否存在的更正脚本:

function sum1_x(){
a=
while [ $a -ge 1 ]; do
  sum=$[$sum + $a]
  a=$[$a - 1]
done

echo $sum
}

if [ -z "" ]; then  # check that input parameter is exists
echo "No input"
else
sum1_x 
fi

如何停止程序:

  1. Ctrl + 'c'
  2. 打开新的mingw,通过ps -aux | grep "sum1_x.sh"
    找到progrman的pid 然后使用 kill pid 杀死程序

如问题所示,shell 脚本包含许多 bugs.The 命令 sh sum1_x.sh 是错误的 too.It 应该替换为命令 sh sum1_x.sh 9 或东西就像 it.After 我修复了这些错误,它运行没有问题并打印出正确的总和。

但是我还是不知道怎么购买shellscript.Fortunately,一不小心按了Ctrl&c,又可以输入shell命令了