红联Linux门户
Linux帮助

浅谈Linux内部环境变量

发布时间:2015-03-05 14:50:38来源:linux网站作者:圣地亚哥

我们在Window的环境安装Java,必须先配置环境变量才可以运行Java的虚拟机。当然,在Linux安装Java Oracle同理也要配置path,java_home等,环境变量。

常见的环境变量有$OSTYPE $MACHTYPE  $LINENO  $HOSTTYPE $HOME  $GLOBIGNORE $FUNCNAME  $DIRSTACK  $MACHTYPE……..这些内部环境变量在写脚本的时候很有用!

我现在就聊下有意思的环境变量,先是BASH_VERSINFO[n](n=1,2,3,4,5)

# Bash version info:
for n in 0 1 2 3 4 5
do
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done
# BASH_VERSINFO[0] = 2                      #主要版本号
# BASH_VERSINFO[1] = 05                    # 次要版本号.
# BASH_VERSINFO[2] = 8                      # 补丁层次
# BASH_VERSINFO[3] = 1                      # 创建版本
# BASH_VERSINFO[4] = release                # 发行状态.
# BASH_VERSINFO[5] = i386-RedHat-linux-gnu  # 架构
                                            # (与 $MACHTYPE一样).
$FUNCNAME
xyz23 ()
{
echo "$FUNCNAME now executing."  # xyz23 now executing.
}
xyz23
echo "FUNCNAME = $FUNCNAME"        # FUNCNAME =
                                  # 在函数外面是空值
$IFS
输入区域分隔
默认是space, tab, and newline(/n), 但是也可能改变,比如去解析一个数据分隔符
注意$* 用在第一地址空间 在 $IFS里.
bash$ echo $IFS | cat -vte
bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"'
#w:x:y:z
Example $IFS and whitespace

#!/bin/bash
# $IFS treats whitespace differently than other characters.
output_args_one_per_line()
{
forarg
do echo "[$arg]"
done
}
echo; echo "IFS=\" \""
echo "-------"
IFS=" "
var=" a  b c  "
output_args_one_per_line $var  # output_args_one_per_lines输出" a  b c  "`#
# [a]
# [b]
# [c]
echo; echo "IFS=:"
echo "-----"
IFS=:
var=":a::b:c:::"              # 与上面相同 但是替换":" 成 " ".
output_args_one_per_line $var
#
# []
# [a]
# []
# [b]
# [c]
# []
# []
# []
echo
exit 0

$LINENO
这个变量常在脚本里出现. 对于调bug很有用
# *** BEGIN DEBUG BLOCK ***
last_cmd_arg=$_  # Save it.
echo "At line number $LINENO, variable \"v1\" = $v1"
echo "Last command argument processed = $last_cmd_arg"

$PWD
工作目录(你现在在的目录)
删除目录脚本!!!!
#!/bin/bash
E_WRONG_DIRECTORY=73
clear # Clear screen.
TargetDirectory=/home/bozo/projects/GreatAmericanNovel
cd $TargetDirectory
echo "Deleting stale files in $TargetDirectory."
if [ "$PWD" != "$TargetDirectory" ]
then  
echo "Wrong directory!"
echo "In $PWD, rather than $TargetDirectory!"
echo "Bailing out!"
exit $E_WRONG_DIRECTORY
fi
rm -rf *
rm .[A-Za-z0-9]*    # 删除带点的文件(比如隐藏文件)
# rm -f .[^.]* ..?*  移除多个点的文件

$REPLY

$SECONDS
这个变量是计算脚本运行的多长时间
#!/bin/bash
ENDLESS_LOOP=1
INTERVAL=1
echo
echo "Hit Control-C to exit this script."
echo
while [ $ENDLESS_LOOP ]
do
if [ "$SECONDS" -eq 1 ]
then
units=second
else
units=seconds
fi
echo "This script has been running $SECONDS $units."
sleep $INTERVAL
done
exit 0

Example. Timed read
#!/bin/bash
TIMELIMIT=4        # 4 seconds
read -t $TIMELIMIT variable <&1
echo
if [ -z "$variable" ]
then
echo "Timed out, variable still unset."
else
echo "variable = $variable"
fi
exit 0
$UID
可以检查是不是root用户,可用于固件硬化
Example 9-5. Am I root?
#!/bin/bash
ROOT_UID=0  # root用户$UID 0.
if [ "$UID" -eq "$ROOT_UID" ]  then
echo "You are root."
else
echo "You are just an ordinary user (but mom loves you just the same)."
fi
exit 0
# =============================== #
##着有另一个方法
ROOTUSER_NAME=root
username=ìd -nu`            
# orusername=`whoami`
ìf [ "$username" = "$ROOTUSER_NAME" ]
then
echo "Rooty, toot, toot. You are root."
else
echo "You are just a regular fella."
fi

$#
命令行第二个表达式或者是单个的变量
$*
所有的单个参数,就想一个单词一样
$@
与 $*一样, 但是每一个参数是一个限定的字符串, 因此字符串是功过交互, 没有多余的作用,意思就是,在其他的字符串里每一个字符串都看成一个分隔的单词。
Example  arglist: Listing arguments with $* and $@

#!/bin/bash
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0àrgument1 argument2 etc."
exit $E_BADARGS
fi
echo
index=1
echo "Listing args with \"\$*\":"
for arg in "$*"  # 不起作用"$*" 不是限制.
do
echo "Arg #$index = $arg"
let "index+=1"
done            # $* 看成是所有的表达是的单个单词
echo "Entire arg list seen as single word."
echo
index=1
echo "Listing args with \"\$@\":"
forarg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done            # $@ 看所有的额变大事是一个分隔的单词
echo "Arg list seen as separate words."
echo
exit 0

还可以加一个特别有意思的,加一个shift,前面的一个数值就会消失,小伙伴可以试试。
.
#!/bin/bash
# Invoke with ./scriptname 1 2 3 4 5
echo "$@"    # 1 2 3 4 5
shift
echo "$@"    # 2 3 4 5
shift
echo "$@"    # 3 4 5
# Each "shift" loses parameter $1.

其他特殊参数
$-
在脚本里作为一个标记,一个作用就是可以交互自测
$!
PID (process id) 最后一个进程
$_
这个特殊变量的值是设置在提交前的的值
Example 9-9. underscore variable
#!/bin/bash
echo $_              # /bin/bash
du >/dev/null        #没有输出
echo $_              # du
ls -al >/dev/null    # 没有输出
echo $_              # -al  (last argument)
:
echo $_            
reference:Advanced.Bash.Shell.Scripting.Guid,unix.shell范例精讲。