所以,我正在尝试为一个类制作一个 shell 脚本,它基本上只是执行一些管理操作。现在,我在第 27 行和第 45 行的 done 命令上遇到语法错误。我觉得这可能是我对 awk 的使用。
#!/bin/sh
ps -cefl > userInfo
while [ "$menuOption" != 4 ]; do
echo "===================================="
echo "Select a menu option!"
echo "(1) Ancestry History"
echo "(2) Who's online"
echo "(3) What process a user is running"
echo "(4) Exit"
echo -n "Menu option:"
read menuOption
echo " "
if [ "$menuOption" -eq 1 ]; then
echo "The ancestry tree for the current process is. . ."
echo " "
PID=$$
while [ $PID -ne 1 ]
do
echo $PID
echo " | "
while read PIDS; do
myPID=$(echo $PIDS | awk '{print $1}')
myPPID=$(echo $PIDS | awk '{print $2}')
if [ $myPID -eq $PID ]; then
PID=$myPPID
fi
done < <(grep $PID userInfo | awk '{printf "%i %i\n",$4,$5}')
done
echo " 1 "
echo " "
elif [ "$menuOption" -eq 2 ]; then
echo "Online users:"
echo "-------------"
who | awk '{print $1}' | sort -u
echo "-------"
echo " "
elif [ "$menuOption" -eq 3 ]; then
i=0
echo "Select a user to see the processes!"
echo "-----------------------------------"
while read value; do
listNames["$i"]="$value"
echo "$i)${listNames[$i]}"
i=$(($i+1))
done < <(who | awk '{print $1}' | sort -u)
echo -n "Select user: "
read userOption
echo " "
echo "You've selected: ${listNames[$userOption]}"
grep ${listNames[$userOption]} userInfo | awk '{printf "%-7s %-7s %-7s %-7s %-7s %-7s %-10s %s %s\n",$3,$4,$5,$9,$11,$12,$13,$14,$15}' | sort;
echo " "
elif [ "$menuOption" -eq 4 ]; then
echo "Exiting Script!"
fi
done