linux-day03

1 test

Basic grammar 

1) test 

2) [    ]

1.1 Number 

 [ 3 -eq 3 ]  && echo "en" || echo "ene~~"
en
 [ 3 -gt  3 ]  && echo "en" || echo "ene~~"            
ene~~
[ 3 -ge  3 ]  && echo "en" || echo "ene~~" 
en
[ 3 -lt  3 ]  && echo "en" || echo "ene~~"          
ene~~
 [ 3 -le  3 ]  && echo "en" || echo "ene~~" 
en
[ 3 -ne  3 ]  && echo "en" || echo "ene~~"  
ene~~
test 3 -eq 4 && echo "en" || echo "ene~~" 
ene~~

 

1.2 String

test str1  == str2 test whether the strings are equal

test str1 != str2 test string is not equal  
test str1 test string is not empty    !=null  【str】 【$str】 If it is empty, return false (null!=str) {}
test -n str1 test string Is not empty, not empty, true, false
test -z str1 test whether the string is empty

[ $name ] && echo  "shi" || echo "buhsi"
shi
 [ abc ]  && echo  "shi" || echo "buhsi"
shi
 [ $name ]
[ $name == mayun ] && echo  "shi" || echo "buhsi"
shi
 [ $name != mayun ] && echo  "shi" || echo "buhsi" 
buhsi
 test -e /a.txt  && echo  "shi" || echo "buhsi" 
buhsi

 

1.3 File

[ -e /1.txt ]  && echo  "shi" || echo "buhsi" 
shi
[ -e /1.txt ]  && echo  "shi" || echo "buhsi" 
shi
[ -f /1.txt ] && echo "shi" || echo "bushi"
shi
[ -d /1.txt ] && echo "shi" || echo "bushi" 
bushi
[ -e /1.txt ] && echo "shi" || echo "bushi" 
shi
[ -r /1.txt ] && echo "shi" || echo "bushi" 
shi
[ -w /1.txt ] && echo "shi" || echo "bushi" 
shi
[ -x /1.txt ] && echo "shi" || echo "bushi" 
bushi
[ -L /1.txt ] && echo "shi" || echo "bushi" 
bushi

1.4 Multiple conditions

Multiple conditions

-a    and

-o    or

[ -r ./first.sh  -a  -w ./first.sh ]  && echo "en"   ||  echo "enen~~~"       
en

 [ -r ./first.sh  -o  -x ./first.sh ]  && echo "en"   ||  echo "enen~~~"    

en

 [ -r ./first.sh  -a  -x ./first.sh ]  && echo "en"   ||  echo "enen~~~"   

enen~~~

&& 和 ||

&& Logic and conditions are met before the following statement is executed

[ -z “$name” ] && echo  invalid  || echo ok

|| Logical OR, the following statement is executed only if the condition is not met

test “$name” == ”yangmi” && echo ok  || echo  invalid

2 flow control if 

if [  ]

then 

else 

be

if [  ] 

    then   ...

elif []

   then  ... 

else

        .....

be

if [  ]

then 

     if  [  ]

      then   ...

      else ...

    be 

else  ....

be

#!/bin/bash
# 注释   测试if流程控制
name=zss
if [ $name == lss ]
  then echo "你好李四"
else
  echo "你好张三"
fi
----------------------------------
#!/bin/bash
# $1 特殊的变量  接收执行脚本的参数的  $1  $2 $3 ... $9  ${10}
name=$1
age=$2
echo  $name 
echo $age
----------------------------------------
#!/bin/bash
#
name=$1
if [ $name == caocao ]
  then echo "weiguo"
elif [ $name == sunquan ]
  then echo "wuguo"
else 
  echo "shuguo"
fi  
------------------------------------------
#!/bin/bash
#
name=$1
age=$2
if [ $age -gt 18 ]
then 
    if [ $name == zss]
	 then echo "老男人"
	elif [ $name == banzhang ]
	  then echo "不太老的男人"
	 else echo "老娘们"
	fi
else 
echo  "game over"
fi

 

3 for process control

#!/bin/bash
for (( i=1 ; i<=10 ;i++ ))
do
# 求i的平方
 echo  $(($i*$i))
done
-----------------------------
#!/bin/bash
for i in 1 2 3 4 5 
do 
echo  $i
done
---------------------------------
#!/bin/bash
for i in 1 2 3 
do 
echo  linux0$i
done
----------------------------------
#!/bin/bash
for hostname in linux01 linux02 linux03 
do 
echo    $hostname
done
---------------------------------
#!/bin/bash
for  name  in  `cat /doit18/hosts.txt`
do 
echo   $name
done
------------------------------------写一个脚本 启动集群中所有的tomcat
#!/bin/bash
for hostname in  linux01  linux02  linux03 
do
ssh $hostname "source /etc/profile ;/opt/apps/apache-tomcat-7.0.47/bin/startup.sh;exit"
done
-------------------------停止集群中所有的tomcat

#!/bin/bash
for hostname in  linux01  linux02  linux03 
do
ssh $hostname "source /etc/profile ;/opt/apps/apache-tomcat-7.0.47/bin/shutdown.sh;exit"
done

4 process control while

5 case syntax special branch statement (short for if)

#!/bin/bash
case  $1  in 
"zss")
echo "zhangsansan"
;;
"lss")
echo "lisisi"
;;
"abc")
 echo "ABC"
;;
*)
 echo "qita"
esac

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_37933018/article/details/108643504