shell script 入門 筆記

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   strong   檔案   

shell script 入門


在 shell script 的撰寫中還需要用到底下的注意事項:

1.  指令的執行是從上而下、從左而右的分析與執行;
2.  指令的執行就如同第五章內提到的: 指令、選項不參數間的多個空白都會被忽略掉;
3.  空白行也將被忽略掉,而且 [tab] 按鍵所推開的空白同樣規為空白格鍵;
4.  如果讀取到一個 Enter 符號 (CR) ,就嘗試開始執行該行 (或該串) 命令;
5.  至亍如果一行的內容太多,則可以使用『 \[Enter] 』來延伸至下一行;
6.  『 # 』可做為批註!任何加在 # 後面的資料將全部被規為批註文字而被忽略!



第一個shell script,必須是hello world。哈哈


#!/bin/bashPATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e  "hello world!  \n"exit 0       



[email protected]:~/shell_script_beginner$ sh ./sh01.sh
hello world!  



特此說明一下,Ubuntu預設的shell是dash,而不是bash,echo -e時候會有問題,會把-e也列印出來。


轉載link:http://webnoties.blog.163.com/blog/static/1835251412013518362635/

很謝謝作者的blog幫我搞定了這個預設shell不是bash的問題

因為ubuntu預設的sh是串連到dash的,又因為dash跟bash的不相容所以出錯了.執行時可以把sh換成bash 檔案名稱.sh來執行.成功.dash是什麼東西,查了一下,應該也是一種shell,貌似使用者對它的詬病頗多.
by the way修改sh預設串連到bash的一種方法:
sudo dpkg-reconfigure dash
選擇no即可.












#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh02.sh# e-mail      : [email protected]# code purpose:#               This program would ask the user to input some varible's value#       and then print them out into the screen.PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input your first name:" first_name #give the user a message whatshould be inputedread -p "Please input your second name" second_nameecho -e "\nYour full name is $first_name $second_name" #print out the user's name                                                                                                                                                                                                                                                                                                                                                                                                                                                               


[email protected]:~/shell_script_beginner$ sh ./sh02.sh
Please input your first name:Jason
Please input your second nameLeaster

Your full name is Jason Leaster



#! /bin/bash# code writer : EOF# code date   : 2014.07.29# code file   : sh04.sh# e-mail      : [email protected]# code purpose:#               This program was coded for demonstrating some#       mathmatical operations on varibles which in bash script#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e "You SHOULD input 2 numbers ,I will cross them !\n"read -p "first number: " first_numread -p "second number: " second_numtotal=$(($first_num*second_num))echo -e "total is : $total"
[email protected]:~/shell_script_beginner$ sh ./sh04.sh
You SHOULD input 2 numbers ,I will cross them !

first number: 10
second number: 25
total is : 250


不怕丟人的說,上面這地方寫成$(total)差點糾結而shi。。。。



#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh05.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for command -- test#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e "Please input a filename, I will check the filename's type and permission.\n\n"read -p "Input a filename : " filenametest -z $filename && echo "You must input a filename." && exit 0test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0test -f $filename && filetype="regulare file"test -d $filename && filetype="directory"test -r $filename && perm="readable"test -w $filename && perm="writable"test -x $filename && perm="excutable"echo "The filename: $filename is a $filetype"echo "And the permissions are: $perm"


[email protected]:~/shell_script_beginner$ sh ./sh05.sh
Please input a filename, I will check the filename‘s type and permission.


Input a filename : sh01.sh
The filename: sh01.sh is a regulare file
And the permissions are: writable



刪除一個環境變數用      unset 環境變數名


 




#! /bin/bash# code writer : EOF# code date   : 2014.07.29# code file   : sh06.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for [ A==B ]#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) : " yn[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0[ "$yn" == "N" -o "$yn" == "n" ] && echo "NO,interrupt" && exit 0echo "I don't know what you choice is" && exit 0


[email protected]:~/shell_script_beginner$ sh ./sh06.sh
Please input (Y/N) : y
OK, continue



#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh07.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo that user input a file name#       and some parameters, lately program process it and print out the file#       type and permission.#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "The script name is ==> $0"echo "Total parameter numbe is ==> $#"[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here" && exit 0echo "Your whole parameter is ==> [email protected]"echo "1st parameter ==> $1"echo "2nd parameter ==> $2"


[email protected]:~/shell_script_beginner$ sh ./sh07.sh
The script name is ==> ./sh07.sh
Total parameter numbe is ==> 0
The number of parameter is less than 2. Stop here
[email protected]:~/shell_script_beginner$ sh ./sh07.sh hello world
The script name is ==> ./sh07.sh
Total parameter numbe is ==> 2
Your whole parameter is ==> hello world
1st parameter ==> hello
2nd parameter ==> world





#! /bin/bash# code writer : EOF# code date   : 2014.07.30 # code file   : sh08.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for command -- shift#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"shiftecho "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"shift 3echo "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"



[email protected]:~/shell_script_beginner$ sh ./sh08.sh
Total parameter number is ==> 0
Your whole parameter is ==>
Total parameter number is ==> 0
Your whole parameter is ==>
Total parameter number is ==> 0
Your whole parameter is ==>
[email protected]:~/shell_script_beginner$ sh ./sh08.sh hello world
Total parameter number is ==> 2
Your whole parameter is ==> hello world
Total parameter number is ==> 1
Your whole parameter is ==> world
Total parameter number is ==> 1
Your whole parameter is ==> world
[email protected]:~/shell_script_beginner$ sh ./sh08.sh hello world jason leaster tonight
Total parameter number is ==> 5
Your whole parameter is ==> hello world jason leaster tonight
Total parameter number is ==> 4
Your whole parameter is ==> world jason leaster tonight
Total parameter number is ==> 1
Your whole parameter is ==> tonight


有意思的是如果檔案名稱之後沒有“參數”,parameter number就是0,如果參數少於shift移動的距離(比方說代碼中的shift 3),就會保留最低數目為1,如果參數少於shift移動的距離






#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh08.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; then fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) :" ynif [ "$yn" == "Y" -o "$yn" == "y" ]; then        echo "OK,continue"        exit 0fiif [ "$yn" == "N" -o "$yn" == "n" ]; then        echo "Oh,interrupt!"        exit 0fiecho "I don't know what your choice is" && exit 0



[email protected]:~/shell_script_beginner$ sh ./sh09.sh
Please input (Y/N) :y
OK,continue
[email protected]:~/shell_script_beginner$ sh ./sh09.sh
Please input (Y/N) :n
Oh,interrupt!







#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh9.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; elif [] ;then else fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) :" ynif [ "$yn" == "Y" -o "$yn" == "y" ]; then        echo "OK,continue"        exit 0elif [ "$yn" == "N" -o "$yn" == "n" ];then        echo "Oh,interrupt!"        exit 0else        echo "Are you kidding me? You don't know what means \"Input (Y/N)\" \n"fiecho "I don't know what your choice is" && exit 0

[email protected]:~/shell_script_beginner$ sh ./sh10.sh
Please input (Y/N) :hehe
Are you kidding me? You don‘t know what means "Input (Y/N)" \n
I don‘t know what your choice is









#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh10.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; elif [] ;then else fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "This program will print your selection !"case $1 in        "one")        echo "Your choice is ONE"        ;;        "two")        echo "Your choice is TWO"        ;;        *)        echo "42"        ;;esac


[email protected]:~/shell_script_beginner$ sh ./sh12.sh hello world
This program will print your selection !
42



#! /bin/bash# code writer : EOF# code date   : 2014.07.30 # code file   : sh13.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "function and while loop"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHfunction secret(){        echo "hello world!"}temp=10while [  $temp != 0 ]do        secret        temp=$(($temp-1))        echo "$temp"done

[email protected]:~/shell_script_beginner$ sh ./sh13.sh
hello world!
9
hello world!
8
hello world!
7
hello world!
6
hello world!
5
hello world!
4
hello world!
3
hello world!
2
hello world!
1
hello world!
0



最後sh -x將使用do啊的script 內容顯示到螢幕上,這是很有用的參數,而-n不會顯示任何資訊

[email protected]:~/shell_script_beginner$ sh -x ./sh13.sh
+ PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:/home/liuzjian/bin
+ export PATH
+ temp=2
+ ‘[‘ 2 ‘!=‘ 0 ‘]‘
+ secret
+ echo ‘hello world!‘
hello world!
+ temp=1
+ echo 1
1
+ ‘[‘ 1 ‘!=‘ 0 ‘]‘
+ secret
+ echo ‘hello world!‘
hello world!
+ temp=0
+ echo 0
0
+ ‘[‘ 0 ‘!=‘ 0 ‘]‘




去衡山的時候,在白龍潭(應該是吧,不記得啦。。。)遇見的兩個小孩。。。多麼美好年紀啊。。。


相關文章

聯繫我們

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