bash脚本无法正常工作,无法发现任何错误

时间:2017-05-30 18:29:34

标签: linux bash ubuntu case-statement

新用户在这里,对不起,如果我没有正确发布,但我一直盯着这段代码几个小时,我不知道它有什么问题,我之前做了一些代码非常相似,并且运行正常,请让我知道我做错了什么。

#!/bin/bash

export TERM=xterm
while true
do
    clear #Gets rid of previous output
    #displays main menu
    menu="Main menu: 
    showMan = Show the manual path.
    addMan = Add an item to the manual path.
    setMan = Set the manual path to '/man'.
    fileName = Enter filename.
    editFile = Edit file.
    headDisplay = How many lines to display of the head of the file?
    tailDisplay = How many lines to display of the tail of the file?
    listProcess = listing of processes
    userProcess = Processes run by user
    quit = Exit"
    echo -e "$menu";
    read -r option 
    #gets user choice
    clear
    case $option in
    "showMan") 
        echo $MANPATH 
        ;;

    "addMan")
        echo "Add an item"
        read -r item
        export MANPATH="$MANPATH:$item"
        ;;

    "setMan")
        export MANPATH="/man"
        ;;

    "fileName")
        echo "Enter filename:"
        read -r filename
        ;;

    "editFile")
        nano "$filename"
        ;;

    "headDisplay")
        echo "How many lines of code to display from top of the file?:"
        read -r numLines
        head -"$numLines" "$filename"
        ;;

    "tailDisplay")
        echo "How many lines of code to display from bottom of the file?:"
        read -r numLines
        tail -"$numLines" "$filename"
        ;;

    "listProcess")
        ps -ef
        ;;

    "userProcess")
        echo "Please enter username:"
        read -r username
        ps -u "$username" | sed 1d | wc -l
        ;;

    "quit")
        exit
        ;;

    *)
        echo "Invalid input."
        ;;
    esac

    #Asks user if they want to continue
    echo "Would you like to continue?  (Y/n)"
    read -r input
    if [ "$input" == "n" ]; then
        exit
    fi
done

1 个答案:

答案 0 :(得分:0)

你的问题是你脚本刚开始时的问题:

  

#!/斌/庆典

这指示您当前的shell使用此解释器/ shell来执行脚本。在您的情况下,它无法在 / bin / bash 找到可执行文件。

要解决此问题,您应该将其更改为bash shell可执行文件的位置(您可以使用“which bash”找到它)或删除它以使用当前shell。

我更喜欢路径修复,因为如果你在脚本中使用bash特定的命令和语句,如果你使用不同的shell作为默认值,它将会中断。