實驗六 編輯Linux腳本程序
實驗目的
1.熟悉簡單的Linux腳本程序的結構與原理
2.掌握編寫腳本程序的方法與步驟
3.學會編寫并運行簡單腳本程序
實驗環境
ubuntu 14.04 LTS
實驗內容
1.編寫簡單的腳本程序
a.用Vim命令新建名為"test.sh"的腳本程序文件
6003@go:~$ vim test.sh
b.編輯程序并保存退出
如下為顯示"Hello World"的程序的編寫
c.為程序添加用戶權限
6003@go:~$ chmod 755 test.sh
d.運行測試鎖邊機的腳本程序
6003@go:~$ ./test.sh
如下為測試結果
6003@go:~$ vim testsh.sh
6003@go:~$ vim test.sh
6003@go:~$ chmod 755 test.sh
6003@go:~$ ./test.sh
‘Hello World!’
2.一些簡單腳本程序的輸出展示
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.特定輸入的反應輸出命令
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.循環算數命令
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
實驗小結
1.利用Vim編輯器編輯腳本程序時需注意格式要求。
2.編寫Linux腳本程序需要注意添加權限,并能使程序符合系統要求。
3.可以在不同操作端上編寫程序命令,并在系統源文件下運行命令。