下面的脚本在linux as4系统中执行无误。欢迎学习讨论!(此笔记采用两中颜色区分不同的shell脚本)
一 . echo命令用于显示文本行或变量。
echo [optiong] string
optiong选项:
-e解析string 中的转义字符
-n回车不换行,linux系统默认会车是换行的
string中的转义符 (/c,/f,/t,/n)/c回车表示会车不换行 ,/f 禁止,/t相当与tab键 ,/n表示会车换行
实例:
#!/bin/bash
#echod
echo -e "this echo's 3 new lines/n/n/n"
echo "ok"
echo
echo "this echo's 3 new lines/n/n/n"
echo "this log files have all been done">mylogfile.txt
二 .read语句
read语句可以从键盘或文件中的一行文本中读入信息,并将其给一个变量。read varible1 varible2 …
如果只指定了一个变量,那么read将会把所有的输入赋给此变量,只至遇到第一个文件结束符或会车;如果给出了多个变量,他们按顺序分别被富裕不同的变量。Shell将用空格作为变量间的分割符。
实例:
#!/bin/bash
#readname
echo –n “first name:”
read firstname
echo –n “last name:”
read lastnme
echo –e “your first name is :${firstnaem}/n”
echo –e “your last name is :${lastname}/n”
三 流控制
[root@localhost ~]# cat caseselect.txt
#!/bin/bash
#case select
echo -n "Enter a number from 1 to 3:"
read ANS
case $ANS in
1)
echo "you select 1"
;;
2)
echo "you select 2"
;;
3)
echo "you select 3"
;;
*)
echo "'basename $0': This not between 1 and 3" >&2
exit;
;;
esac
[root@localhost ~]# cat forlist2.txt
#!/bin/bash
for loop in "orange red blue grey"
do
echo $loop
done
[root@localhost ~]# cat forlist3.txt
#!/bin/bash
for loop in `cat myfile`
do
echo $loop
done
[root@localhost ~]# cat forloop.txt
#!/bin/bash
for loop in 1 2 3 4 5
do
echo $loop
done
[root@localhost ~]# cat ifcp.txt
#!/bin/bash
if cp myfile.bak myfile
then
echo "success copy"
else
echo "'basename $0':error could not copy the file">&2
fi
[root@localhost ~]# cat ifelif.txt
#!/bin/bash
#ifelif
echo -n "Enter your name:"
read NAME
if [ -z $NAME ] || [ "$NAME" = "" ];then
echo "YOU did not enter a name."
elif [ "$NAME" = "root" ];then
echo "Hello root"
elif [ "$NAME" = "chinaitlab" ];then
echo "Hello chinaitlab"
else
echo "You are not root for chinaitlab,but hi,$NAME"
fi
[root@localhost ~]# cat iftest
#!/bin/bash
#if test
if [ "10" -lt "12" ]
then
echo "yes 10 is less than 12"
else
echo "no"
fi
[root@localhost ~]# cat iftest2
#!/bin/bash
#if test2
echo -n "Enter your name:"
read NAME
if [ "$NAME" == "" ];then
echo "You did not enter any information"
else
echo "Your name is ${NAME}"
fi
[root@localhost ~]# cat linshi.sh
cat > fileA << "EOF"
this is line 1.
This is line 2.
EOF
[root@localhost ~]# cat name.txt
chinaitlab
shenzhen
[root@localhost ~]# cat names.txt
shenzhen
shanghai
beijing
[root@localhost ~]# cat until_mon
#!/bin/bash
#until_mon
#montior the parttion used
Part="/home"
#get disk used
LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do
echo "Filesystem /home is nearly full"|mail root
done
[root@localhost ~]# cat until2_mon
#!/bin/bash
#until_mon
#montior the parttion used
Part="/home"
#get disk used
LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''
echo $LOOK_OUT
until [ "$LOOK_OUT" -gt "90" ]
do
echo "Filesystem /home is nearly full"|mail root
LOOK_OUT='df|grep "$Part"|awk '{print ${print $5}'|sed 's/%//g''
sleep 3600
done
[root@localhost ~]# cat whilebreakout.txt
#!/bin/bash
#breakout
while :
do
echo -n "Enter any number [1...5]:"
read ANS
case $ANS in
1|2|3|4|5)
echo "you enter a number between 1 and 5."
;;
*)
echo "wrong number,Bye."
break
;;
esac
done
[root@localhost ~]# cat whilebreakout2.txt
#!/bin/bash
#breakout
while :
do
echo -n "Enter any number [1...5]:"
read ANS
case $ANS in
1|2|3|4|5)
echo "you enter a number between 1 and 5!"
;;
*)
echo -n "Wrong number,continue (y/n)?:"
read IS_CONTINUE
case $IS_CONTINUE in
y|yes|Y|Yes)
continue
;;
*)
break
;;
esac
esac
done
[root@localhost ~]# cat whileread.txt
echo "press <ctrl>+d to return."
while echo -n "enter the film name you love best:";read FILM
do
echo "Yeah,${FILM} is a good file!"
done
[root@localhost ~]# cat whileread2.txt
#!/bin/bash
#whileread
while read LINE
do
echo $LINE
done<names.txt
[root@localhost ~]# cat whileread2.txt
#!/bin/bash
#whileread
while read LINE
do
echo $LINE
done<names.txt
[root@localhost ~]# cat whileread3.txt
#!/bin/bash
#whileread
while read LINE<names.txt
do
echo $LINE
done<names.txt