需求描述:
在寫shell腳本的過程中,用到了if else的寫法,突然有多個參數需要判斷
那么就想到了if else if的用法,於是進行如下的測試。
測試過程:
1.寫如下的測試腳本,進行多個值的判斷
#!/bin/bash if [[ $1 = 'tomcat' ]]; then echo "Input is tomcat" else if [[ $1 = 'redis' ]] || [[ $1 = 'zookeeper' ]]; then echo "Input is $1" else echo "Input Is Error." fi
2.執行腳本,看腳本是否正常執行
[oracle@standby ~]$ ./ts01.sh zookeeper
./ts01.sh: line 12: syntax error: unexpected end of file
備注:發現執行是錯誤的,經過查看可以知道,shell腳本中不是else if而是elif這個寫法
3.修改腳本
#!/bin/bash if [[ $1 = 'tomcat' ]]; then echo "Input is tomcat" elif [[ $1 = 'redis' ]] || [[ $1 = 'zookeeper' ]]; then echo "Input is $1" else echo "Input Is Error." fi
4.再次執行修改過的腳本
[oracle@standby ~]$ ./ts01.sh zookeeper Input is zookeeper [oracle@standby ~]$ ./ts01.sh tomcat Input is tomcat [oracle@standby ~]$ ./ts01.sh redis Input is redis [oracle@standby ~]$ ./ts01.sh mysql Input Is Error.
備注:腳本執行正常,正確的輸出了需要的結果。
shell腳本中else if的正確使用方法:
if condition; then commands; elif condition;then commands; else commands; fi
文檔創建時間:2018年3月14日10:54:11