环境和shell变量

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值