如何使用 else 作為第一個 if 指令

如何使用 else 作為第一個 if 指令

我目前正在編寫 bash 腳本。我確實有兩個 if 指令。我無法真正描述這裡的問題,所以我將用程式碼描述我的問題

#!/bin/bash
echo -e "What is the SGID?"
read Cevap50

if [ ! -f /home/fixscript/AudioBot$audiobot_port/.adminlikler_sgid_silmeyin ]
then
rm -rf .rights.toml
touch .adminlikler_sgid_silmeyin
echo -ne "$Cevap50" >> .adminlikler_sgid_silmeyin
adminlikler_sgid=`cat .adminlikler_sgid_silmeyin`
rights_bir
echo "        groupid = [ $adminlikler_sgid ]" >> .rights.toml
echo '  # And/Or your admin Client Uids here' >> .rights.toml
elif [ ! -f /home/fixscript/AudioBot$audiobot_port/.adminlikler_uid_silmeyin ]
then
echo "  useruid = []" >> .rights.toml
rights_iki
clear
else #There is the problem, this else should affect line 14th's if. But it just effects both of them.
#Some Codes
else #This else should affect line 5th's if.

答案1

將 if elif 和 else 視為鏈節,if 是開始,elifs 是中間鏈接,else 是結束鏈接。你寫的是“開始,中間鏈接,結束鏈接,結束鏈接”。為了分隔第 5 行和第 14 行的 if,您要尋找的是嵌套 if。

所以我的建議是這樣的

#!/bin/bash
echo -e "What is the SGID?"
read Cevap50

if [ ! -f /home/fixscript/AudioBot$audiobot_port/.adminlikler_sgid_silmeyin ]
then
    rm -rf .rights.toml
    touch .adminlikler_sgid_silmeyin
    echo -ne "$Cevap50" >> .adminlikler_sgid_silmeyin
    adminlikler_sgid=`cat .adminlikler_sgid_silmeyin`
    rights_bir
    echo "        groupid = [ $adminlikler_sgid ]" >> .rights.toml
    echo '  # And/Or your admin Client Uids here' >> .rights.toml
else #Effects line 5's if.
    if [ ! -f /home/fixscript/AudioBot$audiobot_port/.adminlikler_uid_silmeyin ]
    then
        echo "  useruid = []" >> .rights.toml
        rights_iki
        clear
    else # Effects line 15's if

相關內容