-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathdemo20
executable file
·57 lines (41 loc) · 1.52 KB
/
demo20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# 检查一些系统环境变量
# 这是很好的预防性防护.
# 例如,如果$USER(用户在控制台的名字)没有被设置,
#+ 系统将不接受你.
: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?"MAIL变量没有定义,这里是我自定义的哦"}
echo
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "Your mail INBOX is located in $MAIL."
echo
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo
echo
# ------------------------------------------------------
# ${variablename?}结构也能检查一个脚本中变量的设置情况
#
ThisVariable=Value-of-ThisVariable
# 注意,顺便说一下字符串变量可能被设置成不允许的字符
#
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable".
echo
echo
: ${ZZXy23AB?"ZZXy23AB has not been set."}
# 如果变量ZZXy23AB没有被设置,
#+ 则脚本会打印一个错误信息而结束。
# 你可以指定错误信息.
# : ${variablename?"ERROR MESSAGE"}
# 等同于: dummy_variable=${ZZXy23AB?}
# dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."}
#
# echo ${ZZXy23AB?} >/dev/null
# 使用命令"set -u"后,再比较这些检查变量是否被设置的方法的不同...
#
echo "You will not see this message, because script already terminated."
HERE=0
exit $HERE # 不会执行到这儿.
# 实际上,这个脚本会返回退出状态码为1(echo $?).