Linux系列-shell學習筆記

來源:互聯網
上載者:User

標籤:shell   文本編輯   helloworld   testing   遍曆   

第一記

1、  簡單的helloworld編寫

Shell輸入下輸入命令:vi helloworld.sh

隨後進入文本編輯:

#!/bin/shell#this is ahelloworld testa=”helloworld”echo $a

執行helloworld.sh檔案

命令:

# sh helloworld.sh

2、變數賦值的方式是

# variable_name = variable_value

如果對一個已經有值的變數賦值,新值將取代舊值。取值的時候要在變數名前加$,$variable_name可以在引號中使用,如果出現混淆的情況,可以使用花括弧來區分,例如:

# echo "Hi, $as"

就不會輸出“Hi, helloworlds”,而是輸出“Hi,”。這是因為Shell把$as當成一個變數,而$as未被賦值,其值為空白。正確的方法是:

$ echo "Hi, ${a}s"

單引號中的變數不會進行變數替換操作。

 

3.who 命令查看有誰登陸系統

 who| wc –l   計算使用者總數

 

4.cat > nusers 建立檔案nusers,使用cat複製終端的輸入

        echo “haha”

        然後 ctrl+d表示end of file

        使用chmod +x nesers 讓檔案擁有執行的許可權

         測試:./nusers

 

         shell識別三種命令:內建命令、shell函數以及外部命令

 

5.printf和echo的區別

printf不像echo那樣能夠自動換行

 

6.重新導向與管道


7.shell命令

# who 查看有誰登陸# date查看當前日期# chmod +x file修改file檔案,讓其成為可執行檔# echo 顯示文本字串內容# cat file顯示file檔案的內容,和more差不多# ./file執行file檔案# set顯示完整的環境變數配置列表# testing=`date `反引號的作用:輸出值;此處吧date值輸出並賦值給testing# $testing$用於輸出變數的值# command > outputfile將command命令的結果寫入到outputfile檔案中(覆蓋式寫入)# command >> outputfile將command命令的結果寫入到outputfile檔案中(追加式寫入)# wc << EOF提示符一直提示輸入資料,知道輸入EOF,然後wc命令開始對內聯輸入重新導向提供的資料執行行,詞和位元組計數。# rpm –qa > rpm.list將已經安裝的軟體包列表資料輸入到rpm.list# sort rpm.list排序rpm.list# rpm –qa | sort通過管道|將兩條命令合成一條# expr 1 + 5expr執行加法運算,結果為6#bc進行浮點數運算命令# $?查看最後一條命令的退出狀態代碼


第二記

1.for命令

格式:
for var in listdocommandsdone

寫一個用for遍曆list的程式,如下:

[[email protected] test]# vi sh1201#!/bin/shfor i in liudiwei haha xiaoli liangliang xuxudo        echo "this is my friend $i"done
測試

[[email protected] test]# ./sh1201this is my friend liudiweithis is my friend hahathis is my friend xiaolithis is my friend liangliangthis is my friend xuxu

如果list裡面有單引號,如I’m a student.

可以使用兩種方式來解決:

(1)      使用逸出字元  I\’m a student

(2)      使用雙引號來定義用到單引號的值。”I’m” a student.

2.while命令

while命令的格式:

while test commanddo other commandsdone

編寫testwhile

[[email protected] test]# vi testwhile#!/bin/shi=0while [ $i -le 10 ]do        echo "i=$i"        i=$[ $i + 1 ]done

測試:

[[email protected] test]# ./testwhilei=0i=1i=2i=3i=4i=5i=6i=7i=8i=9i=10

3.util命令

命令格式

until test commandsdo other commandsdone

說明:直到  commands為真,不然就一直執行other commands.

4.嵌套迴圈輸出九九乘法表

編輯一個新檔案

[[email protected] test]# vi mutil99

九九乘法表代碼:

#/bin/shfor (( i=1;i<=9;i++))do        for ((j=1;j<=i;j++))        do                echo -n "$j*$i=$[$i*$j] "        done        echodone

結果:

[[email protected] test]# ./mutil991*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

5.break和continue

6.處理迴圈輸出

迴圈遍曆檔案夾並判斷是一個目錄還是一個檔案

[[email protected] test]# vi isdirorfile#/bin/shfor file in /root/*do        if [ -d "$file" ]        then                echo "$file is a directory!"        else                echo "$file is a file!"        fidone    > output.txt  #講結果輸出到output.txt檔案裡

結果

[[email protected] test]# more output.txt/root/anaconda-ks.cfg is a file!/root/dead.letter is a file!/root/downloads is a directory!/root/hadoop is a directory!/root/hello.sh is a file!/root/initial-setup-ks.cfg is a file!/root/src is a directory!/root/test is a directory!/root/testq is a file!









Linux系列-shell學習筆記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.