SHELL第一課學習筆記

來源:互聯網
上載者:User

標籤:necessary   change   解譯器   作業系統   shell   linux   

SHELL第一課學習筆記

  1. 什麼叫Shell

    shell是一個命令直譯器,它在作業系統最外層,負責直接與使用者對話,把使用者輸入的命令解釋給

作業系統並處理各種各樣的操作的輸出結果,輸出到螢幕返回使用者(互動式或者非互動式)。


案例1.簡單清除/var/log下的messages日誌指令碼:

#!bin/bashcd /var/logcat /dev/null > messagesecho "Logs cleaned up"

案例2.包含變數、命令、流程式控制制語句清除/var/log下的messages日誌簡單指令碼

#!bin/bashLOG_dir=/var/logROOT_uid=0if [ $UID -ne $ROOT_uid ]echo "You must be root to runing this csript" exit 1ficd $LOG_dir || { echo "Cannot change  to necessary directory"  ##||前面命令不成功再實行中括弧的命令  exit 1       }cat /dev/null > messages && echo "Log cleaned up"   ##&& 前面命令成功才實行後面命令exit 0

2.執行指令碼有三種方式

a.bash script-name或sh script-name

b.path/script-name或./script-name(當前路徑實行)

c.source script-name 或. script-name

第c種分法通常使用source或"."讀入或者載入指定的shell指令檔,然後執行指定shell指令碼中的所有的語句。這些語句將作用當前父shell指令碼進程一部分運行。因此,使用soucrce或者“.”點號可以將指令碼自身指令碼中的變數的值或函數等的傳回值傳遞到當前的父shell指令碼中使用。這是第三種方式最大的區別。

    source或“.”點號命令的功能是當前shell中執行source或“.”點號載入並執行的相關指令檔中的命令及語句,而不是產生一個子shell來執行命令檔案中的命令。

在PHP開發中include相當source或點號。

案例說明如下:

[[email protected] jiaoben]# cat >tesh.shecho "I am xiaoping"^C[[email protected] jiaoben]# sh tesh.shI am xiaoping[[email protected] jiaoben]# .tesh.sh #點後面需要空格-bash: .tesh.sh: command not found[[email protected] jiaoben]# ./tesh.sh #沒有執行許可權-bash: ./tesh.sh: Permission denied[[email protected] jiaoben]# source tesh.shI am xiaoping[[email protected] jiaoben]# chmod + tesh.sh[[email protected] jiaoben]# chmod +x tesh.sh[[email protected] jiaoben]# ./tesh.shI am xiaoping[[email protected] jiaoben]# source tesh.shI am xiaoping[[email protected] jiaoben]# ./1.10.sh    break.sh   jiance.sh  prinf.sh   tesh.sh    zifu.sh[[email protected] jiaoben]# ./1.10.sh    break.sh   jiance.sh  prinf.sh   tesh.sh    zifu.sh[[email protected] jiaoben]# echo ‘userdir=`pwd`‘ >testsource.sh[[email protected] jiaoben]# cat testsource.shuserdir=`pwd`[[email protected] jiaoben]# sh testsource.sh  #直接載入執行指令碼[[email protected] jiaoben]# echo $userdir  #並沒有載入到檔案的變數[[email protected] jiaoben]# . testsource.sh[[email protected] jiaoben]# echo $userdir ##沒有變數後面沒有``符號才直接輸出pwd[[email protected] jiaoben]# cat testsource.shuserdir=pwd[[email protected] jiaoben]# pwd/jiaoben[[email protected] jiaoben]# vi shshengri.sh  shif.sh[[email protected] jiaoben]# vi testsource.sh[[email protected] jiaoben]# . testsource.sh  #直接載入執行指令碼[[email protected] jiaoben]# echo $userdir   #可以載入指令碼的變數,輸出螢幕/jiaoben#當前視窗是一個shell,而指令碼testsource.sh又是另外一個shell,當使用sh testsource.sh執行是已經定義變數,是不會定義變數傳遞到當前視窗,也沒有這個功能。用. testsource.sh可以使用定義變數傳遞到當前的shell,就可以輸出螢幕來。如果想載入指令碼中的變數應該是用.或者source.


本文出自 “小k” 部落格,請務必保留此出處http://zyp88.blog.51cto.com/1481591/1606534

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.