1、bash中的算术运算

  • let运算符
[root@:vg_adn_tidbCkhsTest~/tidb-bench/sysbench]#let i=1+2
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i
3
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#let i1=10
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#let i2=20
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#let i3=$i1+$i2
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i3
30
  • $[expression]
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#i1=$[11+22]
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i1
33
  • $((experssion))
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#i=$((22+33))
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#echo $i
55
  • expr  arg1  操作符  arg2
[root@:vg_adn_tidbCkhsTest ~/tidb-bench/sysbench]#i=`expr 11 \* 10`;echo $i
110

一般情况下使用expr运算符都要用反引号

2、判断某个用户是否存在,如果不存在,则创建此用户

#!/bin/bash
id hadop &> /dev/null || useradd hadoop

3、计算passwd文件中第10个用户和第20个用户的UID之和

#!/bin/bash
uid1=$(id -u `cat /etc/passwd |head | tail -1|cut -d':' -f1`)
uid2=$(id -u `cat /etc/passwd |head -20| tail -1|cut -d':' -f1`)
sum=$[$uid1+$uid2]
echo "10 and 20 user 's uuid is $sum"

4、统计某个文件的空白行

[root@:vg_adn_tidbCkhsTest /tmp]#cat /etc/init.d/functions | grep '^$'| wc -l
90

5、判断返回值状态

[root@:vg_adn_tidbCkhsTest /tmp]#cat /etc/fstab &> /dev/null
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#cat /etc/fstabhaha &> /dev/null #查看一个不存在的文件
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1

6、条件测试

判断某需求是否满足,需要由测试机制来实现:

  如何编写测试表达式以实现所需的测试

  • (1)执行命令,并利用命令状态返回值来判断

    0:成功

    1:失败

  • (2)测试表达式

    test EXPRESSION

    [ EXPRESSION ]  单中括号多见于数值比较

    [[ EXPRESSION ]]   双中括号多见于字符串比较

      注意:EXPRESSION两段必须有空白字符,否则为语法错误

bash的测试类型

数值测试:

-eq:是否等于

-ne:是否不等于

-gt:是否大于

-ge:是否大于等于

-lt:是否小于

-le:是否小于等于

[root@:vg_adn_tidbCkhsTest /tmp]#[ 11 -eq 21 ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#[ 11 -eq 11 ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

字符串测试

==:是否等于

>:是否大于

<:是否小于

!=:是否不等于

=~:左侧字符串能否被右侧的PATTERN所匹配。右侧的PATTERN不能使用bash的通配符来进行。并且PATTERN是匹配左侧字符串的某一部分而已

-z "STRING":判断指定的字串是否为空,空则为真,不空则为假

-n "STRING":判断指定的字符串是否不空,不空则真,空则为假。

[root@:vg_adn_tidbCkhsTest /tmp]#[ tom == tom ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[ tom != tom ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#[ "tom" != "join" ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

并不是说字符串比较这样子就完美了,如下所示:

[root@:vg_adn_tidbCkhsTest /tmp]#[ "a" > "b" ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[ "a" < "b" ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[[ "a" > "b" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#[[ "a" < "b" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

所以说字符串的比较有两点要注意:

1、字符串要加引用

2、要使用双中括号[[ EXPRESSION ]]来进行比较

[root@:vg_adn_tidbCkhsTest /tmp]#name=tom[root@:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o.*" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o*" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "om" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[[ $name =~ "o?" ]]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1

7、文件测试

文件存在性测试:

-e:FILE    文件的存在性测试,存在则为真,不存在则为假

文件类型测试:

-b FILE:文件是否为块设备文件

-c FILE: 文件是否为字符设备文件

-d FILE:文件是否是目录文件

-f FILE:文件是否是普通文件

-l FILE:文件是否为链接文件

-S FILE:文件是否为套接字文件

-p FILE:文件是否为管道文件

[root@:vg_adn_tidbCkhsTest /tmp]#[ -b /dev/xvda1 ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[ -f /etc/init.d/functions ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

文件权限性测试

-r  FILE:文件是否可读

-w FILE:文件是否可写

-x FILE:文件是否可执行

[root@:vg_adn_tidbCkhsTest /tmp]#[ -x /usr/bin/passwd ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

文件特殊权限测试

-g FILE:文件是否拥有SGID权限

-u FILE:文件是否拥有SUID权限

-k FILE:文件是否拥有sticky权限

[root@:vg_adn_tidbCkhsTest /tmp]#[ -u /usr/bin/passwd ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

文件大小测试

-s FILE :文件是否为非空内容,有内容则为真,无内容则为假

[root@:vg_adn_tidbCkhsTest /tmp]#touch ./haha.sh
[root@:vg_adn_tidbCkhsTest /tmp]#[ -s haha.sh ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1
[root@:vg_adn_tidbCkhsTest /tmp]#echo "hahahaha" > haha.sh
[root@:vg_adn_tidbCkhsTest /tmp]#[ -s haha.sh ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0

从属关系测试

-O FILE:当前有效用户是否为文件属主

-G FILE:当前有效用户是否为文件属组

[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
0
[root@:vg_adn_tidbCkhsTest /tmp]#[ -O /home/tidb ]
[root@:vg_adn_tidbCkhsTest /tmp]#echo $?
1

组合条件运算

[ condition1 -a condition2 ]    相当于  condition1  &&  condition2

[ condition1 -o condition2 ]    相当于  condition1  ||  condition2

[ -not condition ]  相当于  !condition

练习题:判断主机名是否为空或主机名是“localhost.localdomain”

#!/bin/bash
hostName=$(hostname)
[ -z "$hostName" ] || [[ $hostName == "localhost.localdomain" ]] #这一句相当于: [ -n "$hostName" -o $hostName == "localhost.localdomain" ]
if [ $? -eq 0 ];then
hostname www.feng.com
fi

脚本的状态返回值

默认是脚本中执行的最后一条命令的状态返回值

自定义状态退出状态码:

exit [n]:n为自己指定的状态码。

注意L:shell进程遇到exit时,即会终止,因此,整个脚本执行即为结束。

向脚本传递参数:

位置参数变量

Scripts.sh arg1  arg2

引用方式:

$1,$2,......${n}..

[root@:vg_adn_tidbCkhsTest /tmp]#cat test4.sh
#!/bin/bash
file1_lines=$(grep '^$' $1 | wc -l)
file2_lines=$(grep '^$' $2 | wc -l)
echo "the totle lines: $[${file1_lines} + ${file2_lines}]"
[root@:vg_adn_tidbCkhsTest /tmp]#bash test4.sh /etc/init.d/functions /etc/issue
the totle lines: 91

特殊变量:

$0:脚本文件路径本身

$#:保存传递给当前脚本的参数的个数。

$*与$@:保存传递给当前脚本的所有参数。

[root@:vg_adn_tidbCkhsTest /tmp]#cat test5.sh
#!/bin/bash
echo "the name of scripts is $(basename $0)"
echo "the arg's sum is $#"
echo "some names are $*"
[root@:vg_adn_tidbCkhsTest /tmp]#./test5.sh 1.txt 2.txt
the name of scripts is test5.sh
the arg's sum is 2
some names are 1.txt 2.txt

练习题:编写一个脚本,用来添加一个用户,要求必须指定一个用户

[root@:vg_adn_tidbCkhsTest /tmp]#cat test6.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo "At least one user";
   exit 2
fi
if grep "^$1\>" /etc/passwd &> /dev/null;then
echo "user has been exits"
else
useradd $1
echo $1 | passwd --stdin $1 &> /dev/null
echo "useradd finished"
fi
[root@:vg_adn_tidbCkhsTest /tmp]#./test6.sh hbase
useradd finished
[root@:vg_adn_tidbCkhsTest /tmp]#./test6.sh hbase
user has been exits
[root@:vg_adn_tidbCkhsTest /tmp]#

练习题:通过命令行参数给定两个数字,输出其中较大的数值

[root@:vg_adn_tidbCkhsTest /tmp]#cat test7.sh
#!/bin/bash
if [ $# -lt 2 ];then
echo "At least two numbers"
exit 2
fi
if [ $1 -lt $2 ];then
echo "The max num is $2"
else
echo "The max num is $1"
fi
[root@:vg_adn_tidbCkhsTest /tmp]#./test7.sh 3
At least two numbers
[root@:vg_adn_tidbCkhsTest /tmp]#./test7.sh 3 5
The max num is 5
[root@:vg_adn_tidbCkhsTest /tmp]#./test7.sh 7 5
The max num is 7

练习题:通过命令行参数给定一个用户名,判断其ID号时偶数还是奇数

[root@:vg_adn_tidbCkhsTest /tmp]#cat test8.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo "At least one user"
exit 2
fi
if ! grep "^$1\>" /etc/passwd &> /dev/null ;then
echo "user not exits"
else
uuid=`id -u $1`
if [ $uuid%2==0 ];then
echo "this uuid is a ji shu"
else
echo "this uuid is a ou shu"
fi
fi
[root@:vg_adn_tidbCkhsTest /tmp]#./test8.sh hbase
this uuid is a ji shu

Linux运维之shell脚本基础知识的更多相关文章

  1. linux运维自动化shell脚本小工具

    linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU # ...

  2. linux运维需要掌握的基础知识

    踏入linux运维工程师这一职业,其实有很多工具技能需要掌握,下面我来给大家一一介绍. 1.shell脚本和另一个脚本语言,shell是运维人员必须具备的,不懂这个连入职都不行,至少也要写出一些系统管 ...

  3. Linux运维之shell脚本进阶篇

    一.if语句的使用 1)语法规则 if [条件] then 指令 fi 或 if [条件];then 指令 fi 提示:分号相当于命令换行,上面两种语法等同特殊写法:if[ -f"$file ...

  4. Linux运维之shell脚本

    一.bash漏洞 1)bash漏洞 bash漏洞是控制Linux计算机命令提示符的软件中存在的漏洞. bash是一个为GNU计划编写的Unix shell.它的名字是一系列缩写:Bourne-Agai ...

  5. CentOS 下运维自动化 Shell 脚本之 expect

    CentOS 下运维自动化 Shell脚本之expect 一.预备知识: 1.在 Terminal 中反斜杠,即 "" 代表转义符,或称逃脱符.("echo -e与pri ...

  6. 什么是Shell?Shell脚本基础知识详细介绍

    这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...

  7. Linux运维七:网络基础

    1:网线 2:交换机,路由器 交换机(Switch)意为“开关”是一种用于电(光)信号转发的网络设备.它可以为接入交换机的任意两个网络节点提供独享的电信号通路.最常见的交换机是以太网交换机.其他常见的 ...

  8. Shell脚本基础知识详细介绍(一)

    Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种 ...

  9. shell脚本基础知识

    虽然现在能在Linux系统下生存,但是自觉效率太低,和高手有很大的差距. 这就是关于Linux的知识太过匮乏,有很多事情知道该怎么做,但是就是没法在Linux下实现,为了提升工作效率,必须要接触Lin ...

随机推荐

  1. JSP 基础(二)

    五 注释  5.1 JSP注释     <%--注释内容--%> 5.2 HTML注释    <!--注释内容--> 5.3 Java注释 六 JSP指令 在JSP中有三种类型 ...

  2. C++语言------顺序表实现,用动态数组的方法

    C++ 中常用的一些东西,通过使用动态数组来实现顺序表, 掌握了一下知识点: 1.预处理有三中方法 宏定义,文件包含,条件编译 2.使用同名的变量时,可以在外层使用命名空间 类解决变量名重定义的错误 ...

  3. [总结]多项式类数学相关(定理&证明&板子)

    目录 写在前面 前置技能 多项式相关 多项式的系数表示 多项式的点值表示 复数相关 复数的意义 复数的基本运算 单位根 代码相关 多项式乘法 快速傅里叶变换 DFT IDFT 算法实现 递归实现 迭代 ...

  4. Git 使用SSH密钥操作

    git使用ssh密钥 git支持https和git两种传输协议,github分享链接时会有两种协议可选: git协议链接图例 : ↓ https协议链接图例:↓ git使用https协议,每次pull ...

  5. WEB控件没有什么所谓好不好,而是用得好不好

    这几天Insus.NET有写几篇博文,虽然写得没怎么样,但均是Insus.NET现实开发过程中所遇或是所想的一些内容.<没有什么,开发ASP.NET时随便写写,想到什么写什么>http:/ ...

  6. winform窗体 小程序【进程】

    进程 一个应用程序就是一个进程,我的理解是,只要是打开应用程序,就会创建进程. 在.NET框架在using.System.Diagnostics名称空间中,有一个类Process,用来创建一个新的进程 ...

  7. Scott用户的表结构

    在Oracle的学习之中,重点使用的是SQL语句,而所有的SQL语句都要在scott用户下完成,这个用户下一共有四张表,可以使用: SELECT * FROM tab; 查看所有的数据表的名称,如果现 ...

  8. Android 7.0 以上保留数据去除锁屏密码的方法

    在很多情况下,由于我们的疏忽,导致忘记了锁屏密码.也没有开启 USB 调试,内置存储中还有要保留的数据.我们需要在不清除数据的情况下删除锁屏密码.接下来我们就介绍该如何做. 注意事项 首先要注意,这个 ...

  9. 无法外网访问VM中的hadoop yarn的8088端口

    1.检查是否正确的启动了resourcemanager服务 若是没有启动,请检查yarn-site-xml配置 2.若是启动了 1.检查客户机和虚拟机之间是否能够相互ping通 2.检查虚拟机防火墙是 ...

  10. NIO学习笔记六:channel 之前数据传输及scatter/gather

    在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel传输到另外一个channel. FileChannel的transferFrom()方法可以将 ...