shell脚本语法错误:在完成命令期间重定向意外

时间:2013-09-13 03:27:49

标签: bash shell ubuntu awk

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

2 个答案:

答案 0 :(得分:7)

不,这是因为你使用sh作为翻译。将其改为bash。

答案 1 :(得分:4)

引起麻烦的行是:

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

这些正在使用'process substitution',这是bash的一项功能,在sh运行时会被禁用。由于脚本顶部的shebang是#!/bin/sh,这会导致麻烦。您可以通过运行来证明这是问题:

bash yourscript.sh

当你这样做时,你不应该再犯这个错误;你可能会得到其他错误,或者它可能都有效。全部修复后,将shebang更改为#!/bin/bash