这是我的代码:
echo
echo "WELCOME"
echo "-------------------------------------------------"
while true
do
echo
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 mOption
case $mOption in
1) echo
echo "The Ancestry Tree For Current Process is....";
ps -ef > processes
grep "ps -ef" processes > grepProcesses
awk '{print $2, $3}' processes > awkProcesses
PID=$(awk '{print $2}' grepProcesses)
echo $PID
SPID=$(awk '{print $3}' grepProcesses)
PID=$SPID
end=0
while [ $end != 1 ]
do
echo " | "
echo $SPID
PID=$SPID
SPID=$(grep ^"$PID " awkProcesses | cut -d' ' -f2)
if [ "$PID" = "1" ]
then
end=1
fi
done
rm processes
rm grepProcesses
rm awkProcesses
;;
2) echo
echo "Users Currently Online";
who | cut -d' ' -f1
;;
3) echo
echo "Select a Currently Online User to View their Processes:"
index=0
who | while read onlineUser
do
echo "$index-$onlineUser" who|cut -d' ' -f1>>userList
index=$((index+1))
done
awk '{ print $0 }' userList
read choice
if [ $choice ]
then
echo
echo ***Process Currently Running***
person=$(grep ^$choice userList |cut -d'-' -f2)
ps -ef >> process
grep $person process
else
echo You have made a mistake. Please start over.
fi
rm userList
rm process
;;
4) echo
echo "Exiting..."
exit 1;;
*)
echo
echo "Invalid Input. Try Again."
;;
esac
done
每次运行它时,我都会不断收到语法错误“hmwk1.sh: 17: hmwk1.sh: Syntax error: word unexpected (expecting "in")” 我查找了不同的语法示例,看来我的代码看起来是正确的。但是我的反复试验让我无处修复我的代码。
我会不会遗漏了某种括号或引号?
最佳答案
如果你只是在提示符下按回车键
read mOption
case $mOption in
然后 $mOption 是一个空标记,使 shell 只看到
case in
这是一个语法错误。如果你添加
test -z "$mOption" && continue
在中间,如果会修复那个问题。
关于linux - 开关盒 : Confusing error in "Syntax error: word unexpected (expecting "in")",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399928/