實(shí)驗(yàn)六 編輯Linux腳本程序
實(shí)驗(yàn)?zāi)康?/h2>
1.熟悉簡(jiǎn)單的Linux腳本程序的結(jié)構(gòu)與原理
2.掌握編寫腳本程序的方法與步驟
3.學(xué)會(huì)編寫并運(yùn)行簡(jiǎn)單腳本程序
實(shí)驗(yàn)環(huán)境
ubuntu 14.04 LTS
實(shí)驗(yàn)內(nèi)容
1.編寫簡(jiǎn)單的腳本程序
a.用Vim命令新建名為"test.sh"的腳本程序文件
6003@go:~$ vim test.sh
b.編輯程序并保存退出
如下為顯示"Hello World"的程序的編寫
c.為程序添加用戶權(quán)限
6003@go:~$ chmod 755 test.sh
d.運(yùn)行測(cè)試鎖邊機(jī)的腳本程序
6003@go:~$ ./test.sh
如下為測(cè)試結(jié)果
6003@go:~$ vim testsh.sh
6003@go:~$ vim test.sh
6003@go:~$ chmod 755 test.sh
6003@go:~$ ./test.sh
‘Hello World哩牍!’
2.一些簡(jiǎn)單腳本程序的輸出展示
a.顯示用戶輸入年齡的年齡范圍
6003@go:~$ vim test02.sh
6003@go:~$ chmod 755 test02.sh
6003@go:~$ ./test02.sh
Please enter your age:
21
You are in your 20s
6003@go:~$ ./test02.sh
Please enter your age:
90
Sorry, you are out of the age range.
b.特定輸入的反應(yīng)輸出命令
6003@go:~$ vim test03.sh
6003@go:~$ chmod 755 test03.sh
6003@go:~$ ./test03.sh
Please enter a letter:
A
You have typed a vowel!
6003@go:~$ ./test03.sh
Please enter a letter:
c
You have typed a consonant!
c.循環(huán)算數(shù)命令
6003@go:~$ vim test04.sh
6003@go:~$ chmod 755 test04.sh
6003@go:~$ ./test04.sh
Enter the number
1
The factorial of 1 is 1
6003@go:~$ ./test04.sh
Enter the number
3
The factorial of 3 is 6
6003@go:~$ ./test04.sh
Enter the number
16
The factorial of 16 is 20922789888000
實(shí)驗(yàn)小結(jié)
1.利用Vim編輯器編輯腳本程序時(shí)需注意格式要求萝衩。
2.編寫Linux腳本程序需要注意添加權(quán)限壕曼,并能使程序符合系統(tǒng)要求。
3.可以在不同操作端上編寫程序命令锐锣,并在系統(tǒng)源文件下運(yùn)行命令。