如何在第一个 if 命令中使用 else

如何在第一个 if 命令中使用 else

我目前正在编写一个 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 是开始,elif 是中间环节,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

相关内容