Shell 编程 基础
程序员文章站
2022-08-08 17:23:10
本篇主要写一些 脚本的基础知识,编程规范。 第一个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