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

目录
相关文章
|
5天前
|
Shell Linux
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
60 32
|
5月前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
3月前
|
存储 Shell Linux
Linux 如何更改默认 Shell
Linux 如何更改默认 Shell
87 0
Linux 如何更改默认 Shell
|
4月前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
109 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
3月前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
5月前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
4月前
|
存储 Shell Linux
【Linux】shell基础,shell脚本
Shell脚本是Linux系统管理和自动化任务的重要工具,掌握其基础及进阶用法能显著提升工作效率。从简单的命令序列到复杂的逻辑控制和功能封装,Shell脚本展现了强大的灵活性和实用性。不断实践和探索,将使您更加熟练地运用Shell脚本解决各种实际问题
64 0
|
9天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
3月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
347 8
|
3月前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
1134 6