本篇主要寫一些 腳本的基礎知識,編程規範。 第一個shell腳本 執行腳本 source . sh ./ 符號使用 管道符 和 awk 重定向輸出 重定向輸入 [root@localhost ~] rm test.txt error.log [root@localhost ~] cat error. ...
本篇主要寫一些shell
腳本的基礎知識,編程規範。
第一個shell腳本
[root@localhost ~]# vim first.sh
#!/bin/bash
# This is first Shell Script !
cd /boot/
echo "當前所在路徑:"
pwd
echo "以vml為開頭的文件信息:"
ls -lh vml*
執行腳本
source
[root@localhost ~]# source first.sh
當前所在路徑:
/boot
以vml為開頭的文件信息:
-rwxr-xr-x. 1 root root 5.7M Sep 4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]#
.
[root@localhost ~]# . first.sh
當前所在路徑:
/boot
以vml為開頭的文件信息:
-rwxr-xr-x. 1 root root 5.7M Sep 4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]#
sh
[root@localhost ~]# sh first.sh
當前所在路徑:
/boot
以vml為開頭的文件信息:
-rwxr-xr-x. 1 root root 5.7M Sep 4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]#
./
[root@localhost ~]# ./first.sh
bash: ./first.sh: Permission denied
[root@localhost ~]# ls -lh first.sh
-rw-r--r--. 1 root root 139 Sep 23 22:04 first.sh
[root@localhost ~]# chmod +x first.sh
[root@localhost ~]# ls -lh first.sh
-rwxr-xr-x. 1 root root 139 Sep 23 22:04 first.sh
[root@localhost ~]# ./first.sh
當前所在路徑:
/boot
以vml為開頭的文件信息:
-rwxr-xr-x. 1 root root 5.7M Sep 4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23 2017 vmlinuz-3.10.0-693.el7.x86_64
符號使用
管道符 和 awk
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}'
root /bin/bash
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F":" '{print $1,$7}'
root /bin/bash
[root@localhost ~]# df -hT | grep "/$" | awk '{print $1,$6}'
/dev/sda2 21%
[root@localhost ~]# df -hT | grep "/$" | awk -F" " '{print $1,$6}'
/dev/sda2 21%
重定向輸出
[root@localhost ~]# echo "123" > test.txt
[root@localhost ~]# cat test.txt
123
[root@localhost ~]# echo "456" >> test.txt
[root@localhost ~]# cat test.txt
123
456
重定向輸入
[root@localhost ~]# rm test.txt
rm: remove regular file ‘test.txt’? ^C
[root@localhost ~]# echo "y" > temp.txt
[root@localhost ~]# ls te*
temp.txt test.txt
[root@localhost ~]# rm test.txt < temp.txt
rm: remove regular file ‘test.txt’? [root@localhost ~]#
[root@localhost ~]# ls te*
temp.txt
錯誤重定向
- 混合輸出
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ &> error.log
[root@localhost ~]# cat error.log
tar: Removing leading `/' from member names
/opt/
/opt/rh/
- 標準錯誤輸出
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ 2> error.log
/opt/
/opt/rh/
[root@localhost ~]# cat error.log
tar: Removing leading `/' from member names
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ 2>> error.log
/opt/
/opt/rh/
[root@localhost ~]# cat error.log
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
變數
定義、查看變數
[root@localhost ~]# Product=java
[root@localhost ~]# Version=1.8
[root@localhost ~]# echo $Product
java
[root@localhost ~]# echo $Product $Version
java 1.8
大括弧
[root@localhost ~]# echo $Product1.8
.8
[root@localhost ~]# echo ${Product}1.8
java1.8
引號
[root@localhost ~]# echo "java $Version"
java 1.8
[root@localhost ~]# echo 'java $Version'
java $Version
反撇號
[root@localhost ~]# which cat
/usr/bin/cat
[root@localhost ~]# ls -lh /usr/bin/cat
-rwxr-xr-x. 1 root root 53K Nov 6 2016 /usr/bin/cat
[root@localhost ~]# ls -lh `which cat`
-rwxr-xr-x. 1 root root 53K Nov 6 2016 /usr/bin/cat
read
[root@localhost ~]# read -p "請輸入一個整數:" num
請輸入一個整數:10
[root@localhost ~]# echo $num
10
變數作用範圍
- 設置局部變數為全局變數
[root@localhost ~]# echo $Product $Version
java 1.8
[root@localhost ~]# bash
[root@localhost ~]# echo $Product $Version
[root@localhost ~]# exit
[root@localhost ~]# export Product Version
[root@localhost ~]# bash
[root@localhost ~]# echo $Product $Version
java 1.8
- 直接定義全局變數
[root@localhost ~]# export num=100
[root@localhost ~]# echo $num
100
[root@localhost ~]# bash
[root@localhost ~]# echo $num
100
數值變數的運算
[root@localhost ~]# num1=8
[root@localhost ~]# num2=5
[root@localhost ~]# expr $num1 + $num2
13
[root@localhost ~]# expr $num1 - $num2
3
[root@localhost ~]# expr $num1 \* $num2
40
[root@localhost ~]# expr $num1 / $num2
1
[root@localhost ~]# expr $num1 % $num2
3
- Demo
[root@localhost ~]# vim suanshu.sh
#!/bin/bash
read -p "請輸入第1個數字:" num1
read -p "請輸入第2個數字:" num2
result1=`expr $num1 + $num2`
result2=`expr $num1 - $num2`
result3=`expr $num1 \* $num2`
result4=`expr $num1 / $num2`
result5=`expr $num1 % $num2`
echo "加法:$num1 + $num2 = $result1"
echo "減法:$num1 - $num2 = $result2"
echo "乘法:$num1 * $num2 = $result3"
echo "除法:$num1 / $num2 = $result4"
echo "取餘:$num1 % $num2 = $result5"
[root@localhost ~]# chmod +x suanshu.sh
[root@localhost ~]# ./suanshu.sh
請輸入第1個數字:8
請輸入第2個數字:5
加法:8 + 5 = 13
減法:8 - 5 = 3
乘法:8 * 5 = 40
除法:8 / 5 = 1
取餘:8 % 5 = 3
特殊變數
環境變數
[root@localhost ~]# env
位置變數
[root@localhost ~]# vim suanshu.sh
#!/bin/bash
echo "第一個位置變數值為:$1"
echo "第二個位置變數值為:$2"
result1=`expr $1 + $2`
result2=`expr $1 - $2`
result3=`expr $1 \* $2`
result4=`expr $1 / $2`
result5=`expr $1 % $2`
echo "加法:$1 + $2 = $result1"
echo "減法:$1 - $2 = $result2"
echo "乘法:$1 * $2 = $result3"
echo "除法:$1 / $2 = $result4"
echo "取餘:$1 % $2 = $result5"
[root@localhost ~]# chmod +x suanshu.sh
[root@localhost ~]# ./suanshu.sh 8 5
第一個位置變數值為:8
第二個位置變數值為:5
加法:8 + 5 = 13
減法:8 - 5 = 3
乘法:8 * 5 = 40
除法:8 / 5 = 1
取餘:8 % 5 = 3
預定義變數
[root@localhost ~]# vim suanshu.sh
#!/bin/bash
echo "第一個位置變數值為:$1"
echo "第二個位置變數值為:$2"
result1=`expr $1 + $2`
result2=`expr $1 - $2`
result3=`expr $1 \* $2`
result4=`expr $1 / $2`
result5=`expr $1 % $2`
echo "加法:$1 + $2 = $result1"
echo "減法:$1 - $2 = $result2"
echo "乘法:$1 * $2 = $result3"
echo "除法:$1 / $2 = $result4"
echo "取餘:$1 % $2 = $result5"
echo "執行的腳本名稱:$0"
echo "執行的參數個數:$#"
echo "執行的參數內容:$*"
[root@localhost ~]# chmod +x suanshu.sh
[root@localhost ~]# ./suanshu.sh 8 5
第一個位置變數值為:8
第二個位置變數值為:5
加法:8 + 5 = 13
減法:8 - 5 = 3
乘法:8 * 5 = 40
除法:8 / 5 = 1
取餘:8 % 5 = 3
執行的腳本名稱:./suanshu.sh
執行的參數個數:2
執行的參數內容:8 5
[root@localhost ~]# echo $?
0