variable-name = value
[root@Gwan ~]# LOCALTEST="test"
[root@Gwan ~]# echo ${LOCALTEST}
test
[root@Gwan ~]# echo $LOCALTEST
test
[root@Gwan ~]# set ##显示所有本地变量,此时能找到LOCALTEST
[root@Gwan ~]# exit
[root@Gwan ~]# set ##看不到LOCALTEST
readonly variable-name:创建符号常量的好方法!
hours_per_day = 24 seconds_per_hour = 3600 days_per_week = 7 赋值
readyonly hours_per_day seconds_per_hour days_per_week 设置为只读模式
[root@Gwan ~]# LOCALTEST="test"
[root@Gwan ~]# echo $LOCALTEST
test
[root@Gwan ~]# readonly LOCALTEST
[root@Gwan ~]# LOCALTEST="wade tao"
-bash: LOCALTEST: readonly variable
[root@Gwan ~]# readonly ##查看当前所有的只读变量
declare -ar BASH_VERSINFO='([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")'
declare -ir EUID="0"
declare -r LOCALTEST="test"
declare -ir PPID="6981"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="0"
环境变量:用于所有用户进程(又称子进程),登录进程成为父进程。
Shell中执行的用户进程均成为子进程。即可运行在父进程和子进程
用export设置或显示/打印(export -p)环境变量;readonly则使得变量不得修改。
[root@Gwan ~]# export CHINAWADE="wade"
[root@Gwan ~]# env | grep CHINA
CHINAWADE=wade
[root@Gwan ~]# readonly CHINAWADE
显示用户定义错误信息value
[root@Gwan ~]# echo ${testvar1:?"ERROR, no value defined"}
-bash: testvar1: ERROR, no value defined
【NOTE】从程序的环境中删除变量,要用env命令,env也可临时地改变环境变量的值。
env –i PATH=$PATH HOME=$HOME LC_ALL=C awk ‘…’ file1 file2
i = initialize 忽略继承的环境,仅使用命令行上所给定的变量与值。
【NOTE】打印时,env不会为环境变量加上引号,以供从新输入到shell中,export –p可显示此功能。
替换运算符
3. ${变量:-word} 如果变量存在且非空,则返回其值;否则返回word
用于:如果变量未定义,则返回默认值即word
${变量:+word} 如果变量存在且非空,则返回word;否则返回null
同于:测试变量是否存在
${变量:=word} 如果变量存在且非空,则返回其值;否则,设置它为word,并返回其值
用于:如果变量非订立,则设置变量为默认值,且还返回!
${变量:?message} 如果变量存在且非空,则返回其值;否则,显示变量:message,并退出当前的命令或脚本。省略message会出现默认信息:parameter null or not set。
用于:不足忧变量未定义所导致的错误。
[root@Gwan ~]# testvar = “this is a test”
[root@Gwan ~]# echo ${testvar1:-chinawade}
Chinawade #-相当于not
[root@Gwan ~]# echo ${testvar:+chinawade}
chinawade
[root@Gwan ~]# echo ${testvar:-chinawade}
this is a test
[root@Gwan ~]# echo ${testvar:=chinawade}
this is a test
[root@Gwan ~]# echo ${testvar2:="chinawade"}
chinawade
[root@Gwan ~]# echo $testvar2
chinawade
变量清除
Unset :从执行中的shell中删除变量(unset -v)与函数(unset -f)
[root@Gwan ~]# echo $testvar
this is a test
[root@Gwan ~]# unset testvar
[root@Gwan ~]# echo $testvar
[root@Gwan ~]# echo $testvar2
chinawade
[root@Gwan ~]# readonly testvar2
[root@Gwan ~]# unset testvar2
-bash: unset: testvar2: cannot unset: readonly variable
##readonly要谨慎使用。
位置变量(位置参数positional parameter):$0 $1 ...... $9 ${10}
Step1: 创建脚本parm,并给其执行权限(r4w2x1)
[root@Gwan ~]# ll | grep par
-rw-r--r-- 1 774 root 591 Aug 3 23:47 parm
[root@Gwan ~]# chmod 777 parm
[root@Gwan ~]# ll | grep par
-rwxrwxrwx 1 774 root 591 Aug 3 23:47 parm
[root@Gwan ~]# cat parm
#!/bin/bash
#parm
echo "This is the name of the script. $0"
echo "This is the 1st location parameter of the shell: $1"
echo "This is the 2nd location parameter of the shell: $2"
echo "This is the 3rd location parameter of the shell: $3"
echo "This is the 4th location parameter of the shell: $4"
echo "This is the 5th location parameter of the shell: $5"
echo "This is the 6th location parameter of the shell: $6"
echo "This is the 7th location parameter of the shell: $7"
echo "This is the 8th location parameter of the shell: $8"
echo "This is the 9th location parameter of the shell: $9"
Step2: 执行脚本,赋值。
[root@Gwan ~]# ./parm A B C D E F
This is the name of the script. ./parm
This is the 1st location parameter of the shell: A
This is the 2nd location parameter of the shell: B
This is the 3rd location parameter of the shell: C
This is the 4th location parameter of the shell: D
This is the 5th location parameter of the shell: E
This is the 6th location parameter of the shell: F
This is the 7th location parameter of the shell:
This is the 8th location parameter of the shell:
This is the 9th location parameter of the shell:
Step3: 向系统命令传递参数
[root@Gwan ~]# ll | grep parm.sh
-rw-r--r-- 1 root root 48 Aug 4 00:14 parm.sh
[root@Gwan ~]# cat parm.sh
#!/bin/bash
#parm.sh
find /root -name $1 -print
[root@Gwan ~]# chmod 755 parm.sh
[root@Gwan ~]# ll | grep parm.sh
-rwxr-xr-x 1 root root 48 Aug 4 00:14 parm.sh
[root@Gwan ~]# ./parm.sh myfile
/root/myfile
[root@Gwan ~]# ./parm.sh parm
/root/parm
标准变量
bash默认建立了一些标准环境变量,可在/etc/profile中定义
EXINIT 定义vi
HOME 根(登录)目录
[root@Gwan ~]# echo $HOME
/root
IFS 分隔符,缺省是空,显示是:
[root@Gwan ~]# echo $IFS
[root@Gwan ~]# IFS=':'
[root@Gwan ~]# echo $PATH
/usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /usr/X11R6/bin /root/bin
[root@Gwan ~]# IFS=''
[root@Gwan ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
LOGNAME
[root@Gwan ~]# echo $LOGNAME
root
[root@Gwan ~]# set | grep "LOG"
LOGNAME=root
MAIL ##邮箱放在哪里
MAILCHECK ##每个多少S检查有邮件进来
[root@Gwan ~]# set | grep "MAIL"
MAIL=/var/spool/mail/root
MAILCHECK=60
[root@Gwan ~]# echo $MAIL
/var/spool/mail/root
MAILPATH ##当有多个邮箱的时候使用
TERM ##总段字符类型
[root@Gwan ~]# set | grep "TERM"
TERM=vt100
PATH ##可执行文件去寻找的路径
[root@Gwan ~]# set | grep "PATH"
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
TZ ## TIME ZONE
PS1 ## 提示符
[root@Gwan ~]# set | grep "PS1"
PS1='[\u@\h \W]\$ ' u-root h-host w-working directory
PS2 ##一行上面运行多个命令时
PWD ## present working directory
[root@Gwan ~]# set | grep "PWD"
OLDPWD=/home
PWD=/root
特殊变量
[root@Gwan ~]# cat parm
#!/bin/bash
#parm
echo "This is the name of the script. $0"
echo "This is the 1st location parameter of the shell: $1"
echo "This is the 2nd location parameter of the shell: $2"
echo "This is the 3rd location parameter of the shell: $3"
echo "This is the 4th location parameter of the shell: $4"
echo "This is the 5th location parameter of the shell: $5"
echo "This is the 6th location parameter of the shell: $6"
echo "This is the 7th location parameter of the shell: $7"
echo "This is the 8th location parameter of the shell: $8"
echo "This is the 9th location parameter of the shell: $9"
echo "Display the number of parmeters:$#"
echo "Display all parameters in the script.:$*"
echo "Display process ID:$$"
echo "Display the exit state of the former CMD:$?"
[root@Gwan ~]# ./parm A CHINA CHINAWADE GWAN
This is the name of the script. ./parm
This is the 1st location parameter of the shell: A
This is the 2nd location parameter of the shell: CHINA
This is the 3rd location parameter of the shell: CHINAWADE
This is the 4th location parameter of the shell: GWAN
This is the 5th location parameter of the shell:
This is the 6th location parameter of the shell:
This is the 7th location parameter of the shell:
This is the 8th location parameter of the shell:
This is the 9th location parameter of the shell:
Display the number of parmeters:4
Display all parameters in the script.:A CHINA CHINAWADE GWAN
Display process ID:10117
Display the exit state of the former CMD:0
1.8影响变量的命令
declare -设置或显示变量
export --创建传给子shell的变量
-p显示所有
declare -x USER="root"
[root@Gwan ~]# export country="USA"
[root@Gwan ~]# export | grep cou
declare -x country="USA"
readonly
[root@Gwan ~]# ./parm A CHINA chinawade gwan
This is the name of the script. ./parm
This is the 1st location parameter of the shell: A
This is the 2nd location parameter of the shell: CHINA
This is the 3rd location parameter of the shell: chinawade
This is the 4th location parameter of the shell: gwan
This is the 5th location parameter of the shell:
This is the 6th location parameter of the shell:
This is the 7th location parameter of the shell:
This is the 8th location parameter of the shell:
This is the 9th location parameter of the shell:
Display the number of parmeters:4
Display all parameters in the script.:A CHINA chinawade gwan
Display process ID:10404
Display the exit state of the former CMD:0
This is the 1st location parameter of the shell: chinawade
This is the 2nd location parameter of the shell: gwan
[root@Gwan ~]# cat parm
#!/bin/bash
#parm
echo "This is the name of the script. $0"
echo "This is the 1st location parameter of the shell: $1"
echo "This is the 2nd location parameter of the shell: $2"
echo "This is the 3rd location parameter of the shell: $3"
echo "This is the 4th location parameter of the shell: $4"
echo "This is the 5th location parameter of the shell: $5"
echo "This is the 6th location parameter of the shell: $6"
echo "This is the 7th location parameter of the shell: $7"
echo "This is the 8th location parameter of the shell: $8"
echo "This is the 9th location parameter of the shell: $9"
echo "Display the number of parmeters:$#"
echo "Display all parameters in the script.:$*"
echo "Display process ID:$$"
echo "Display the exit state of the former CMD:$?"
shift 2
echo "This is the 1st location parameter of the shell: $1"
echo "This is the 2nd location parameter of the shell: $2"
2.2 引号
2.2.1 引用的必要性
变量和替换操作,在脚本中执行变量替换时最容易犯的一个错误就是引用错误。
[root@Gwan ~]# echo ert *
ert 10201_database_linux32.zip anaconda-ks.cfg Desktop firefox-3.6.3.tar.bz2 gnome-libs-devel-1.4.1.2.90-44.1.i386.rpm GwanRPMS helloworld.sh hifile install.log install.log.syslog myfile parm parm.sh wade
[root@Gwan ~]# echo "ert *"
ert *
2.2.2 双引号
使用双引号可引用除$、', \外的任意字符或字符串。
[root@Gwan ~]# echo -e "ert, $SHELL '\n* china`echo wade` "
ert, /bin/bash '
* chinawade
2.2.3 单引号
单引号与双引号类似,不同的是shell会忽略任何引用值,也就是说,如果屏蔽了其特殊含意,会将引号里德所有字符,包括引号都作为一个字符串。
[root@Gwan ~]# echo 'ert, $SHELL * china`echo wade`'
ert, $SHELL * china`echo wade`
2.2.4 反引号
用于设置系统命令的输出到变量。shell将反引号中的内容作为一个系统命令,并执行器内容。
[root@Gwan ~]# echo "*china`echo wade`"
*chinawade
2.2.5 反斜杠
如果一个字符有特殊含意(& * + ^ $ ' " | ?),反斜杠防止shell误解其含义,即屏蔽其特殊含义。
[root@Gwan ~]# echo *
10201_database_linux32.zip anaconda-ks.cfg Desktop firefox-3.6.3.tar.bz2 gnome-libs-devel-1.4.1.2.90-44.1.i386.rpm GwanRPMS helloworld.sh hifile install.log install.log.syslog myfile parm parm.sh wade
You have new mail in /var/spool/mail/root
[root@Gwan ~]# echo \*
*
2.3
运算符
运算符是对计算机发的指令
运算对象
-数字、字符(字面值)
-变量
-表达式
表达式:运算符和运算对象的组合体
2.3.1 运算符类型
按位运算符
~,《,》,&, |, ^
$[] 表示形式告诉shell对方括号中的表达式求值。
[root@Gwan ~]# echo $[2+8]
10
[root@Gwan ~]# echo $[2>>8]
0
[root@Gwan ~]# echo $[2<<4]
32
[root@Gwan ~]# echo $[2^4]
6
[root@Gwan ~]# echo $[~3]
-4
2.3.1
逻辑运算符
[root@Gwan ~]# echo $[2&&2]
1
[root@Gwan ~]# echo $[2&&0]
0
[root@Gwan ~]# echo $[0&&0]
0
[root@Gwan ~]# echo $[1&&0]
0
[root@Gwan ~]# echo $[1||0]
1
[root@Gwan ~]# echo $[0||0]
0
[root@Gwan ~]#
赋值运算符
[root@Gwan ~]# var=65
[root@Gwan ~]# let var+=4
[root@Gwan ~]# echo $var
69
[root@Gwan ~]#
表达式替换
$[]和(())
习惯使用$[],所有shell的求值都是用整数完成,可以接受不同基数的数字
$[ base#n] n表示基数从2到36的任何基数。
[root@Gwan ~]# echo $[10#8 +1 ]
9
运算符的优先级
有张表,多查询
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24463783/viewspace-675577/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24463783/viewspace-675577/