Shell腳本語法-- if/then/elif/else/fi 數組
和C語言相似,在Shell中用if、then、elif、else、fi這幾條命令實現分支控制。這種流程控制語句本質上也是由若干條Shell命bash
- if [ -f ~/.bashrc ]; then
- . ~/.bashrc
- fi
- #! /bin/sh
- if [ -f /bin/bash ]
- then echo "/bin/bash is a file"
- else echo "/bin/bash is NOT a file"
- fi
- if :; then echo "always true"; fi
- #! /bin/sh
- echo "Is it morning? Please answer yes or no."
- read YES_OR_NO
- if [ "$YES_OR_NO" = "yes" ]; then
- echo "Good morning!"
- elif [ "$YES_OR_NO" = "no" ]; then
- echo "Good afternoon!"
- else
- echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
- exit 1
- fi
- exit 0
- #!/bin/bash
- echo "Enter your name: "
- read A
- if [ "$A" = "GS" ];then
- echo "yes"
- elif [ "$A" = "UC" ];then
- echo "no"
- else
- echo "your are wrong"
- fi
- #!/bin/bash
- read -p "Enter your name: " A
- if [ "$A" = "GS" ];then
- echo "yes"
- elif [ "$A" = "UC" ];then
- echo "no"
- else
- echo "your are wrong"
- fi