Shell脚本编写意外运算符
我已经为这个问题挣扎了几个小时,并尝试了在讨论板上能找到的一切。我无法切换到Shell脚本编写意外运算符,shell,unix,sh,Shell,Unix,Sh,我已经为这个问题挣扎了几个小时,并尝试了在讨论板上能找到的一切。我无法切换到bash,这是在bin/sh中 问题在于while循环,while[$currentID-gt 0]。错误是“21:[:2893:意外操作员 #!/bin/sh Menu_Option=0 echo "WELCOME" echo "--------------------------------------------------------------" while [ $Menu_Option -ne 4 ] d
bash
,这是在bin/sh
中
问题在于while循环,while[$currentID-gt 0]
。错误是“21:[:2893:意外操作员
#!/bin/sh
Menu_Option=0
echo "WELCOME"
echo "--------------------------------------------------------------"
while [ $Menu_Option -ne 4 ]
do
echo "select a menu option"
echo "1- ancestry history"
echo "2- who is online"
echo "3- what process any user is running"
echo "4- exit"
read Menu_Option
case $Menu_Option in
1)
echo "THE ANCESTRY TREE FOR CURRENT PROCESS IS...."
echo " "
ps -ef > file1
currentID=$(awk '{if ($3 == '$PPID') print $2}' file1)
while [ $currentID -gt 0 ]
do
Process_Name=$(awk '{if ($2 == $currentID) print $8 " " $9}' file1)
我希望以下是您需要的:
#!/bin/sh
Menu_Option=0
echo "WELCOME"
echo "--------------------------------------------------------------"
while [ $Menu_Option -ne 4 ]
do
echo "select a menu option"
echo "1- ancestry history"
echo "2- who is online"
echo "3- what process any user is running"
echo "4- exit"
read -r Menu_Option
case $Menu_Option in
1)
echo "THE ANCESTRY TREE FOR CURRENT PROCESS IS...."
echo " "
ps -ef > file1
currentID=$(awk '{if ($3 == '$PPID') print $2}' file1)
while [ "$currentID" -gt 0 ]
do
Process_Name=$(awk '{if ($2 == '"$currentID"') print $8 " " $9}' file1)
echo "$Process_Name"
currentID=$(awk '{if ($3 == '"$currentID"') print $2}' file1)
if [ "$currentID" = "" ]
then
currentID=-1;
fi
done
echo ""
esac
done
您是否检查了
currentID
设置为什么?在设置后添加类似echo$currentID
的内容,以查看您从awk
返回了什么。它返回了多个值。我如何解决此问题?如果不知道您的意图以及返回了什么值,很难说清楚。我打算查找当前值进程ID来映射进程树。它返回4个进程ID’不要忘记引用变量,尤其是在[…]
中。如果不这样做,你只会自找麻烦。(如果你没有弄清楚,这可能会导致错误…)谢谢。我很好奇为什么你在awk声明中使用单引号和双引号。一旦我完成了脚本,我得到了评论和反馈。做出了相应的更改。