linux 编写脚本示范,Linux-scripts-简单脚本和脚本的执行
9.Scripts
9.1什么是 Shell scripts
shell script 是利用 shell 的功能所寫的一個『程序 (program)』,這個程序是使用純文本文件,將一些 shell 的語法與指令(含外部指令)寫在里面,搭配正規表示法、管線命令與數據流重導向等功能,以達到我們所想要的處理目的。
9.1.1干嘛學習 shell scripts
l自動化管理的重要依據
l追蹤與管理系統的重要工作
l簡單***檢測功能
l連續指令單一化
l簡易的數據處理
9.1.2第一支 script 的撰寫與執行
在 shell script 的撰寫中還需要用到底下的注意事項:
l指令的執行是從上而下、從左而右的分析與執行;
l指令、選項與參數間的多個空白都會被忽略掉;
l空白行也將被忽略掉,并且 [tab] 按鍵所推開的空白同樣視為空格鍵;
l如果讀取到一個 Enter 符號 (CR) ,就嘗試開始執行該行 (或該串) 命令;
l至于如果一行的內容太多,則可以使用『 \[Enter] 』來延伸至下一行;
l『 # 』可做為批注!任何加在 # 后面的資料將全部被視為批注文字而被忽略!
1.撰寫第一支 script
[root@localhost tmp]# vim hello.sh#編寫腳本
#!/bin/bash?????????? #指定執行腳本的shell
# Program:
#?????? This program shows "Hello World!" in your screen.
# History:
# 2015/07/16 VBird First release?????? #? #開頭的為注釋
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH????????? #定義PATH,屬個人習慣
echo -e "Hello World! \a \n"?????? #輸出hello world!
exit 0???????? #設置腳本返回值
[root@localhost tmp]# sh hello.sh#運行腳本
Hello World!
9.1.3撰寫 shell script 的良好習慣建立
建議一定要養成良好的 script 撰寫習慣,在每個 script 的文件頭處記錄好:
nscript 的功能;
nscript 的版本信息;
nscript 的作者與聯絡方式;
nscript 的版權宣告方式;
nscript 的 History (歷史紀錄);
nscript 內較特殊的指令,使用『絕對路徑』的方式來下達;
nscript 運作時需要的環境變量預先宣告與設定。
9.2簡單的 shell script 練習
9.2.1簡單范例
1.對談式腳本:變量內容由用戶決定
[root@localhost tmp]#vim showname.sh
#!/bin/bash
read -p "Please input your first name: " firstname????? # 提示使用者輸入
read -p "Please input your last name:? " lastname?????? # 提示使用者輸入
echo -e "\nYour full name is: ${firstname} ${lastname}" # 結果由屏幕輸出
[root@localhost tmp]# sh? showname.sh
Please input your first name:liu
Please input your last name:? hua
Your full name is: liu hua
2.隨日期變化:利用 date 進行檔案的建立
[root@localhost tmp]#? vim 1.sh
#!/bin/bash
echo -e "I will use 'touch' command to create 3 files." # 純粹顯示信息
read -p "Please input your filename: " filename???????? # 提示使用者輸入
date1=$(date --date='2 days ago' +%Y%m%d)? # 前兩天的日期
date2=$(date --date='1 days ago' +%Y%m%d)? # 前一天的日期
date3=$(date +%Y%m%d)????????????????????? # 今天的日期
file1=${filename}${date1}????????????????? # 底下三行在配置文件名
file2=${filename}${date2}
file3=${filename}${date3}
touch "${file1}"?????????????????????????? # 底下三行在建立檔案
touch "${file2}"
touch "${file3}"
[root@localhost tmp]# sh 1.sh
I will use 'touch' command to create 3 files.
Please input your filename: file
[root@localhost tmp]# ls
1.sh? file20200425? file20200426? file20200427? hello.sh
3.數值運算:簡單的加減乘除
[root@localhost tmp]# vim 1.sh
#!/bin/bash
echo -e "You SHOULD input 2 numbers, I will multiplying them! \n"
read -p "first number:? " firstnu
read -p "second number: " secnu
total=$((${firstnu}*${secnu}))
echo -e "\nThe result of ${firstnu} x ${secnu} is ==> ${total}"
[root@localhost tmp]# sh 1.sh
You SHOULD input 2 numbers, I will multiplying them!
first number:? 2
second number: 3
The result of 2 x 3 is ==> 6
9.2.2 script的執行方式差異 (source, sh script, ./script)
[root@localhost tmp]# cat showname.sh
#!/bin/bash
read -p "Please input your first name: " firstname
read -p "Please input your last name:? " lastname
echo -e "Your full name is: ${firstname} ${lastname}"
1.相對路徑執行
[root@localhost tmp]# chmod a+x showname.sh? #先增加執行權限
[root@localhost tmp]# ./showname.sh????#相對路徑執行
Please input your first name: liu
Please input your last name:? hua
Your full name is: liu hua
[root@localhost tmp]# echo $firstname $lastname
#父進程中沒有這兩個變量
2.絕對路徑執行(也需要執行權限)
[root@localhost tmp]# /tmp/showname.sh
Please input your first name: liu
Please input your last name:? hua
Your full name is: liu hua
[root@localhost tmp]# echo $firstname $lastname
#父進程中沒有這兩個變量
#注:
l相對路徑和絕對路徑執行需要文件的執行權限
l腳本在子進程中執行,所以,父進程中沒有$firstname $lastname兩個變量
l直接執行showname.sh,可能會失敗,因為當前目錄可能不在PATH中
3.sh執行
[root@localhost tmp]# sh showname.sh
Please input your first name: liu
Please input your last name:? hua
Your full name is: liu hua
[root@localhost tmp]# echo $firstname $lastname
#腳本在子進程中執行,所以,父進程中沒有$firstname $lastname兩個變量
#sh執行不需要文件的執行權限
4.source和.執行
[root@localhost tmp]# . showname.sh
Please input your first name: liu
Please input your last name:? hua
Your full name is: liu hua
[root@localhost tmp]# echo $firstname $lastname
liu hua
[root@localhost tmp]# source showname.sh
Please input your first name: liu
Please input your last name:? hua
Your full name is: liu hua
[root@localhost tmp]# echo $firstname $lastname
liu hua
#腳本在父進程中執行,所以,有$firstname $lastname兩個變量
#不需要文件的執行權限
總結
以上是生活随笔為你收集整理的linux 编写脚本示范,Linux-scripts-简单脚本和脚本的执行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux脚本多线程,Shell多线程操
- 下一篇: linux命令clock,简单了解Lin