1 / 32

BASH SHELL & SHELL SCRIPTS

BASH SHELL & SHELL SCRIPTS. Chien-Chih Huang , Department of Mathematics, National Taiwan University 2011/07/07. Outline. BASH Shell What is BASH shell Bash shell 的功能 Shell 的 變數 資料 流重導向 (Redirection ) 管線命令 (pipe ) 萬用字元與特殊符號 Regular Expressions & Sed Shell Scripts References.

keegan
Télécharger la présentation

BASH SHELL & SHELL SCRIPTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BASH SHELL & SHELL SCRIPTS Chien-Chih Huang, Department of Mathematics, National Taiwan University2011/07/07

  2. Outline • BASH Shell • What is BASH shell • Bash shell 的功能 • Shell 的變數 • 資料流重導向 (Redirection) • 管線命令 (pipe) • 萬用字元與特殊符號 • Regular Expressions & Sed • Shell Scripts • References BASH SHELL & SHELL SCRIPTS

  3. BASH Shell BASH SHELL & SHELL SCRIPTS

  4. What is BASH shell • Bash is a Unix shell for the GNU Project. (For Linux, the Bash is the default) • Shell is the interface between end user and the Linux system, similar to the commands in Windows • For example, • $ ls –al | more #(better format of listing directory) • Bash is installed as in /bin/sh BASH SHELL & SHELL SCRIPTS

  5. Bash shell 的功能 • 命令編修能力: (上下鍵)(history) • 命令與檔案補全功能: ([tab] 按鍵) • 命令別名設定功能: (alias) • $ alias ll='ls-l' • 工作控制、前景背景控制: • job control: &, [ctrl]-z, jobs, fg, bg, kill • Shell Scripts • Wildcard BASH SHELL & SHELL SCRIPTS

  6. Shell 的變數 • 變數的取用: echo • $ echo $PATH • $ echo {$PATH} • 變數的設定規則 • 環境變數的功能 • env、set • 變數鍵盤讀取、陣列與宣告 • read、 array 、declare • 變數內容的刪除、取代與替換 BASH SHELL & SHELL SCRIPTS

  7. Shell 的變數 • 變數的設定規則 • = 與空白 • $ myname=CC • $ myname= CC ╳ • $myname=CC Huang ╳ • $ 2myname=CC ╳ • “雙引號” and ‘單引號’ • $ var=“langis $LANG” $ var=‘langis $LANG’ • $ echo $var $ echo $var • $ langis en_US $ lang is $LANG • 跳脫字元『 \ 』 • $(command) • $ version=$(uname -r) • $ echo $version • $ 2.6.18-128.el5 • "$變數名稱" 或 ${變數} 累加內容: $ PATH="$PATH":/home/bin • 以 export 來使變數變成環境變數:$ export PATH • 使用 unset取消變數: $ unset myname BASH SHELL & SHELL SCRIPTS

  8. Shell 的變數 • 環境變數的功能 • 用 env觀察環境變數與常見環境變數說明 • $HOME home directory • $PATH path • $PS1提示符號 • $$process id of the script • $#number of input parameters • $0name of the script file • $IFS separation character (white space) • 用 set 觀察所有變數 (含環境變數與自訂變數) • export: 自訂變數轉成環境變數 BASH SHELL & SHELL SCRIPTS

  9. Shell 的變數 • 變數鍵盤讀取、陣列與宣告 • read [-pt] variable • -p :後面可以接提示字元! • -t :後面可以接等待的『秒數!』這個比較有趣~不會一直等待使用者啦! • declare [-aixr] variable • -a :將後面名為 variable 的變數定義成為陣列 (array) 類型 • -i :將後面名為 variable 的變數定義成為整數數字 (integer) 類型 • -x :用法與 export 一樣,就是將後面的 variable 變成環境變數; • -r :將變數設定成為 readonly類型,該變數不可被更改內容,也不能 unset • array: • $ var[1]=“small min” • $ var[2]=“big min” • $var[3]=“nice min” • $ echo “${var[1]}, ${var[2]}, ${var[3]}” • small min, big min, nice min BASH SHELL & SHELL SCRIPTS

  10. Shell 的變數 • 變數內容的刪除、取代與替換 • Example: $ foo=/usr/bin/X11/startx $ echo ${foo#*/} usr/bin/X11/startx $ echo ${foo##*/} startx $ bar=/usr/local/etc/local/networks $ echo ${bar%local*} /usr/local/etc/ $ echo ${bar%%local*} /usr/ BASH SHELL & SHELL SCRIPTS

  11. 資料流重導向 (Redirection) • < 與 << 和 > 與 >> • $ ls –l > log_ls • $ cat > catfile < ~/.bashrc • $ cat > catfile << “eof” • > This is a test. • > eof<==輸入這關鍵字,立刻就結束而不需要輸入 [ctrl]+d • 命令執行的判斷依據: ; , &&, || BASH SHELL & SHELL SCRIPTS

  12. 管線命令 (pipe): | • 擷取命令: cut, grep • 排序命令: sort, wc, uniq • 雙向重導向: tee • 字元轉換命令: tr, col, join, paste, expand • 分割命令: split • 參數代換: xargs $ more .bashrc | grep alias $ more .bashrc | grepalias | grepls BASH SHELL & SHELL SCRIPTS

  13. 萬用字元與特殊符號 BASH SHELL & SHELL SCRIPTS

  14. BASH SHELL & SHELL SCRIPTS

  15. Regular Expressions & Sed BASH SHELL & SHELL SCRIPTS

  16. Regular Expressions BASH SHELL & SHELL SCRIPTS

  17. BASH SHELL & SHELL SCRIPTS

  18. Sed • sed [-nefr] [動作]: • 選項與參數 • -n :使用安靜(silent)模式。在一般 sed的用法中,所有來自 STDIN 的資料一般都會被列出到螢幕上。但如果加上 -n 參數後,則只有經過 sed特殊處理的那一行(或者動作)才會被列出來。 • -e :直接在指令列模式上進行 sed的動作編輯; • -f :直接將 sed的動作寫在一個檔案內, -f filename 則可以執行 filename 內的 sed動作; • -r :sed的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法) -i :直接修改讀取的檔案內容,而不是由螢幕輸出。 • 動作說明: [n1[,n2]]function n1, n2 :不見得會存在,一般代表『選擇進行動作的行數』,舉例來說,如果我的動作 是需要在 10 到 20 行之間進行的,則『 10,20[動作行為] 』 • function : • a :新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~ • c :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行! • d :刪除,因為是刪除啊,所以 d 後面通常不接任何咚咚; • i :插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行); • p :列印,亦即將某個選擇的資料印出。通常 p 會與參數 sed -n 一起運作~ • s:取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配 正規表示法!例如 1,20s/old/new/g 就是啦! BASH SHELL & SHELL SCRIPTS

  19. Sed • $ nl/etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown • $ nl/etc/passwd | sed '2a drink tea' 1root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin drink tea 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin BASH SHELL & SHELL SCRIPTS

  20. Sed • $ nl/etc/passwd | sed '2,5c No 2-5 number‘ 1 root:x:0:0:root:/root:/bin/bash No 2-5 number 6 sync:x:5:0:sync:/sbin:/bin/sync • $ nl/etc/passwd | sed -n '5,7p' 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown • sed 's/要被取代的字串/新的字串/g' • $ sed-i 's/\.$/\!/g' regular_express.txt BASH SHELL & SHELL SCRIPTS

  21. Shell Scripts BASH SHELL & SHELL SCRIPTS

  22. 第一支 script #!/bin/bash # sh01.sh # Program: # This program shows "Hello World!!" in your screen. echo -e "Hello World!! \n" exit 0 $ vi sh01.sh $ sh sh01.sh Hello World!! $ BASH SHELL & SHELL SCRIPTS

  23. Shell script 的預設變數($0,$1...) • $ scriptname.sh opt1 opt2 opt3 opt4 $0 $1 $2 $3 $4 • $#:代表後接的參數『個數』,以上表為例這裡顯示為『 4 』; • $@:代表『 "$1" "$2" "$3" "$4" 』之意,每個變數是獨立的(用雙引號括起來); • $*:代表『 "$1c$2c$3c$4" 』,其中 c 為分隔字元,預設為空白鍵, 所以本例中代表『 "$1 $2 $3 $4" 』之意。 $ sh sh03.sh 11 aa bb cc sh03.sh 11 aa bb cc 4 11 aa bb cc 11 aa bb cc #sh03.sh echo $0 echo $1 echo $2 echo $3 echo $4 echo $# echo $@ echo $* exit 0 BASH SHELL & SHELL SCRIPTS

  24. Condition • test or ‘[ ]’ if test –f fred.c then ... fi • if [ -f fred.c ] • then • ... • fi • if [ -f fred.c ];then • ... • fi • -d file if directory • -e file if exist • -f file if file • -g file if set-group-id • -r file if readable • -s file if size >0 • -u file if set-user-id • -w file if writable • -x file if executable • expression1 –eq expression2 • expression1 –ne expression2 • expression1 –gt expression2 • expression1 –ge expression2 • expression1 -lt expression2 • expression1 –le expression2 • !expression • string1 == string2 • string1 != string2 • -n string (if not empty string) • -z string(if empty string) BASH SHELL & SHELL SCRIPTS

  25. Control Structure Syntax if condition then statement else statement fi #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday if [ $timeofday== “yes” ]; then echo “Good morning” else echo “Good afternoon” fi exit 0 Is it morning? Please answer yes or no yes Good morning BASH SHELL & SHELL SCRIPTS

  26. Condition Structure #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday if [ $timeofday = “yes” ]; then echo “Good morning” elif [ $timeofday = “no” ]; then echo “Good afternoon” else echo “Sorry, $timeofday not recongnized. Enter yes or no” exit 1 fi exit 0 BASH SHELL & SHELL SCRIPTS

  27. Loop Structure #!/bin/sh for foo in bar fud 43 do echo $foo done exit 0 bar fud 43 Syntax for variable do statement done BASH SHELL & SHELL SCRIPTS

  28. Loop Structure Syntax while condition do statement done #!/bin/sh for foo in 1 2 3 4 5 6 7 8 9 10 do echo “here we go again” done exit 0 #!/bin/sh foo = 1 while [ “$foo” –le 10 ] do echo “here we go again” foo = $(($foo+1)) done exit 0 • Syntax • until condition • do • statement • done BASH SHELL & SHELL SCRIPTS

  29. Case Statement Syntax case variable in\ pattern [ | pattern ] …) statement;; pattern [ | pattern ] …) statement;; … esac #!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes) echo “Good Morning”;; y) echo “Good Morning”;; no) echo “Good Afternoon”;; n) echo “Good Afternoon”;; * ) echo “Sorry, answer not recongnized”;; esac exit 0 BASH SHELL & SHELL SCRIPTS

  30. List • AND (&&) statement1 && statement2 && statement3 … • OR (||) statement1 || statement2 || statement3 … BASH SHELL & SHELL SCRIPTS

  31. Run.sh #!/bin/bash # --- user define var_k_all="3.5 3.6 3.7 3.8 3.9 4.0" x0="0.3242342342" outfile="of" # --- define script variables PARA_SRC="para_src" PARA_FILE="par_file" SED_FILE="./sedfile" ii=0 for var_k in $var_k_all; do ii=$(($ii+1)) echo "1s/r/$var_k/g" > $SED_FILE echo "2s/x/$x0/g" >> $SED_FILE echo "3s/filename/$outfile$ii/g" >> $SED_FILE sed -f $SED_FILE $PARA_SRC > $PARA_FILE echo "computing... r="$var_k ./logist > log_c matlab -nodisplay <plogist.m>> log_m /bin/rm $SED_FILE done BASH SHELL & SHELL SCRIPTS

  32. Reference • 鳥哥的 Linux 私房菜認識與學習 BASH http://linux.vbird.org/linux_basic/0320bash.php#bash_bash • 鳥哥的 Linux 私房菜正規表示法與文件格式化處理http://linux.vbird.org/linux_basic/0330regularex.php. • 鳥哥的 Linux 私房菜學習Shell Scripts http://linux.vbird.org/linux_basic/0340bashshell-scripts.php • http://www.ibm.com/developerworks/library/l-bash/ • http://www.ibm.com/developerworks/library/l-bash2.htm • http://www.ibm.com/developerworks/library/l-bash3.html BASH SHELL & SHELL SCRIPTS

More Related