思维导图学 Linux Shell攻略之小试牛刀篇
曾听一位大神讲过,带着目的去学,知识往往能记得牢,记得稳。借助思维导图这个工具,对一些我感兴趣的知识点进行分类管理。以后方便自己复习。
我会以思维导图+代码段的方式,回滚学习linux shell编程。
转义/色彩
与用户交互的接口
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
|
#打印一个普通的字符串 [[email protected] ~] # echo "it's isa dog"
it's is a dog #打印一个带有单引号和换行符的字符串,单引号可正常输出,但换行符没有效果 #没有达到想要的效果 [[email protected] ~] # echo "it's isa dog\n this is new line"
it's is a dog\n this is new line # -e 开启转义功能 [[email protected] ~] # echo -e "it'sis a dog\nthis is new line"
it's is a dog this is new line -e enable interpretation of backslash escapes
[[email protected] ~] # echo it is a dog
it is a dog #红字 [[email protected] ~] # echo -e "\e [1;31mthisis a color\e[0m"
this is a color [[email protected] ~] # echo -e"\033[1;31mthis is a red color\033[0m"
this is a red color #绿底 [[email protected] ~] # echo -e"\e[1;42mthis is a red color\e[0m"
this is a red color #红字绿底 [[email protected] ~] # echo -e"\e[1;31;42mthis is a red color\e[0m"
this is a red color #有效数字 echo "scale=3;3/8" | bc
echo $ bc
|
计算
这是编程语言的功能之一了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
va=1; vb=2; #echo $($va+$vb);error #echo $(va+vb); error echo [$va+$vb] #output :[1+2]
echo $[va+vb] #ok
echo $(($va+$vb)) #//ok
let result=$va+vb #ok
echo $result
result=` expr 3 + 1` #ok, 注意等号,两边不能有空格;result=`expr $va + 1` 也可以
echo $result
result=$( expr $va + 1) #ok, 注意等号,两边不能有空格,+号必须有空格,否则会当成字符串输出
echo $result
|
输出变量长度
内置功能(感兴趣而已)
1
2
3
4
5
|
1234 4 |
函数
这是最基本的,不能语句罗列吧
1
2
3
4
5
6
7
|
#括号里不能有参数,获取参数通过$1,$2....获取 function sayHello(){
echohello $1
} #[email protected]:参数列表 #$*:参数字符串 sayHello zgy; #这样调用
|
读取命令序列
可得一个命令的结果
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
|
#!/bin/bash COMMANDS= ls | cat -n
echo $COMMANDS #输出为空
COMMANDS=$( ls | cat -n)
#$COMMANDS #error
echo $COMMANDS #输出期望结果
echo `$COMMANDS` #error
echo ` ls | cat -n` #输出期望结果 反引用
############################################### #子shell,在子shell操作,不影响主shell echo ` pwd `;
cd /bin
echo ` pwd `;
# output# # /root/test # /bin echo ` pwd `;
( cd /bin )
echo ` pwd `;
# output# # /root/test # /root/test |
打印所用时间
评定一个算法的效率
1
2
3
4
5
6
7
8
9
10
|
start=$( date +%s) #start=`date +%s`,等号不能有空格,如果有空格,会被变量当成命令
for (( i = 0; i < 100000; i++ )); do
echo $i > /dev/null
done end=` date +%s`
diff =$(($end-$start))
echo "use times(ms):" $ diff
echo "use times(ms):" $(($end-$start))
|
常用的测试
判断权限等,shell编程汇总功能常用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#[[]] 必须有空格 #是否是文件,文件是否存在 1.txt is file
#是否是可执行文件 [[email protected] test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can be execute"
1.txt can be execute [[email protected] test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can't be execute"
1.txt can't be execute [[email protected] test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" || echo "1.txt can't be execute"
1.txt can be execute #是否是目录 1.txt is't dir
1.txt is dir
#判断是空串吗? is not null is null -z 与-n功能相反 |
小计
看书本,很简单的代码,也就是一看就懂的代码。其实真正自己写出来,在运行起来得到结果,也不容易。 眼高手低要不得。
我就在写程序是经常遇到一些这样情况。有时候要求有空格(比如条件判断时)。有时候不能有空格(变量赋值时)。有时候,单引号有时候又 反引号。哎要注意啊这些小细节,总结经验。
小小代码也不简单。
如果广大读者,也可以看着我的脑图,一步步写一下脚本,也会有所收获。
算个开篇吧。断断续续,随着学习深入,例子也会逐渐深入。希望自己的shell水平,能有所突破。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1607563,如需转载请自行联系原作者