需求描述:mysql
在寫shell腳本的過程當中,用到了if else的寫法,忽然有多個參數須要判斷redis
那麼就想到了if else if的用法,因而進行以下的測試。sql
測試過程:shell
1.寫以下的測試腳本,進行多個值的判斷tomcat
#!/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.執行腳本,看腳本是否正常執行bash
[oracle@standby ~]$ ./ts01.sh zookeeper
./ts01.sh: line 12: syntax error: unexpected end of file
備註:發現執行是錯誤的,通過查看能夠知道,shell腳本中不是else if而是elif這個寫法oracle
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.再次執行修改過的腳本spa
[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.
備註:腳本執行正常,正確的輸出了須要的結果。code
shell腳本中else if的正確使用方法:
if condition; then commands; elif condition;then commands; else commands; fi
文檔建立時間:2018年3月14日10:54:11