0

所以,我正在尝试为一个类制作一个 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
4

2 回答 2

7

不,这是因为您使用 sh 作为解释器。将其更改为 bash。

于 2013-09-13T03:29:36.490 回答
4

引起麻烦的线路是:

done < <(grep $PID userInfo | awk '{printf "%i %i\n",$4,$5}')
done < <(who | awk '{print $1}' | sort -u)

这些正在使用“进程替换”,这是一个bash在运行时被禁用的功能sh。由于脚本顶部的 shebang 是#!/bin/sh,这会引起麻烦。您可以通过运行来证明这是问题所在:

bash yourscript.sh

当你这样做时,你应该不再得到那个错误;您可能会遇到其他错误,或者可能一切正常。一切都解决后,将 shebang 更改为#!/bin/bash.

于 2013-09-13T04:04:04.783 回答