语法错误:文件结束意外(期待“fi”),但fi已经存在

时间:2017-07-18 15:40:05

标签: bash

当我试图运行这个bash时,它给了我一个语法错误,结果是意外的,它期望最后一行的“fi”,它已经存在。我仍然是bash的新手,但我知道做基本的东西。有谁知道我做错了什么?

#!/bin/sh
echo "============================================================================================"
echo "||What is the Songs Genre (Classical, Country, Disco, Dubstep, Pop, Rap, Rock, or Techno)?||"
echo "============================================================================================"

read "Genre"

if ( $genre=="Classical")
then 
cd ~/Music/Classical

if ( $genre=="Country") 
then
cd ~/Music/Country

if ( $genre=="Disco") 
then
cd ~/Music/Disco

if ( $genre=="Dubstep")
then 
cd ~/Music/Dubstep

if ( $genre=="Pop") 
then
cd ~/Music/Pop

if ( $genre=="Rap")
then 
cd ~/Music/Rap

if ( $genre=="Rock")
then 
cd ~/Music/Rock

if ( $genre=="Techno")
then 
cd ~/Music/Techno

fi

echo "============================"
echo "||Paste Youtube link here.||"
echo "============================"

read "Link"
sudo youtube-dl -x --audio-format mp3 $Link

echo "========================"
echo "||Any More songs?(Y/N)||"
echo "========================"

read "Loop"

if $Loop==Y
then
cd
sh youtube.sh & exit

else
exit
fi

2 个答案:

答案 0 :(得分:1)

您需要在每个if...then阻止后添加if,或者将第一个elif语句更改为{{1}}。

答案 1 :(得分:1)

这可以更简洁。我写这个:

#!/bin/bash
echo "=============================================="

# put the genre names into an array 
genres=( Classical Country Disco Dubstep Pop Rap Rock Techno )

# use `select` instead of a cascading if statement
PS3="Select a genre: "
select choice in "${genres[@]}"; do
    # a tricky condition to check if the choice is in the array
    if [[ " ${genres[*]} " == *" $choice "* ]]; then
        break
    fi
done

read -p "Enter Youtube link: " link

mkdir -p ~/Music/"$choice"       # ensure the directory exists
cd ~/Music/"$choice"
youtube-dl -x --audio-format mp3 "$link"    # do you really need sudo for this?

read -p "Again?(Y/N) " again
if [[ $again == [yY]* ]]; then
    exec "$0"                    # re-launch without hardcoding the program name
fi