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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18777815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:33:01  来源:igfitidea点击:

shell script syntax error: redirection unexpected during done command

bashshellubuntuawk

提问by T.J. Williams

So, I'm trying to make a shell script for a class and it basically just performs a few administrative operations. Right now, I'm getting a syntax error on the done command on lines 27 and 45. I feel it might be my use of 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 }')
                myPPID=$(echo $PIDS | awk '{print }')
                if [ $myPID -eq $PID ]; then
                    PID=$myPPID
                fi
            done < <(grep $PID userInfo | awk '{printf "%i %i\n",,}')
        done
        echo " 1 "
        echo " "
    elif [ "$menuOption" -eq 2 ]; then
        echo "Online users:"
        echo "-------------"
        who | awk '{print }' | 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 }' | 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",,,,,,,,,}' | sort;
        echo " "
    elif [ "$menuOption" -eq 4 ]; then
        echo "Exiting Script!"
    fi
done

回答by Ignacio Vazquez-Abrams

No, it's due to your use of sh as an interpreter. Change it to bash.

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

回答by Jonathan Leffler

The lines causing trouble are:

引起问题的线路是:

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

These are using 'process substitution', which is a feature of bashthat is disabled when it is run as sh. Since the shebang at the top of the script is #!/bin/sh, this causes trouble. You can demonstrate that it is the problem by running:

这些正在使用“进程替换”bash当它作为sh. 由于脚本顶部的 shebang 是#!/bin/sh,这会导致麻烦。您可以通过运行来证明这是问题所在:

bash yourscript.sh

When you do that, you should no longer get that error; you might get other errors, or it might all work. When all is fixed, change the shebang to #!/bin/bash.

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