if条件中的奇怪语法错误 - Shell脚本

时间:2018-01-01 14:32:40

标签: bash shell

所以我在Ubuntu中制作了一个shell脚本。它的目的很简单。您给出带参数的命令,每次都会得到不同的操作。问题是,当我运行脚本时,由于一个elif中的语法错误,它实际上无法运行。最可疑的是,我有一个类似的elif以上工作或至少没有弹出语法错误... 我离开我的代码让你看到它并理解。提前谢谢!

if [ "$1" = "-a" -a $# -lt 3 ]
then
    echo "Add a new line in katalogos!"
    read -p "Give me a name... " name
    read -p "Give me a surname... " surname
    read -p "Give me a city name... " cityName
    read -p "Give me a phone number... " num
    echo "$name $surname $cityName $num" > katalogos
elif [ "$1" = "-l" -a $# -lt 3 ]
then
    echo "Content of katalogos will be sorted numerically and blank lines will be excluded!"
    sort -b -n katalogos
elif [ "$1" = "-s" -a $# -lt 4 ]
    if [[ $2 != *[!0-9]* ]]
    then
        echo "Content of katalogos will be sorted according to the second argument!"
        sort +$3 katalogos
    fi
elif [ "$1" = "-c" -a $# -lt 4 ] // syntax error
    if [[ $2 = *[!0-9]* ]]
    then
        echo "Content of katalogos will be sorted according to the keyword!"
        if [ $(grep -e "$2" katalogos | wc -l) -eq 0 ]
        then
            echo "String is not matched."
        else
            grep -e "$2" katalogos
        fi
    fi
elif [ "$1" = "-d" -a ( "$3" = "-b" -o "$3" = "-r" ) ]
    if [[ $2 = *[!0-9]* ]]
    then
        echo "Katalogos's string matching lines will be deleted and blank lines will be in their place, assuming that the third argument equals -b, else just the lines will be deleted!"
        if [ $(grep -e $2 katalogos | wc -l) -eq 0 ]
        then
            echo "String is not matched."
        else
            if [ "$3" = "-b" ]
            then
                sed -i "$3" katalogos | sed -i '$ a '
                echo "A blank line inserted in place of the deleted one."
            else
                sed -i "$3" katalogos
                echo "Line deleted."
            fi
        fi
    fi
elif [ "$1" = "-n" ]
    echo "katalogos's number of blank lines will be shown with the ability to delete them!"
    grep -cvP '\S' katalogos
    read -p "Do you want to delete them? Type 1 for yes or 0 for no... " ans
    if [ $ans -eq 1 ]
    then
        grep -cvP '\S' file | sed -i
        echo "Lines deleted."
    fi
else
    echo "Help centre!"
    echo "-Type ./telcat -a to insert a new line to katalogos."
    echo "-Type ./telcat -l to see the contents of katalogos sorted numerically (excluding blank lines)."
    echo "-Type ./telcat -s plus a number to see the contents of katalogos sorted by the data that the number points to."
    echo "-Type ./telcat -c plus a keyword to see only the lines that match with the word given."
    echo "-Type ./telcat -d plus a keyword and -b or -r to delete the lines that contain the word given. Specifically if the third argument is -b it will automatically add a blank line to the deleted one and if it is -r it will not."
    echo "-Type ./telcat -n to see the number of blank lines of katalogos."
    echo "End of help centre!"
fi

0 个答案:

没有答案