VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网络工程 > linux编程 >
  • Shell 编程 基础

 

本篇主要写一些shell脚本的基础知识,编程规范。


第一个shell脚本#


Copy
[root@localhost ~]# vim first.sh

Copy
#!/bin/bash # This is first Shell Script ! cd /boot/ echo "当前所在路径:" pwd echo "以vml为开头的文件信息:" ls -lh vml*

执行脚本#

source#


Copy
[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]#

.#


Copy
[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#


Copy
[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 ~]#

./#


Copy
[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#


Copy
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}' root /bin/bash

Copy
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F":" '{print $1,$7}' root /bin/bash

Copy
[root@localhost ~]# df -hT | grep "/$" | awk '{print $1,$6}' /dev/sda2 21%

Copy
[root@localhost ~]# df -hT | grep "/$" | awk -F" " '{print $1,$6}' /dev/sda2 21%

重定向输出#


Copy
[root@localhost ~]# echo "123" > test.txt [root@localhost ~]# cat test.txt 123

Copy
[root@localhost ~]# echo "456" >> test.txt [root@localhost ~]# cat test.txt 123 456

重定向输入#


Copy
[root@localhost ~]# rm test.txt rm: remove regular filetest.txt’? ^C

Copy
[root@localhost ~]# echo "y" > temp.txt [root@localhost ~]# ls te* temp.txt test.txt

Copy
[root@localhost ~]# rm test.txt < temp.txt rm: remove regular filetest.txt’? [root@localhost ~]# [root@localhost ~]# ls te* temp.txt

错误重定向#

  • 混合输出

Copy
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ &> error.log [root@localhost ~]# cat error.log tar: Removing leading `/' from member names /opt/ /opt/rh/
  • 标准错误输出

Copy
[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

Copy
[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

变量#

定义、查看变量#


Copy
[root@localhost ~]# Product=java [root@localhost ~]# Version=1.8 [root@localhost ~]# echo $Product java [root@localhost ~]# echo $Product $Version java 1.8

大括号#


Copy
[root@localhost ~]# echo $Product1.8 .8 [root@localhost ~]# echo ${Product}1.8 java1.8

引号#


Copy
[root@localhost ~]# echo "java $Version" java 1.8 [root@localhost ~]# echo 'java $Version' java $Version

反撇号#


Copy
[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#


Copy
[root@localhost ~]# read -p "请输入一个整数:" num 请输入一个整数:10 [root@localhost ~]# echo $num 10

变量作用范围#

  • 设置局部变量为全局变量

Copy
[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
  • 直接定义全局变量

Copy
[root@localhost ~]# export num=100 [root@localhost ~]# echo $num 100 [root@localhost ~]# bash [root@localhost ~]# echo $num 100

数值变量的运算#


Copy
[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

Copy
[root@localhost ~]# vim suanshu.sh

Copy
#!/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"

Copy
[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

特殊变量#

环境变量#


Copy
[root@localhost ~]# env

位置变量#


Copy
[root@localhost ~]# vim suanshu.sh

Copy
#!/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"

Copy
[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

预定义变量#


Copy
[root@localhost ~]# vim suanshu.sh

Copy
#!/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 "执行的参数内容:$*"

Copy
[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
相关教程