學習 linux shell 的書例子

來源:互聯網
上載者:User

example about linux shell </p><p>#!/bin/bash<br />echo "Hello world!"<br />echo "Hello $LOGNAME,It's nice talking to you"<br />echo "U present working on a machine is `uname -n`"<br />echo "U present working directory is $(pwd)"<br />echo "Bye for now $LOGNAME.The time is $(date +%T)"</p><p>#===========================================<br />NAME="Tiger"<br />export name</p><p>#===========================================<br />DESC=$(awk -F: '{print $2}' database.txt)<br />echo $DESC</p><p>#===========================================<br />exec date #替換當前shell,建立立一個shell執行date<br />#===========================================<br />echo -e "Who are u ?"<br />read answer<br />echo "Hello $answer,Welcome!"</p><p>#===========================================<br />if [$LOGNAME !=root ]<br />then<br /> echo "Must have root privileges to run this program"<br /> exit 1<br />fi<br />cat <Warning:All jobs in the printer ques will be removed.<br />Please turn of the printer now.Press return when you are read to contine.Otherwise press Control C.<br />EOF<br />read JUNK #just to wait for user response<br />echo<br />/etc/rc.d/init.d/lpd stop<br />echo -e " please turn the printer on now."<br />echo "presss return to continue"<br />read JUNK<br />echo<br />/etc/rc.d/init.d/lpd start<br />#===========================================<br />wc < /etc/passwd<br />cat /etc/passwd|wc<br />#===========================================<br />條件 判斷是否為真<br />字元判斷:<br />[string1=string2] string1等於string2<br />[string==string2] string1等於string2<br />[string!=string2] string1不等於string2<br />[string] string 不為空白<br />[-z string] string長度為0<br />[-n string] string長度為n<br />[-l string] string長度</p><p>整形判斷:<br />[int1 -eq int2] int1不等於int2<br />[int1 -ne int2] int1不等於int2<br />[int1 -gt int2] int1大於int2<br />[int1 -ge int2] int1大於等於int2<br />[int1 -lt int2] int1小於int2<br />[int1 -le int2] int1小於等於int2</p><p>邏輯判斷<br />[[pattern1 && pattern2]] p1和p2都為真<br />[[pattern1 || pattern2]] p1或p2為真<br />[[!pattern]] p不匹配</p><p>-b 特定塊檔案<br />-c 特定字元檔案<br />-d 目錄存在<br />-e 檔案存在<br />-f 非目錄普通檔案存在<br />-G 檔案存在並屬於一個特定的Gid<br />-g 設定Set-group-id<br />-k 拈貼位設定<br />-L 檔案是一個符號聯結<br />-p 檔案是一個管道<br />-O 檔案存在並屬於一個有效果的uid<br />-r 檔案可讀<br />-S 檔案是一個通訊端<br />-s 檔案size非零<br />-t fd已在終端開啟<br />-u 設定Set-user-id<br />-w 檔案可寫<br />-x 檔案可執行</p><p>#!/bin/bash<br />file=./test</p><p>if [ -d $file ]<br />then<br /> echo "$file is a directory"<br />elif [ -f $file ]<br />then<br /> if [ -r $file -a -w $file -a -x $file]<br /> then<br /> echo "you have read,write,and execte<br /> permission on $file"<br /> fi<br />else<br /> echo "$file is neither afile nor a directory."<br />fi</p><p>#===========================================<br />null命令,用:表示。是一個內建的什麼都不做的命令。</p><p>#===========================================<br />case命令:<br />case variable in<br />value1)<br /> command(s)<br /> ;;<br />value2)<br /> command(s)<br /> ;;<br />*)<br />command(s)<br /> ;;<br />esac</p><p>#!/bin/bash<br />echo -n "Choose a foreground color for your xterm window:"<br />read color<br />case "$color" in<br />[Bb]l??)<br /> xterm -fg blue -fn terminal &<br /> ;;<br />[Gg]ree*)<br /> xterm -fg darkgreen -fn terminal &<br /> ;;<br />red | orange)<br /> xterm -fg "$color" -fn terminal &<br /> ;;<br />*)<br /> xtern -fn terminal<br /> ;;<br />esac</p><p>echo "Out of case cammnad"<br />#===========================================<br />for 命令:<br />for variable in word_list<br />do<br /> command(s)<br />done</p><p>#!/bin/bash<br />for pal in Tom Dick Harry Joe<br />do<br /> echo "HGi $pal"<br />done<br />echo "Out of loop"</p><p>---------------<br />#cat sayit<br />tom<br />patty<br />ann<br />jake</p><p>#!/bin/bash<br />for person in $(cat sayit)<br />do<br /> mail $person < letter<br /> echo $person was sent a letter.<br />done<br />echo "The letter has been sent."</p><p>for name in $*<br />do<br /> echo $name<br />done<br />#===========================================<br />while命令:<br />while command<br />do<br /> command(s)<br />done<br />#!/bin/bash<br />num=0<br />while(($num <10))<br />do<br /> echo -n "$num"<br /> let num+=1<br />done<br />echo -e " After loop exits,continue running here"</p><p>------------------<br />echo "Who was the 2nd U.S. president to be impeached?"<br />read answer<br />while [[ "$answer" !="Bill Clinton" ]]<br />do<br /> echo "Wrong try again!"<br /> read answer<br />done<br />echo you got it!</p><p>#===========================================<br />util 命令<br />#!/bin/bash<br />util who |grep linda<br />do<br /> sleep 5<br />done<br />talk linda@dragonwings</p><p>-----------------<br />cat hour<br />!/bin/bash<br />let hour=0<br />until ((hour>24)) or [ $hour -gt 24 ]<br />do<br /> case "$hour" in<br /> [0-9]|[0-1])<br /> echo "Good morning!"<br /> ;;<br /> 12)<br /> echo "Lunch time."<br /> ;;<br /> 1[3-7]<br /> echo "Siesta time."<br /> ;;<br /> *)<br /> echo "Good night"<br /> ;;<br /> esac<br /> let hour+=1<br />done<br />#===========================================<br />select 菜單<br />#!/bin/bash<br />ps3="Select a program to execute: "<br />select program in 'ls -F' pwd date<br />do<br /> $program<br />done</p><p>--------------<br />1) ls -F<br />2) pwd<br />3) date<br />#? 1<br />p315.sh*<br />#? 2<br />/home/luser623/shell<br />#? 3<br />Thu Nov 10 22:12:26 CST 2005<br />#? 2<br />/home/luser623/shell<br />#? 3<br />Thu Nov 10 22:12:29 CST 2005</p><p>--------------------------<br />#!/bin/bash<br />ps3="select choose one of the three boys :"<br />select choice in tom dan guy<br />do<br /> case $choice in<br /> tom)<br /> echo Tom is a cool dude!<br /> break<br /> ;;<br /> dan|guy)<br /> echo dan an guy are both wonderful.<br /> break<br /> ;;<br /> *)<br /> echo "$choice is not one of your choices" 1>&2<br /> echo "Try again."<br /> ;;<br /> esac<br />done</p><p>#===========================================<br />shift命令:<br />shift [n],把位置參數向左移n位,當沒有表明n時,n=1<br />#!/bin/bash<br />set joe marry tom sam</p><p>shift #now s* 只剩下marry tom sam,joe已經刪除<br />set $(date) #Thu Mar 16 10:02 PST 2000<br />shift 5<br />echo %* #只剩下2000<br />shift 2<br />shift:shift count must b <=$# #出錯資訊</p><p>-------------<br />#!/bin/bash<br />while(($#>0))<br />do<br /> echo $*<br /> shift<br />done<br />[luser623@LBaseE1 shell]$ ./p318.sh 1 2 3 4 5 6 7 8 9<br />1 2 3 4 5 6 7 8 9<br />2 3 4 5 6 7 8 9<br />3 4 5 6 7 8 9<br />4 5 6 7 8 9<br />5 6 7 8 9<br />6 7 8 9<br />7 8 9<br />8 9<br />9<br />%2<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.