shell学习记录---4

来源:互联网 发布:跨数据库 sql语句 编辑:程序博客网 时间:2024/05/02 02:49




test很常用  

可以通过man test查看用法.



#!/bin/bash#ifcpif cp $1 $2 ;thenecho "cp ok !"elseecho "file $0:error cp"fi

[root@localhost Exercise]# ./ifcp.txt info.txt  myinfo.txtcp ok ![root@localhost Exercise]# cat myinfo.txt /dev/mapper/VolGroup00-LogVol0014093368/dev/sda1tmpfs.host:/[root@localhost Exercise]# ./ifcp.txt in1fo.txt  myinfo.txtcp: cannot stat `in1fo.txt': No such file or directoryfile ./ifcp.txt:error cp[root@localhost Exercise]# 

echo "`basename $0`:error cp" >&2

`basename $0`  文件名;

#!/bin/bash#ifelseifecho -n "Enter your name ! :"read NAMEif [ -z "$NAME" ] || [ "$NAME" = "" ];thenecho "you did not enter any thing"elif [ "$NAME" = "root" ];thenecho "Hello root"elseecho "you are not root ,but Hi ,$NAME"fi

[root@localhost Exercise]# ./ifelseif Enter your name ! :root Hello root[root@localhost Exercise]# ./ifelseif Enter your name ! :yikaiyou are not root ,but Hi ,yikai[root@localhost Exercise]# 


#!/bin/bash#caseecho -n "Enter a number from 1 to 3 :"read NUMcase $NUM in1)echo "you select 1";;2)echo "you select 2";;3)echo "you select 3";;*)echo "`basename $0`:wrong number !!!"exit;;;esac

[root@localhost Exercise]# ./case Enter a number from 1 to 3 :2you select 2[root@localhost Exercise]# ./case Enter a number from 1 to 3 :3you select 3[root@localhost Exercise]# ./case Enter a number from 1 to 3 :6case:wrong number !!!

for :

以空格分割:

#!/bin/bash#forlistfor loop in 1 2 3 4 5doecho $loopdone

[root@localhost Exercise]# chmod 755 forlist [root@localhost Exercise]# ./forlist 12345[root@localhost Exercise]# 
引号包括:
#!/bin/bash#forlistfor lop in "1 2 3 4 5"doecho $lopdone

[root@localhost Exercise]# ./forlist 1 2 3 4 5[root@localhost Exercise]# 

以空格分割:

#!/bin/bash#forlistfor lop in `cat hello.txt`doecho $lopdone

[root@localhost Exercise]# cat hello.txt Are you ok ?Bye!!![root@localhost Exercise]# vim forlist[root@localhost Exercise]# ./forlist Areyouok?Bye!!![root@localhost Exercise]# 



nouhp 后台执行;

nouhp ./名称


#!/bin/bash#whilereadecho "choose <ctrl>+D to break !"while echo -n "your favourite film:" ; read Filmdoecho "Yes! ${Film} is a good film!"done

[root@localhost Exercise]# ./whileread choose <ctrl>+D to break !your favourite film:KATYes! KAT is a good film!your favourite film:KATYes! KAT is a good film!your favourite film:[root@localhost Exercise]# [root@localhost Exercise]# 

$1的位置很重要:

#!/bin/bash#whileread2while read LINEdoecho $LINEdone<$1


[root@localhost Exercise]# ./whileread2 hello.txt Are you ok ?Bye!!![root@localhost Exercise]# cat hello.txt Are you ok ?Bye!!![root@localhost Exercise]# 

$1的位置:

#!/bin/bash#whileread2while read LINE<$1doecho $LINEdone

[root@localhost Exercise]# ./whileread2 hello.txt Are you ok ?Are you ok ?Are you ok ?Are you ok ?Are you ok ?Are you ok ?......
while 的使用  

记住 while与:间有个空格;

#!/bin/bash#breakoutwhile :   # ///////记住  while与:间有个空格;doecho -n "Enter any number [1...5]:"read ANScase $ANS in1|2|3|4|5)echo "you enter a number between 1 and 5.";;*)echo "Wrong number, Bye."break;;esacdone

[root@localhost Exercise]# ./breakout Enter any number [1...5]:4you enter a number between 1 and 5.Enter any number [1...5]:6Wrong number, Bye.[root@localhost Exercise]# 

continue 和break:

#!/bin/bash#breakoutwhile :doecho -n "Enter any number [1...5]:"read ANScase $ANS in1|2|3|4|5)echo "you enter a number between 1 and 5.";;*)echo -n "Wrong number, continue(y/n)"read yesornocase $yesorno iny|yes|Y|Yes)continue;;*)break;;esacesacdone

[root@localhost Exercise]# ./breakout Enter any number [1...5]:7Wrong number, continue(y/n)yEnter any number [1...5]:8Wrong number, continue(y/n)n[root@localhost Exercise]#