bash shell编程的基本概念
本节讲解变量定义使用以及其作用域
1.变量定义使用以及其作用域(和python等语言一样差不多,不过定义变量赋值等号左右不可以有空格)
清空变量(unset):
[root@master xiaosi]# abc = 5
-bash: abc: command not found
[root@master xiaosi]# abc=4
[root@master xiaosi]# echo $abc
4
[root@master xiaosi]# unset abc
[root@master xiaosi]# echo $abc
[root@master xiaosi]#
设置变量单引号和双引号的区别:
[root@master xiaosi]# a=1
[root@master xiaosi]# echo '$a'
$a
[root@master xiaosi]# echo "$a" #双引号里面会把$解析
1
变量的作用域:
- 普通变量
- 全局变量(环境变量)
- 局部变量(local)
- 内置变量(bash里面本身自带的参数变量)
举个例子(两个不一样的结果):
[root@master xiaosi]# vi foo2.sh
#!/bin/sh
echo "$abc"
echo "The work of finding the abc of the value has been finished"
[root@master xiaosi]# bash foo2.sh
The work of finding the abc of the value has been finished
[root@master xiaosi]# . foo2.sh
13
The work of finding the abc of the value has been finished
[root@master xiaosi]# source foo2.sh
13
The work of finding the abc of the value has been finished
全局变量(设置一下,(bash foo2.sh就可输出abc变量的值,因为这是全局变量,刚那个只在当前目录有用,所以(. foo2.sh ,bash foo2.sh能找出abc的值))
注意的是:如果程序体里面的程序执行了对变量的值的修改,用bash执行,不会改变变量的值,如果是用”.“(点)或bash执行,则会对外部改变变量值
[root@master xiaosi]# export abc=14
[root@master xiaosi]# bash foo2.sh
14
The work of finding the abc of the value has been finished
局部变量与全局变量:
[root@master xiaosi]# export abc=14 #定义一个全局变量
[root@master xiaosi]# vi foo2.sh #编辑文本
#!/bin/sh #说明解释器在哪里,#!(特殊符号,和#不同,它不是注释符)
echo "$abc" #先输出abc的值
foo(){ #定义一个foo函数
echo "the values of abc is $abc " #先输出当前的值
echo "it has generated a local abc=15 in the text anmed foo2.sh "
local abc=15 #定义局部变量
echo "the values of abc has change and now the values of the abc is $abc " #输出此时的abc变量此时abc被局部改为15
}
foo #执行foo函数
echo "the abc in the local has not found and last abc is $abc " #执行完foo()函数后的,,局部的abc定义变量资源回收,abc拿值是向全局变量哪里取值,此时abc又为14
echo "The work of finding the abc of the value has been finished"
[root@master xiaosi]# bash foo2.sh #执行输出结果,与做解释的一致
14
the values of abc is 14
it has generated a local abc=15 in the text anmed foo2.sh
the values of abc has change and now the values of the abc is 15
the abc in the local has not found and last abc is 14
The work of finding the abc of the value has been finished
bash shell脚本的程序变量:
- 参数位置变量
- $0-$9,${10}-${n}
- 所有参数位置视为一个字符串
- $*
- 所有参数位置稍微一个串行多个字符串组成
- $@
- 参数个数
- $#
例子:
- $#
[root@master xiaosi]# vi foo3.sh
#!/bin/sh
echo "\$* = $*" #所有的参数
echo "\$@ = $@" #字符换串行个数
echo "\$# = $#" #统计输入参数个数
echo "\$0 = $0" #输出当前指定的执行的路径
echo "\$1 = $1"
echo "\$2 = $2"
echo "\$3 = $3"
echo "\$4 = $4"
echo "\$5 = $5"
echo "\$6 = $6"
echo "\$7 = $7"
echo "\$8 = $8"
echo "\$9 = $9"
echo "\$10 = $10"
echo "\${11} = ${11}"
echo "\${12} = ${12}"
[root@master xiaosi]# ./foo3.sh 1 2 3 4 5 6 7 8 9 10 11 12
$* = 1 2 3 4 5 6 7 8 9 10 11 12
$@ = 1 2 3 4 5 6 7 8 9 10 11 12
$# = 12
$0 = ./foo3.sh
$1 = 1
$2 = 2
$3 = 3
$4 = 4
$5 = 5
$6 = 6
$7 = 7
$8 = 8
$9 = 9
$10 = 10
${11} = 11
${12} = 12
例子2;
[root@master xiaosi]# vi foo4.sh
#!/bin/sh
echo "\$* = $*"
echo "\$@ = $@"
echo "\$# = $#"
for i in $*;do
echo $i
done
[root@master xiaosi]# ./foo3.sh 1 2 3 4 5 6 7 8 9 10 11 12
$* = 1 2 3 4 5 6 7 8 9 10 11 12
$@ = 1 2 3 4 5 6 7 8 9 10 11 12
$# = 12
1
2
3
4
5
6
7
8
9
10
11
12
例子3:
[root@master xiaosi]# vi foo3.sh
#!/bin/sh
echo "\$* = $*"
echo "\$@ = $@"
echo "\$# = $#"
for i in "$@";do #这里取字符串作为一个输入
echo $i
done
[root@master xiaosi]# ./foo3.sh 1 2 3 4 5 6 7 "8 9" 10 11 12
$* = 1 2 3 4 5 6 7 8 9 10 11 12
$@ = 1 2 3 4 5 6 7 8 9 10 11 12
$# = 11
1
2
3
4
5
6
7
8 9
10
11
12
特殊的内置变量:
- 上一条命令执行结束后的返回值
- $? (用来判断上一条命令执行的2状态)
- 目前bash shell的进程号
- $$
- 上一个后台进程的进程号
- $!
例子:
- $!
[root@master xiaosi]# cat /etc/hosts
192.168.71.100 master
192.168.71.101 slave1
192.168.71.102 slave2
[root@master xiaosi]# grep localhost /etc/hosts
[root@master xiaosi]# grep master /etc/hosts
192.168.71.100 master
[root@master xiaosi]# echo $?
0
[root@master xiaosi]# grep localhost /etc/hosts
[root@master xiaosi]# echo $?
1
例子2:
[root@master xiaosi]# echo $$
13621
[root@master xiaosi]# ps -ef | grep 13621
root 13621 13617 0 11:14 pts/2 00:00:00 -bash #bash的进程号
root 13776 13621 0 12:10 pts/2 00:00:00 ps -ef
root 13777 13621 0 12:10 pts/2 00:00:00 grep 13621
例子三:
[root@master xiaosi]# while : ; do sleep 1;done &
[1] 13805
[root@master xiaosi]# echo $1
[root@master xiaosi]# echo $!
$!
[root@master xiaosi]# echo $!
13805
[root@master xiaosi]# jobs
[1]+ Running while :; do
sleep 1;
done &
[root@master xiaosi]# fg
while :; do
sleep 1;