我们在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