思维导图学 Linux Shell攻略之小试牛刀篇

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dba10g.blog.51cto.com/764602/1607563

曾听一位大神讲过,带着目的去学,知识往往能记得牢,记得稳。借助思维导图这个工具,对一些我感兴趣的知识点进行分类管理。以后方便自己复习。

我会以思维导图+代码段的方式,回滚学习linux shell编程。

wKiom1TCUMziHNpwAAFVIO3v-Io357.jpg

转义/色彩

与用户交互的接口

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
#打印一个普通的字符串
[root@beijing ~] # echo "it's isa dog"
it's is a dog
  
#打印一个带有单引号和换行符的字符串,单引号可正常输出,但换行符没有效果
#没有达到想要的效果
[root@beijing ~] # echo "it's isa dog\n this is new line"
it's is a dog\n this is new line
  
# -e 开启转义功能
[root@beijing ~] # 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
  
[root@beijing ~] # echo it is a dog
it is a dog
  
#红字
[root@beijing ~] # echo -e "\e [1;31mthisis a color\e[0m"
this is a color
[root@beijing ~] # echo -e"\033[1;31mthis is a red color\033[0m"
this is a red  color
#绿底
[root@beijing ~] # echo -e"\e[1;42mthis is a red color\e[0m"
this is a red  color
  
#红字绿底
[root@beijing ~] # 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
[root@beijing  test ] # exportSTR="1234"
  [root@beijing  test ] # echo $STR
1234
[root@beijing  test ] # echo ${#STR}
4

函数

这是最基本的,不能语句罗列吧

1
2
3
4
5
6
7
#括号里不能有参数,获取参数通过$1,$2....获取
function  sayHello(){
          echohello $1
}
#$@:参数列表
#$*:参数字符串
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
#[[]] 必须有空格
#是否是文件,文件是否存在
[root@beijing  test ] # [[ -f 1.txt ]]&& echo "1.txt is file" || echo  "1.txt is notfile"
1.txt is  file
#是否是可执行文件
[root@beijing  test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can be execute"
1.txt can be execute
[root@beijing  test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can't be execute"
1.txt can't be execute
  [root@beijing  test ] # chmod  +x 1.txt
[root@beijing  test ] # [[ -x 1.txt ]]&& echo "1.txt can be execute" ||  echo  "1.txt can't be execute"
1.txt can be  execute
[root@beijing  test ] #
#是否是目录
[root@beijing  test ] # [[ -d 1.txt ]]&& echo "1.txt is dir" || echo  "1.txt is't dir"
1.txt is't  dir
[root@beijing  test ] # [[ -d /bin ]]&& echo "1.txt is dir" || echo  "1.txt is't dir"
1.txt is  dir
#判断是空串吗?
[root@beijing  test ] # [[ -z"1" ]] && echo "is null" ||  echo "is not null"
is not null
[root@beijing  test ] # [[ -z"" ]] && echo "is null" ||  echo "is not null"
is null
-z 与-n功能相反


小计

看书本,很简单的代码,也就是一看就懂的代码。其实真正自己写出来,在运行起来得到结果,也不容易。 眼高手低要不得。

我就在写程序是经常遇到一些这样情况。有时候要求有空格(比如条件判断时)。有时候不能有空格(变量赋值时)。有时候,单引号有时候又 反引号。哎要注意啊这些小细节,总结经验。

小小代码也不简单。

如果广大读者,也可以看着我的脑图,一步步写一下脚本,也会有所收获。

算个开篇吧。断断续续,随着学习深入,例子也会逐渐深入。希望自己的shell水平,能有所突破。

本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/1607563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值