Skip to content

Commit bf367bd

Browse files
committed
add file
0 parents  commit bf367bd

33 files changed

+2302
-0
lines changed

README.md

Lines changed: 1169 additions & 0 deletions
Large diffs are not rendered by default.

example/demo1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
NUM=10
4+
5+
printf "输出数字$NUM\n"
6+
echo $NUM

example/demo10

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a=1.5
2+
3+
let "b = $a + 1.3" # 错误
4+
# t2.sh: let: b = 1.5 + 1.3: syntax error in expression (error token is ".5 + 1.3") 意为表达式错误(错误的符号".5 + 1.3")
5+
6+
echo "b = $b" # b=1

example/demo11

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
a=24
4+
b=47
5+
6+
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
7+
then
8+
echo "Test #1 succeeds."
9+
else
10+
echo "Test #1 fails."
11+
fi
12+
13+
# 错误: if [ "$a" -eq 24 && "$b" -eq 47 ]
14+
#+ 这会尝试执行' [ "$a" -eq 24 '
15+
#+ 然后会因没找到匹配的']'而失败.
16+
#
17+
# 注意: if [[ $a -eq 24 && $b -eq 24 ]]也可以.
18+
# 双方括号的if-test比
19+
#+ 单方括号的结构更灵活.
20+
# (第17行和第6行的"&&"有不同的意思.)
21+
# 多谢Stephane Chazelas指出这一点.
22+
23+
24+
if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
25+
then
26+
echo "Test #2 succeeds."
27+
else
28+
echo "Test #2 fails."
29+
fi
30+
31+
32+
# -a和-o选项提供
33+
#+ 混合条件测试另一个选择.
34+
# 多谢Patrick Callahan指出这一点.
35+
36+
37+
if [ "$a" -eq 24 -a "$b" -eq 47 ]
38+
then
39+
echo "Test #3 succeeds."
40+
else
41+
echo "Test #3 fails."
42+
fi
43+
44+
45+
if [ "$a" -eq 98 -o "$b" -eq 47 ]
46+
then
47+
echo "Test #4 succeeds."
48+
else
49+
echo "Test #4 fails."
50+
fi
51+
52+
53+
a=rhino
54+
b=crocodile
55+
if [ "$a" = rhino ] && [ "$b" = crocodile ]
56+
then
57+
echo "Test #5 succeeds."
58+
else
59+
echo "Test #5 fails."
60+
fi
61+
62+
exit 0

example/demo12

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
# Variables: 赋值和替换
4+
5+
a=375
6+
hello=$a
7+
8+
#-------------------------------------------------------------------------
9+
# =号的左右两边都不能有空白符.
10+
# 如果有一个空白符会怎么样?
11+
#
12+
# 如果用 "VARIABLE =value",
13+
# ^
14+
#+ 脚本会以为"VARIABLE"是一个命令并且此命令带了一个参数"=value"。
15+
#
16+
# 如果用 "VARIABLE= value",
17+
#   ^
18+
#+ 脚本会以为"value"是一个命令,
19+
#+ 并且把环境变量"VARIABLE"赋为空值:""。
20+
#-------------------------------------------------------------------------
21+
22+
23+
echo hello # 没有引用变量,只是输出字符串 "hello".
24+
25+
echo $hello
26+
echo ${hello} # 这句和上面的一句一样
27+
28+
echo "$hello"
29+
echo "${hello}"
30+
31+
echo
32+
33+
hello="A B C D"
34+
echo $hello # A B C D
35+
echo "$hello" # A B C D
36+
# 正如你所看到的:echo $hello和echo "$hello"产生不同的输出。
37+
#  ^ ^
38+
# 把变量引起来会保留空白字符.
39+
40+
echo
41+
42+
echo '$hello' # $hello
43+
# ^ ^
44+
# 在单引号中的变量引用会被禁止,
45+
#+ 字符"$"会仅仅被认为是一个普通的字符,而不是变量的前缀.
46+
47+
# 注意不同引用的不同效果.
48+
49+
50+
hello= # Setting it to a null value.
51+
echo "\$hello (null value) = $hello"
52+
# 注意具有null值的变量不等同于废弃(unset)此变量
53+
#+ 虽然最后的结果都是一样的(看下面的).
54+
55+
# --------------------------------------------------------------
56+
57+
# 在同一行里用空白字符隔开为多个变量赋值是可以的。
58+
#
59+
# 警告:这可能减少可读性,并且可能是不可移植的。
60+
61+
var1=21 var2=22 var3=$V3
62+
echo
63+
echo "var1=$var1 var2=$var2 var3=$var3"
64+
65+
# 在老版本的sh中这可能会引起问题
66+
67+
# --------------------------------------------------------------
68+
69+
echo; echo
70+
71+
numbers="one two three"
72+
# ^ ^
73+
other_numbers="1 2 3"
74+
# ^ ^
75+
# 如果给变量赋的值中有空白字符,引号是必须的。
76+
#
77+
echo "numbers = $numbers"
78+
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
79+
echo
80+
81+
echo "uninitialized_variable = $uninitialized_variable"
82+
# 未初始化的变量具有null值 (即是没有值).
83+
uninitialized_variable= # 声明,但没有初始化它 --
84+
#+ 这就好像上面一样给它设置一个null 值
85+
echo "uninitialized_variable = $uninitialized_variable"
86+
# 它仍然是null值.
87+
88+
uninitialized_variable=23 # 赋值
89+
unset uninitialized_variable # 销毁变量.
90+
echo "uninitialized_variable = $uninitialized_variable"
91+
# 结果仍然是null值.
92+
echo
93+
94+
exit 0

example/demo13

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
# escaped.sh: 转义字符
3+
4+
echo; echo
5+
6+
echo "\v\v\v\v" # 打印出 \v\v\v\v literally.
7+
# 用带着选项-e的'echo'会打印出转义字符串.
8+
echo "============="
9+
echo "VERTICAL TABS"
10+
echo -e "\v\v\v\v" # 打印四个垂直的制表符.
11+
echo "=============="
12+
13+
echo "QUOTATION MARK"
14+
echo -e "\042" # 打印出字符" (引号, 它的八进制ASCII码为42).
15+
echo "=============="
16+
17+
# 当使用像$'\X'的结构时,-e选项是多余的.
18+
echo; echo "NEWLINE AND BEEP"
19+
echo $'\n' # 新行.
20+
echo $'\a' # 警告 (蜂鸣).
21+
22+
echo "==============="
23+
echo "QUOTATION MARKS"
24+
# 版本2开始Bash已经允许使用$'\nnn'结构了.
25+
# 注意在这里,'\nnn'表示一个八进制的值.
26+
echo $'\t \042 \t' # Quote (") framed by tabs.
27+
28+
# 使用$'\xhhh'结构也可以使用十六进制数来转义.
29+
echo $'\t \x22 \t' # Quote (") framed by tabs.
30+
# 多谢Greg Keraunen指出这个..
31+
# 早期的Bash版本允许用'\x022'.(译者注,现在不行了)
32+
echo "==============="
33+
echo
34+
35+
36+
# 用ASCII码值把字符赋给变量.
37+
# ----------------------------------------
38+
quote=$'\042' # 引号"被赋给变量quote了.
39+
echo "$quote This is a quoted string, $quote and this lies outside the quotes."
40+
41+
echo
42+
43+
# 用连串的ASCII码把一串字符赋给变量..
44+
triple_underline=$'\137\137\137' # 137是字符'_'的ASCII码.
45+
echo "$triple_underline UNDERLINE $triple_underline"
46+
47+
echo
48+
49+
ABC=$'\101\102\103\010' # 101, 102, 103分别是A, B, C字符的八进制ASCII码.
50+
echo $ABC
51+
52+
echo; echo
53+
54+
escape=$'\033' # 033是ESC的ASCII码的八进制值
55+
echo "\"escape\" echoes as $escape"
56+
# 不可见的输出.
57+
58+
echo; echo
59+
60+
exit 0

example/demo14

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
echo hello
4+
echo $? # 因为上一条命令执行成功,打印0.
5+
6+
lskdf # 无效命令.
7+
echo $? # 因为上面的无效命令执行失败,打印一个非零的值.
8+
9+
echo
10+
11+
exit 113 # 返回113状态码给shell.
12+
# 可以运行脚本结束后立即执行命令"echo $?" 检验.
13+
14+
# 依照惯例,命令'exit 0'表示执行成功,
15+
#+ 当产生一个非零退出值时表示一个错误或是反常的条件。

example/demo15

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
file="demo15"
3+
if [ -r $file ]
4+
then
5+
echo "文件可读"
6+
else
7+
echo "文件不可读"
8+
fi
9+
if [ -w $file ]
10+
then
11+
echo "文件可写"
12+
else
13+
echo "文件不可写"
14+
fi
15+
if [ -x $file ]
16+
then
17+
echo "文件可执行"
18+
else
19+
echo "文件不可执行"
20+
fi
21+
if [ -f $file ]
22+
then
23+
echo "文件为普通文件"
24+
else
25+
echo "文件为特殊文件"
26+
fi
27+
if [ -d $file ]
28+
then
29+
echo "文件是个目录"
30+
else
31+
echo "文件不是个目录"
32+
fi
33+
if [ -s $file ]
34+
then
35+
echo "文件不为空"
36+
else
37+
echo "文件为空"
38+
fi
39+
if [ -e $file ]
40+
then
41+
echo "文件存在"
42+
else
43+
echo "文件不存在"
44+
fi

example/demo16

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
a=4
4+
b=5
5+
6+
# 这儿的"a"和"b"既能被看成是整数也能被看着是字符串。
7+
# 在字符串和数字之间不是严格界定的,
8+
#+ 因为Bash变量不是强类型的。
9+
10+
# Bash允许变量的整数操作和比较,
11+
#+ 但这些变量必须只包括数字字符.
12+
# 不管怎么样,你应该小心考虑.
13+
14+
echo
15+
16+
if [ "$a" -ne "$b" ]
17+
then
18+
echo "$a is not equal to $b"
19+
echo "(arithmetic comparison)"
20+
fi
21+
22+
echo
23+
24+
if [ "$a" != "$b" ]
25+
then
26+
echo "$a is not equal to $b."
27+
echo "(string comparison)"
28+
# "4" != "5"
29+
# ASCII 52 != ASCII 53
30+
fi
31+
32+
# 在这个实际的例子中,"-ne"和"!="都能工作.
33+
34+
echo
35+
36+
exit 0

example/demo17

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Bash版本信息:
3+
4+
for n in 0 1 2 3 4 5
5+
do
6+
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
7+
done
8+
9+
# BASH_VERSINFO[0] = 3 # 主版本号.
10+
# BASH_VERSINFO[1] = 00 # 次版本号.
11+
# BASH_VERSINFO[2] = 14 # 补丁级.
12+
# BASH_VERSINFO[3] = 1 # 编译版本.
13+
# BASH_VERSINFO[4] = release # 发行状态.
14+
# BASH_VERSINFO[5] = i386-redhat-linux-gnu # 结构体系
15+
# (和变量$MACHTYPE相同).
16+
17+
# 安装在系统里的Bash版本
18+
echo $BASH_VERSION
19+
20+
# 在目录堆栈里面最顶端的值
21+
echo $DIRSTACK

0 commit comments

Comments
 (0)