变量測试语句-test

作用:用来測试变量是否相等,是否为空,文件类型等。

格式:

test 測试条件 或 [] #范围:整数,字符串。文件

1)整数測试:

test int1 -eq int2  測试整数是否相等

test int1 -ge int2  測试int1是否>=int2

test int1 -gt int2  測试int1是否>int2

test int1 -le  int2 測试int1是否<=int2

test int1 -lt int2  測试int1是否<int2

test int1 -ne int2  測试整数是否不相等

2)字符串測试:

test str1=str2  測试字符串是否相等

test str1!=str2  測试字符串是否不相等

test str1  測试字符串是否不为空

test -n str1  測试字符串是否不为空

test -z str1  測试字符串是否为空

3)文件測试:

test -d file  指定文件是否文件夹

test -f file  指定文件是否常规文件

test -x file  指定文件是否可运行

test -r file  指定文件是否可读

test -w file  指定文件是否可写

test -a file 指定文件是否存在

test -s file 文件的大小是否非0

注:test測试语句一般不单独使用,一般作为if语句的測试条件,如;

if test -d file
then
....
fi

test的变量的简写形式”[]”

演示样例-apachtest.sh

#!/bin/bash
# A test shell script for test Apache is running or not web=$(/usr/bin/pgrep httpd) echo "Now let's test the Apache..."
echo #if [ "$web" != "" ]
if [ -n "$web" ]
then
echo "Apache is running..."
else
echo "Apache is NOT running..."
/etc/rc.d/init.d/httpd start
fi

流程控制语句

流控制语句:用于控制shell程序的流程

exit语句:退出程序运行,并返回一个返回码,返回码为0表示正常退出,非0表示非正常退出。

比如:exit 0

一、if

if/then格式

if test -d $1
then
...
fi

演示样例-if_then.sh

#!/bin/bash
# A test shell script for if/then if [ -x /etc/rc.d/init.d/httpd ]
then
echo "Script: /etc/rc.d/init.d/httdp have x power!"
/etc/rc.d/init.d/httpd restart
fi

if/else格式

	if 条件1
then
命令1
elif 条件2
then
命令2
else
命令3
fi

多个条件的联合:

-a: 逻辑与,仅当两个条件都成立时,结果为真。

-o: 逻辑或,两个条件仅仅要有一个成立,结果为真。

演示样例-if_else.sh

#!/bin/bash
# A test shell script for if/elif/else echo -n "Please input a filename: "
read filename if [ -d $filename ]
then
echo "$filename is a directory"
elif [ -f $filename ]
then
echo "$filename is a commen file"
elif [ -c $filename -o -b $filename ]
then
echo "$filename is a device file"
else
echo "$filename is a unkown file"
fi

演示样例-if_elif_exit.sh

#!/bin/bash
# A test shell script for if/elif if [ $# -ne 2 ]
then
echo "Not enough parameters"
exit 1
fi if [ $1 -gt $2 ]
then
echo "$1 is great then $2"
elif [ $1 -lt $2 ]
then
echo "$1 is little then $2"
else
echo "$1 is equal as $2"
fi

二、for/in

for 变量 in 名字表
do
命令列表
done

演示样例-for.sh

#!/bin/bash
# A test shell script for "for" for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
echo "The day is $DAY"
done

awk命令[分段提取]

awk -F域分隔符 ‘命令’[单引號] #假设不用-F指定切割符,默觉得空格

1、检測系统中UID为0的用户

awk -F: '$3==0 {print $1}' /etc/passwd

#awk -F: '{print $1}' /etc/passwd

-F: 指定切割附为:

$3 表示以:为切割附的第三位

2、检測系统中password为空的用户

awk -F: 'length($2)==0 {print $1}' /etc/shadow

#ps aux | grep -v root | awk '{print $2}'

演示样例-awk.sh

#!/bin/bash
# A test script for desplay users infomation /bin/echo -n "Please input a username: "
read username /bin/grep $username /etc/passwd > /dev/null 2> /dev/null if [ $? -eq 0 ]
then
/bin/echo "username is: $username"
else
/bin/echo "user: $username is not exits."
exit 1
fi
/bin/echo # list /etc/passwd info
userinfo=`/bin/grep ^$username:x /etc/passwd`
uid=`echo $userinfo | awk -F: '{print $3}'`
gid=`echo $userinfo | awk -F: '{print $4'}`
dir=`echo $userinfo | awk -F: '{print $6}'`
shell=`echo $userinfo | awk -F: '{print $7}'` # get /etc/group info
groupinfo=`/bin/grep x:$gid /etc/group`
gname=`/bin/echo $groupinfo | awk -F: '{print $1}'` /bin/echo "user id is: $uid"
/bin/echo "default group is: $gname"
/bin/echo "home directory is: $dir"
/bin/echo "shell is: $shell"
/bin/echo "group member info:" # get group members
groups=`/usr/bin/groups $username`
/bin/echo $groups
/bin/echo # get online info
online=`/usr/bin/who | grep $username`
if [ -z "$online" ]
then
echo "$username is not online"
else
echo "$username is online..."
fi

实例-killuser.sh

#思路:将一个用户全部的进程包含shell都关闭,则相当于将该用户踢出了系统
#!/bin/bash
# A shell sript to kill a user in Linux username=$1 killpid=`/bin/ps aux | grep $username | awk '{print $2}'` for PID in $killpid
do
/bin/kill -9 $PID 2> /dev/null
done

Shell编程入门(第二版)(中)的更多相关文章

  1. Shell编程入门(第二版)(下)

    ... ... command n done #select把关键字中的每一项做成类似表单,以交互的方式执行do和done之间的命令 示例-select.sh [python] view plainc ...

  2. Shell编程入门(第二版)(上)

    简单的示例Shell程序 示例1. #!/bin/bash #This is to show what a shell script looks like echo "Our first e ...

  3. Learning ROS for Robotics Programming - Second Edition(《ROS机器人编程学习-第二版》)

    Learning ROS for Robotics Programming - Second Edition <ROS机器人编程学习-第二版> ----Your one-stop guid ...

  4. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  5. shell编程系列1--shell脚本中的变量替换

    shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...

  6. Linux Shell编程入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  7. 转:Linux Shell编程入门

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来 ...

  8. COM编程入门第二部分——深入COM服务器

    本文为刚刚接触COM的程序员提供编程指南,解释COM服务器内幕以及如何用C++编写自己的接口.继上一篇COM编程入门之后,本文将讨论有关 COM服务器的内容,解释编写自己的COM接口和COM服务器所需 ...

  9. 转inux Shell编程入门

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来 ...

随机推荐

  1. Quartz任务调度入门

    Quartz任务调度入门 了解Quartz体系结构Quartz对任务调度的领域问题进行了高度的抽象,提出了调度器.任务和触发器这3个核心的概念,并在org.quartz通过接口和类对重要的这些核心概念 ...

  2. prototype 与 __proto__

    原文:http://rockyuse.iteye.com/blog/1426510 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: 1 <script type= ...

  3. django rest_framework中将json输出字符强制为utf-8编码

    最近在和日本外包合作开发JIRA对接发布系统的版本单时, 遇到这个问题. 就是我们这边的输出浏览器显示为中文,而到了JIRA端就出现乱码. 查了文档,原来django rest_framework的默 ...

  4. HNOI 2014

    D1T1:画框 frame 题意:给你两个n阶正整数方阵,请你求最大的\( \sum_{i = 1}^{n} A_{i, p_i}\times \sum_{i = 1}^{n} B_{i, p_i}  ...

  5. 救济金发放(UVa133)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  6. NoSql数据库 设计上面的一些心得

    NoSql数据库这个概念听闻许久了,也陆续看到很多公司和产品都在使用,优缺点似乎都被分析的清清楚楚.但我心里一直存有一个疑惑,它的出现究竟是为了解决什么问题? 用户信息表,书籍信息表,用户为书籍打分信 ...

  7. CSS3之背景定位原点background-origin

    背景定位原点background-origin是CSS3新添加的有关背景的属性,主要是改变背景起始的原点位置的.CSS3之背景定位原点background-origin的属性值有三个:border.p ...

  8. OOD沉思录 --- 类和对象的关系 --- 使用关系

    使用关系 对象A的方法MethodA使用了B的方法MethodB,则表示A对B存在使用关系 使用关系的最关键问题在于,A如何找到B,存在6种方案 方案一: A包含了B,B作为一个成员定义在A的类中,那 ...

  9. Wannafly挑战赛7 D - codeJan与青蛙

    codeJan喜欢观察世界.有一天,codeJan发现一个非常奇怪的现象.有一些年轻的青蛙聚集在一条直线上的某些位置上,同一个位置可能有多个青蛙.这些青蛙每次只会向前跳一米,并且每只青蛙每跳一次都会发 ...

  10. Android之 解析XML文件(1)—— Pull解析

    (以下文章基本照抄<第一行代码>) 解析XML文件有很多方法,这里主要讲Pull解析和SAX解析.这篇文章主要是讲Pull解析. 一.Pull解析参考代码 先上代码: private vo ...