1.变量结合
[root@localhost ~]# ERROR_MSG="Sorry this file doet not exist user $LOGNAME"
[root@localhost ~]# echo ${ERROR_MSG}
Sorry this file doet not exist user root
[root@localhost ~]# echo ${FIRTST}${SURNAME}
Bruce Willis
2.测试变量是否设置
1.
[root@localhost ~]# echo "The sky is ${COLOUR:-grey} today"
The sky is blue today
[root@localhost ~]# unset COLOUR
[root@localhost ~]# echo "The sky is ${COLOUR:-grey} today"
The sky is grey today
[root@localhost ~]#
2.
root@localhost yjg]# ./vartest.txt
what time do you wish to start the payroll [03:00]:
4:21
process to start at 4:21 OK
It is a monthly or weekly run [Weekly]:
SUN
Run type is SUN
Cannot open input file SUN: No such file or directory
[root@localhost yjg]# ./vartest.txt
what time do you wish to start the payroll [03:00]:
process to start at 03:00 OK
It is a monthly or weekly run [Weekly]:
Run type is Weekly
Cannot open input file Weekly: No such file or directory
3.
[root@localhost yjg]# echo "The file is ${FILE:?" Sorry cannot locate the variable files"}"
-bash: FILE: Sorry cannot locate the variable files
[root@localhost yjg]# FILE="test"
[root@localhost yjg]# echo "The file is ${FILE:?" Sorry cannot locate the variable files"}"
The file is test
3.使用变量保存系统参数
[root@localhost yjg]# SOURCE="/etc/passwd"
[root@localhost yjg]# DEST="/usr/local/yjg/passwd.bak"
[root@localhost yjg]# cp $SOURCE $DEST
4.设置只读变量
[root@localhost yjg]# TAPE_DEV="/dev/rmt/0n"
[root@localhost yjg]# echo ${TAPE_DEV}
/dev/rmt/0n
[root@localhost yjg]# readonly TAPE_DEV
[root@localhost yjg]# TAPE_DEV="/dev/rmt/1n"
-bash: TAPE_DEV: readonly variable
[root@localhost yjg]#
二、环境变量
1.
[root@localhost yjg]# CONSOLE=tty1
[root@localhost yjg]# export CONSOLE
[root@localhost yjg]# echo ${CONSOLE}
tty1
2.env ------------显示所有环境变量
3.unset CONSOLE
4.嵌入shell变量
[root@localhost yjg]# EXINIT='set nu tab=10';export EXINIT
[root@localhost yjg]# vi kkk.txt
[root@localhost yjg]# HOME=/usr/local;export HOME
[root@localhost yjg]# pwd
/home/yjg
[root@localhost yjg]# cd
[root@localhost ~]# pwd
/usr/local
[root@localhost ~]#
[root@localhost ~]# export IFS=:
[root@localhost ~]# echo $PATH
/usr/lib/qt-3.3/bin /usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /usr/X11R6/bin /root/bi
[root@localhost ~]# echo ${LOGNAME}
whoami
[root@localhost ~]#
[root@localhost ~]# EDITOR=vi;export EDITOR
[root@localhost ~]# echo ${EDITOR}
vi
三、set 命令
1、
[yjg@localhost ~]$ vi .profile
set -a
MAIL=/usr/mail/${LOGNAME:?}
PATH=$PATH:$HOME:bin
#
EDITOR=vi
TERM vt220
ADMIN=/usr/adm
PS1="'hostname'>"
2、
#!/bin/sh
#father script
echo "this is the father"
FILM="A few Good Men"
echo "I like the film:$FILM"
#
#
# call the child script
child.txt
echo "back to father"
echo "and the film is :$FILM"
[yjg@localhost ~]$ vi child.txt
#!/bin/sh
#child.txt
echo "Called from father .. i am the child"
echo "film name is :$FILM"
FILM="Die Hard"
echo "changing film to :$FILM"
3、
#!/bin/sh
#father script
echo "this is the father"
FILM="A few Good Men"
echo "I like the film:$FILM"
#
#
# call the child script
export FILM
child.txt
echo "back to father"
echo "and the film is :$FILM"
[yjg@localhost ~]$ vi child.txt
#!/bin/sh
#child.txt
echo "Called from father .. i am the child"
echo "film name is :$FILM"
FILM="Die Hard"
echo "changing film to :$FILM"
四、位置变量参数和特定变量参数
$#----传递到脚本的参数个数
$*-----以一个单字符串显示所有向脚本传递的个数,与位置变量不同,此选项参数可超过9个
$$-----脚本运行的当前进程ID号
$!------后台运行的最后一个进程的进程ID号
$@---- 与$#相同,但是使用时加引号,并在引号中返回每个参数
$- 显示shell使用的当前选项,与set命令相同
$?-----显示最后命令的退出状态,0表示没有错误,其它值表明有错误。
五、反引号
[root@localhost ~]# mydate=`date +%A" the "%e" of "%B" "%Y`
[root@localhost ~]# echo ${mydate}
[root@localhost ~]# expr 12 /* 12
144