思维导图学 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

目录
相关文章
|
28天前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
11天前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
36 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
28天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
1月前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
87 2
|
15天前
|
存储 Shell Linux
【Linux】shell基础,shell脚本
Shell脚本是Linux系统管理和自动化任务的重要工具,掌握其基础及进阶用法能显著提升工作效率。从简单的命令序列到复杂的逻辑控制和功能封装,Shell脚本展现了强大的灵活性和实用性。不断实践和探索,将使您更加熟练地运用Shell脚本解决各种实际问题
12 0
|
1月前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
2月前
|
监控 Shell Linux
在Linux中,如何使用shell脚本检测磁盘使用率?
在Linux中,如何使用shell脚本检测磁盘使用率?
|
11天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
167 65
|
3天前
|
安全 Linux
Linux系统之lsof命令的基本使用
【10月更文挑战第14天】Linux系统之lsof命令的基本使用
25 2
Linux系统之lsof命令的基本使用
|
13天前
|
监控 安全 网络协议