linux shell學習筆記1

下面的腳本在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
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章