带有if,elif,else的Bash脚本 [英] Bash Script with if, elif, else

查看:64
本文介绍了带有if,elif,else的Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是一个作业,因此我不会在此处输入确切的脚本,但是在这一点上我真的很绝望,因为我无法弄清楚基本的东西.因此,我基本上是在检查在命令行中写入的两个参数是否正确(用户需要正确键入),否则它将回显特定的错误消息.但是,当我输入带有100%正确参数的命令时,我会从第一个条件ALWAYS中获得错误回显消息(即使我切换条件语句).看来该脚本只是运行第一个回显而无论如何都停止.请帮忙,我理解这可能很难,因为我的代码更像是骨架.

Okay so this is an assignment so I will not put in the exact script here but I am really desperate at this point because I cannot figure something as basic as if's. So I am basically checking if the two arguments that are written in the command line are appropriate (user needs to type it correctly) or it will echo a specific error message. However, when I put in a command with 100% correct arguments, I get the error echo message from the first conditional ALWAYS (even if I switch around the conditional statements). It seems that the script just runs the first echo and stops no matter what. Please help and I understand it might be hard since my code is more of a skeleton.

 if [ ... ]; then
 echo "blah"

 elif [ ... ]; then
 echo "blah2"

else for file; do
#change file to the 1st argument

done 
fi 

为了使我的脚本真正达到其预期的目的,我显然需要执行其他操作.但是,我的if-fy问题正在困扰. if和elif需要返回false以便脚本运行适当的参数. if和elif检查人员是否在命令行中正确键入.

I obviously need the last else to happen in order for my script to actually serve its intended purpose. However, my if-fy problem is getting in the way. The if and elif need to return false in order for the script to run for appropriate arguments. The if and elif check to see if the person typed in the command line correctly.

推荐答案

elif表示else-if.因此,仅在第一条语句返回false时才进行检查.因此,如果您想检查两者是否正确,就可以.

elif mean else-if. So it only will only be checked if the first statement returns false. So if you want to check if both are correct do.

if [ ... ] then
...
fi

if [ ... ] then
...
fi

这篇关于带有if,elif,else的Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆