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

简介:

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

我会以思维导图+代码段的方式,回滚学习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水平,能有所突破。




本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1607563,如需转载请自行联系原作者

相关文章
|
16天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
14天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
17天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
23天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
15 0
|
30天前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
50 0
|
30天前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
59 0
|
3月前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
39 0
|
4月前
|
运维 Shell Linux
Linux 之大数据定制篇-Shell 编程
Linux 之大数据定制篇-Shell 编程
123 0