1 if Analyzing
1. The basic syntax
if [ conditional formula ]; then
program
be
or
if [ conditional formula ]
then
program
elif [ conditional formula ]
then
program
else
program
be
Precautions:
( 1 ) [ conditional formula ] must be a space between the brackets, and conditional formula
( 2 ) IF after to have space
2. Case practical operation
( 1 ) input a digit , if a 1 , then the output banzhang zhen shuai, if is 2, then the output cls zhen mei, if other , outputs nothing.
[atguigu@hadoop101 datas]$ touch if.sh
[Atguigu @ hadoop101 dates] $ came if.sh
#!/bin/bash
if [ $1 -eq "1" ]
then
echo "banzhang zhen shuai"
elif [ $1 -eq "2" ]
then
echo "cls zhen mei"
be
[atguigu@hadoop101 datas]$ chmod 777 if.sh
[atguigu@hadoop101 datas]$ ./if.sh 1
banzhang zhen shuai
2, case statements
1. The basic syntax
case $ variable name in
" Value 1" )
If the value of the variable equal to the value 1 , then the execution of the program 1
;;
" Value 2" )
If the value of a variable equal to the value 2 , the program is executed 2
;;
... omitting other branches ...
*)
If the value of the variable is not more than the value, the implementation of this program
;;
esac
Precautions:
1) case end of a line must be the word " in ", each pattern match must end with a closing parenthesis ")."
2) double semicolon " ;; " indicates the end of a command sequence, corresponds java of BREAK .
3) The last " * )" indicates the default mode, the equivalent of java in default.
2. Case practical operation
( 1 ) input a digit , if a 1, then the output banzhang, if is 2, then the output CLS, if the other , the output renyao .
[atguigu@hadoop101 datas]$ touch case.sh
[Atguigu @ hadoop101 dates] $ came case.sh
!/bin/bash
case $1 in
"1")
echo "banzhang"
;;
"2")
echo "cls"
;;
*)
echo "renyao"
;;
esac
[atguigu@hadoop101 datas]$ chmod 777 case.sh
[atguigu@hadoop101 datas]$ ./case.sh 1
1
3 for circulation
1. The basic syntax 1
for (( initial value ; circulation control condition ; variable change ))
do
program
done
2. Case practical operation
( 1 ) From 1 was added to 100
[atguigu@hadoop101 datas]$ touch for1.sh
[Atguigu @ hadoop101 dates] $ came for1.sh
#!/bin/bash
s=0
for((i=0;i<=100;i++))
do
s=$[$s+$i]
done
echo $s
[atguigu@hadoop101 datas]$ chmod 777 for1.sh
[atguigu@hadoop101 datas]$ ./for1.sh
“5050”
3. The basic syntax 2
for the variable in value 1 value 2 value 3 ...
do
program
done
4. Case practical operation
( 1 ) Print all input parameters
[atguigu@hadoop101 datas]$ touch for2.sh
[Atguigu @ hadoop101 dates] $ came for2.sh
#!/bin/bash
# Print digital
for i in $*
do
echo "ban zhang love $i "
done
[atguigu@hadoop101 datas]$ chmod 777 for2.sh
[atguigu@hadoop101 datas]$ bash for2.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd
( 2 ) compare $ * and $ @ difference
(A) $ * and $ @ expressed all the parameters passed to the function or script, it is not double quotation marks "" included, have to $ 1 $ 2 ... $ n output parameters all forms .
[atguigu@hadoop101 datas]$ touch for.sh [Atguigu @ hadoop101 dates] $ came for.sh
#!/bin/bash
for i in $* do echo "ban zhang love $i " done
for j in $@ do echo "ban zhang love $j" done
[atguigu@hadoop101 datas]$ bash for.sh cls xz bd ban zhang love cls ban zhang love xz ban zhang love bd ban zhang love cls ban zhang love xz ban zhang love bd |
(b)当它们被双引号“”包含时,“$*”会将所有的参数作为一个整体,以“$1 $2 …$n”的形式输出所有参数;“$@”会将各个参数分开,以“$1” “$2”…”$n”的形式输出所有参数。
[atguigu@hadoop101 datas]$ vim for.sh
#!/bin/bash
for i in "$*"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "ban zhang love $i"
done
for j in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "ban zhang love $j"
done
[atguigu@hadoop101 datas]$ chmod 777 for.sh
[atguigu@hadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd
4 while 循环
1.基本语法
while [ 条件判断式 ]
do
程序
done
2.案例实操
(1)从1加到100
[atguigu@hadoop101 datas]$ touch while.sh
[atguigu@hadoop101 datas]$ vim while.sh
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
s=$[$s+$i]
i=$[$i+1]
done
echo $s
[atguigu@hadoop101 datas]$ chmod 777 while.sh
[atguigu@hadoop101 datas]$ ./while.sh
5050