-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bf367bd
Showing
33 changed files
with
2,302 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
NUM=10 | ||
|
||
printf "输出数字$NUM\n" | ||
echo $NUM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
a=1.5 | ||
|
||
let "b = $a + 1.3" # 错误 | ||
# t2.sh: let: b = 1.5 + 1.3: syntax error in expression (error token is ".5 + 1.3") 意为表达式错误(错误的符号".5 + 1.3") | ||
|
||
echo "b = $b" # b=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/bin/bash | ||
|
||
a=24 | ||
b=47 | ||
|
||
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ] | ||
then | ||
echo "Test #1 succeeds." | ||
else | ||
echo "Test #1 fails." | ||
fi | ||
|
||
# 错误: if [ "$a" -eq 24 && "$b" -eq 47 ] | ||
#+ 这会尝试执行' [ "$a" -eq 24 ' | ||
#+ 然后会因没找到匹配的']'而失败. | ||
# | ||
# 注意: if [[ $a -eq 24 && $b -eq 24 ]]也可以. | ||
# 双方括号的if-test比 | ||
#+ 单方括号的结构更灵活. | ||
# (第17行和第6行的"&&"有不同的意思.) | ||
# 多谢Stephane Chazelas指出这一点. | ||
|
||
|
||
if [ "$a" -eq 98 ] || [ "$b" -eq 47 ] | ||
then | ||
echo "Test #2 succeeds." | ||
else | ||
echo "Test #2 fails." | ||
fi | ||
|
||
|
||
# -a和-o选项提供 | ||
#+ 混合条件测试另一个选择. | ||
# 多谢Patrick Callahan指出这一点. | ||
|
||
|
||
if [ "$a" -eq 24 -a "$b" -eq 47 ] | ||
then | ||
echo "Test #3 succeeds." | ||
else | ||
echo "Test #3 fails." | ||
fi | ||
|
||
|
||
if [ "$a" -eq 98 -o "$b" -eq 47 ] | ||
then | ||
echo "Test #4 succeeds." | ||
else | ||
echo "Test #4 fails." | ||
fi | ||
|
||
|
||
a=rhino | ||
b=crocodile | ||
if [ "$a" = rhino ] && [ "$b" = crocodile ] | ||
then | ||
echo "Test #5 succeeds." | ||
else | ||
echo "Test #5 fails." | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/bin/bash | ||
|
||
# Variables: 赋值和替换 | ||
|
||
a=375 | ||
hello=$a | ||
|
||
#------------------------------------------------------------------------- | ||
# =号的左右两边都不能有空白符. | ||
# 如果有一个空白符会怎么样? | ||
# | ||
# 如果用 "VARIABLE =value", | ||
# ^ | ||
#+ 脚本会以为"VARIABLE"是一个命令并且此命令带了一个参数"=value"。 | ||
# | ||
# 如果用 "VARIABLE= value", | ||
# ^ | ||
#+ 脚本会以为"value"是一个命令, | ||
#+ 并且把环境变量"VARIABLE"赋为空值:""。 | ||
#------------------------------------------------------------------------- | ||
|
||
|
||
echo hello # 没有引用变量,只是输出字符串 "hello". | ||
|
||
echo $hello | ||
echo ${hello} # 这句和上面的一句一样 | ||
|
||
echo "$hello" | ||
echo "${hello}" | ||
|
||
echo | ||
|
||
hello="A B C D" | ||
echo $hello # A B C D | ||
echo "$hello" # A B C D | ||
# 正如你所看到的:echo $hello和echo "$hello"产生不同的输出。 | ||
# ^ ^ | ||
# 把变量引起来会保留空白字符. | ||
|
||
echo | ||
|
||
echo '$hello' # $hello | ||
# ^ ^ | ||
# 在单引号中的变量引用会被禁止, | ||
#+ 字符"$"会仅仅被认为是一个普通的字符,而不是变量的前缀. | ||
|
||
# 注意不同引用的不同效果. | ||
|
||
|
||
hello= # Setting it to a null value. | ||
echo "\$hello (null value) = $hello" | ||
# 注意具有null值的变量不等同于废弃(unset)此变量 | ||
#+ 虽然最后的结果都是一样的(看下面的). | ||
|
||
# -------------------------------------------------------------- | ||
|
||
# 在同一行里用空白字符隔开为多个变量赋值是可以的。 | ||
# | ||
# 警告:这可能减少可读性,并且可能是不可移植的。 | ||
|
||
var1=21 var2=22 var3=$V3 | ||
echo | ||
echo "var1=$var1 var2=$var2 var3=$var3" | ||
|
||
# 在老版本的sh中这可能会引起问题 | ||
|
||
# -------------------------------------------------------------- | ||
|
||
echo; echo | ||
|
||
numbers="one two three" | ||
# ^ ^ | ||
other_numbers="1 2 3" | ||
# ^ ^ | ||
# 如果给变量赋的值中有空白字符,引号是必须的。 | ||
# | ||
echo "numbers = $numbers" | ||
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 | ||
echo | ||
|
||
echo "uninitialized_variable = $uninitialized_variable" | ||
# 未初始化的变量具有null值 (即是没有值). | ||
uninitialized_variable= # 声明,但没有初始化它 -- | ||
#+ 这就好像上面一样给它设置一个null 值 | ||
echo "uninitialized_variable = $uninitialized_variable" | ||
# 它仍然是null值. | ||
|
||
uninitialized_variable=23 # 赋值 | ||
unset uninitialized_variable # 销毁变量. | ||
echo "uninitialized_variable = $uninitialized_variable" | ||
# 结果仍然是null值. | ||
echo | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
# escaped.sh: 转义字符 | ||
|
||
echo; echo | ||
|
||
echo "\v\v\v\v" # 打印出 \v\v\v\v literally. | ||
# 用带着选项-e的'echo'会打印出转义字符串. | ||
echo "=============" | ||
echo "VERTICAL TABS" | ||
echo -e "\v\v\v\v" # 打印四个垂直的制表符. | ||
echo "==============" | ||
|
||
echo "QUOTATION MARK" | ||
echo -e "\042" # 打印出字符" (引号, 它的八进制ASCII码为42). | ||
echo "==============" | ||
|
||
# 当使用像$'\X'的结构时,-e选项是多余的. | ||
echo; echo "NEWLINE AND BEEP" | ||
echo $'\n' # 新行. | ||
echo $'\a' # 警告 (蜂鸣). | ||
|
||
echo "===============" | ||
echo "QUOTATION MARKS" | ||
# 版本2开始Bash已经允许使用$'\nnn'结构了. | ||
# 注意在这里,'\nnn'表示一个八进制的值. | ||
echo $'\t \042 \t' # Quote (") framed by tabs. | ||
|
||
# 使用$'\xhhh'结构也可以使用十六进制数来转义. | ||
echo $'\t \x22 \t' # Quote (") framed by tabs. | ||
# 多谢Greg Keraunen指出这个.. | ||
# 早期的Bash版本允许用'\x022'.(译者注,现在不行了) | ||
echo "===============" | ||
echo | ||
|
||
|
||
# 用ASCII码值把字符赋给变量. | ||
# ---------------------------------------- | ||
quote=$'\042' # 引号"被赋给变量quote了. | ||
echo "$quote This is a quoted string, $quote and this lies outside the quotes." | ||
|
||
echo | ||
|
||
# 用连串的ASCII码把一串字符赋给变量.. | ||
triple_underline=$'\137\137\137' # 137是字符'_'的ASCII码. | ||
echo "$triple_underline UNDERLINE $triple_underline" | ||
|
||
echo | ||
|
||
ABC=$'\101\102\103\010' # 101, 102, 103分别是A, B, C字符的八进制ASCII码. | ||
echo $ABC | ||
|
||
echo; echo | ||
|
||
escape=$'\033' # 033是ESC的ASCII码的八进制值 | ||
echo "\"escape\" echoes as $escape" | ||
# 不可见的输出. | ||
|
||
echo; echo | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
echo hello | ||
echo $? # 因为上一条命令执行成功,打印0. | ||
|
||
lskdf # 无效命令. | ||
echo $? # 因为上面的无效命令执行失败,打印一个非零的值. | ||
|
||
echo | ||
|
||
exit 113 # 返回113状态码给shell. | ||
# 可以运行脚本结束后立即执行命令"echo $?" 检验. | ||
|
||
# 依照惯例,命令'exit 0'表示执行成功, | ||
#+ 当产生一个非零退出值时表示一个错误或是反常的条件。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/sh | ||
file="demo15" | ||
if [ -r $file ] | ||
then | ||
echo "文件可读" | ||
else | ||
echo "文件不可读" | ||
fi | ||
if [ -w $file ] | ||
then | ||
echo "文件可写" | ||
else | ||
echo "文件不可写" | ||
fi | ||
if [ -x $file ] | ||
then | ||
echo "文件可执行" | ||
else | ||
echo "文件不可执行" | ||
fi | ||
if [ -f $file ] | ||
then | ||
echo "文件为普通文件" | ||
else | ||
echo "文件为特殊文件" | ||
fi | ||
if [ -d $file ] | ||
then | ||
echo "文件是个目录" | ||
else | ||
echo "文件不是个目录" | ||
fi | ||
if [ -s $file ] | ||
then | ||
echo "文件不为空" | ||
else | ||
echo "文件为空" | ||
fi | ||
if [ -e $file ] | ||
then | ||
echo "文件存在" | ||
else | ||
echo "文件不存在" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
a=4 | ||
b=5 | ||
|
||
# 这儿的"a"和"b"既能被看成是整数也能被看着是字符串。 | ||
# 在字符串和数字之间不是严格界定的, | ||
#+ 因为Bash变量不是强类型的。 | ||
|
||
# Bash允许变量的整数操作和比较, | ||
#+ 但这些变量必须只包括数字字符. | ||
# 不管怎么样,你应该小心考虑. | ||
|
||
echo | ||
|
||
if [ "$a" -ne "$b" ] | ||
then | ||
echo "$a is not equal to $b" | ||
echo "(arithmetic comparison)" | ||
fi | ||
|
||
echo | ||
|
||
if [ "$a" != "$b" ] | ||
then | ||
echo "$a is not equal to $b." | ||
echo "(string comparison)" | ||
# "4" != "5" | ||
# ASCII 52 != ASCII 53 | ||
fi | ||
|
||
# 在这个实际的例子中,"-ne"和"!="都能工作. | ||
|
||
echo | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
# Bash版本信息: | ||
|
||
for n in 0 1 2 3 4 5 | ||
do | ||
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}" | ||
done | ||
|
||
# BASH_VERSINFO[0] = 3 # 主版本号. | ||
# BASH_VERSINFO[1] = 00 # 次版本号. | ||
# BASH_VERSINFO[2] = 14 # 补丁级. | ||
# BASH_VERSINFO[3] = 1 # 编译版本. | ||
# BASH_VERSINFO[4] = release # 发行状态. | ||
# BASH_VERSINFO[5] = i386-redhat-linux-gnu # 结构体系 | ||
# (和变量$MACHTYPE相同). | ||
|
||
# 安装在系统里的Bash版本 | ||
echo $BASH_VERSION | ||
|
||
# 在目录堆栈里面最顶端的值 | ||
echo $DIRSTACK |
Oops, something went wrong.