目录:

1.1 shell中常用运算符返回顶部

运算符 描述 示例
文件比较运算符
-e filename 如果 filename 存在,则为真 [ -e /var/log/syslog ]
-d filename 如果 filename 为目录,则为真 [ -d /tmp/mydir ]
-f filename 如果 filename 为常规文件,则为真 [ -f /usr/bin/grep ]
-L filename 如果 filename 为符号链接,则为真 [ -L /usr/bin/grep ]
-r filename 如果 filename 可读,则为真 [ -r /var/log/syslog ]
-w filename 如果 filename 可写,则为真 [ -w /var/mytmp.txt ]
-x filename 如果 filename 可执行,则为真 [ -L /usr/bin/grep ]
filename1 -nt filename2 如果 filename1 比 filename2 新,则为真 [ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2 如果 filename1 比 filename2 旧,则为真 [ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string 如果 string 长度为零,则为真 [ -z "$myvar" ]
-n string 如果 string 长度非零,则为真 [ -n "$myvar" ]
string1 = string2 如果 string1 与 string2 相同,则为真 [ "$myvar" = "one two three" ]
string1 != string2 如果 string1 与 string2 不同,则为真 [ "$myvar" != "one two three" ]
算术比较运算符
num1 -eq num2 等于 [ 3 -eq $mynum ]
num1 -ne num2 不等于 [ 3 -ne $mynum ]
num1 -lt num2 小于 [ 3 -lt $mynum ]
num1 -le num2 小于或等于 [ 3 -le $mynum ]
num1 -gt num2 大于 [ 3 -gt $mynum ]
num1 -ge num2 大于或等于 [ 3 -ge $mynum ]

1.2 使用if条件语句返回顶部

  1、单分支if语句

#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
fi

判断是否存在/media/cdrom目录,如果没有就创建

#!/bin/bash
if [ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 1
fi
fdisk -l /dev/sda

是root用户执行命令、不是root用户直接退出

  2、双分支if语句应用

#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $1 is up."
else
echo "Host $1 is down."
fi

使用ping测试网络连通性

    [root@localhost ~]# chmod +x pinghost.sh
    [root@localhost ~]# ./pinghost.sh 192.168.10.10
    Host 192.168.10.10 is up.
    [root@localhost ~]# ./pinghost.sh 192.168.10.1
    Host 192.168.10.1 is down.

  3、多分支if语句应用

#!/bin/bash
read -p "请输入您的分数(0-100):" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then
echo "$GRADE 分,优秀"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ]
then
echo "$GRADE 分,合格"
else
echo "$GRADE 分,不合格"
fi

多分支if语句应用

1.3 shell 中的for循环 返回顶部

  1、根据姓名列表批量的添加用户

    1)创建用户的列表文件
        [root@localhost ~]# vim /root/users.txt
        zhangsan
        lisi
        wangwu
    2)编辑批量添加用户的脚本
        [root@localhost ~]# vim uaddfor.sh

#!/bin/bash
ULIST=$(cat /root/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo "" | passwd --stdin $UNAME &>/dev/null
done

uaddfor.sh

        [root@localhost ~]# ./uaddfor.sh
        [root@localhost ~]#  tail -3 /etc/passwd

    3)编辑批量删除用户的脚本
        [root@localhost ~]# vim udelfor.sh

#!/bin/bash
ULIST=$(cat /bbb/users.txt)
for UNAME in $ULIST
do
userdel -r $UNAME &>/dev/null
done

udelfor.sh

  1、根据IP地址列表检查主机状态

    1)创建IP地址列表文件
        [root@localhost ~]# vim /bbb/ipadds.txt
        172.16.1.1
        172.16.1.111
        172.16.1.222

    2)编辑循环检查各主机的脚本
        [root@localhost ~]# vim chkhosts.sh

#!/bin/bash
HLIST=$(cat /bbb/ipadds.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is up."
else
echo "Host $IP is down."
fi
done

chkhosts.sh

1.4 shell中的while循环语句 返回顶部

  1、批量添加用户脚本

      [root@localhost ~]# vim uaddwhile.sh

#!/bin/bash
PREFIX="stu"
i=1
while [ $i -le 20 ]
do
useradd ${PREFIX}$i
echo "" | passwd --stdin ${PREFIX}$i &> /dev/null
let i++
done

uaddwhile.sh

  2、批量删除用户脚本

      [root@localhost ~]# vim udelwhile.sh

#!/bin/bash
PREFIX="stu"
i=1
while [ $i -le 20 ]
do
userdel -r ${PREFIX}$i
let i++ done

udelwhile.sh

  3、猜价格游戏

      [root@localhost ~]# vim pricegame.sh

#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格范围为0-999,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "恭喜你答对了,实际价格是 $PRICE"
echo "你总共猜测了$TIMES 次"
exit 0
elif [ $INT -gt $PRICE ] ; then
echo "太高了!"
else
echo "太低了!"
fi
done

pricegame.sh

1.5 使用case分支语句返回顶部

  1、编写检查用户输入的字符类型的脚本

      [root@localhost ~]# vim hitkey.sh

#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" KEY
case "$KEY" in
[a-z]|[A-Z])
echo "您输入的是 字母."
;;
[0-9])
echo "您输入的是 数字."
;;
*)
echo "您输入的是 空格、功能键或者其他控制字符."
esac

hitkey.sh

  2、编写系统服务脚本模板

#!/bin/bash
# The next lines are for chkconfig on RedHat systems.
# chkconfig: 35 98 02
# description: Starts and stops xxx Server # The next lines are for chkconfig on SuSE systems.
# /etc/init.d/xxx
#
### BEGIN INIT INFO
# Provides: xxx
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Short-Description: Starts and stops xxx Server
# Description: Starts and stops xxx Server
### END INIT INFO case $1 in
start) # 服务启动需要做的步骤
...
;;
stop) # 服务停止需要做的步骤
...
;;
restart) # 重启服务需要做的步骤
...
;;
status) # 查看状态需要做的步骤
...
;;
*) echo "$0 {start|stop|restart|status}"
exit 4
;;
esac

系统服务脚本模板

  3、编写squid服务脚本  

      [root@s2 ~]# vi /etc/init.d/squid

#!/bin/bash
#chkconfig: 2345 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#description: squid - internet object cache.
PID="usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -anpt | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动squid…….."
$CMD
fi
;; stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;; status)
[ -f $PID ] &> /dev/null
if [$? -eq 0 ]
then
netstat -anpt | grep squid
else
echo "squid is not running"
fi
;; restart)
$0 stop &> /dev/null
echo "正在关闭squid……"
$0 start &> /dev/null
echo "正在启动squid……"
;; reload)
$CMD -k reconfigure
;; check)
$CMD -k parse
;; *)
echo "用法:$0 {start | stop |restart | reload | check | status}"
;;
esac

squid

      将squid添加为系统服务
      [root@s2 init.d]# chmod +x /etc/init.d/squid
      [root@s2 init.d]# chkconfig --add squid
      [root@s2 init.d]# chkconfig squid on

02: shell中的if、case、for等语句的更多相关文章

  1. shell中select、case的使用

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select   select结 ...

  2. Shell中的条件测试和循环语句

    1.条件测试:test或[ 如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为0 运行结果: 带与.或.非的测试命令[ ! EXPR ] : E ...

  3. shell中while循环的陷阱

    在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...

  4. C Shell中的变量数组

    今天刚刚在看一点C Shell的内容,发现一个挺好玩的东西!就是环境变量可以像数组那样来设置!具体设置语法如下: set variable=(element1 element2 ...) //注意元素 ...

  5. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  6. shell中case的用法学习笔记

    这篇文章主要为大家介绍shell中的case语句:可以把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码. 本文转自:http://www.jbxue.com/article ...

  7. linux bash shell中case语句的实例

    本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...

  8. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  9. 【转】shell中的$0 $n $# $* $@ $? $$ 变量 if case for while

    shell中的$0 $n $# $* $@ $? $$  shell 编程 | shift 命令用法笔记 $0当前脚本的文件名 $n传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个 ...

随机推荐

  1. CTP API开发之一:CTP API简介

    官网下载CTP API 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台,CTP平 ...

  2. 2018-2019-2 20165330《网络对抗技术》Exp6 信息搜集与漏洞扫描

    目录 基础问题 相关知识 实验目的 实验内容 实验步骤 实验总结与体会 实验目的 掌握信息搜集的最基础技能与常用工具的使用方法. 返回目录 实验内容 各种搜索技巧的应用 使用搜索引擎 搜索网址目录结构 ...

  3. Linux:获取当前进程的执行文件的绝对路径

    摘要:本文介绍Linux的应用程序和内核模块获取当前进程执行文件绝对路径的实现方法. 注意:使用此方法时,如果执行一个指向执行文件的链接文件,则获得的不是链接文件的绝对路径,而是执行文件的绝对路径. ...

  4. Elasticsearch之settings和mappings的意义

    Elasticsearch之settings和mappings(图文详解)   Elasticsearch之settings和mappings的意义 简单的说,就是 settings是修改分片和副本数 ...

  5. Unified Modeling Language

    https://en.wikipedia.org/wiki/Unified_Modeling_Language

  6. php之setcookie

    注意在任何发送请求头之前不能有输出,不然会获取不到header头 setcookie(key,value, expires, 'path', domain); 在发送cookie时,会自动把cooki ...

  7. Area---poj1265(皮克定理+多边形求面积)

    题目链接:http://poj.org/problem?id=1265 题意是:有一个机器人在矩形网格中行走,起始点是(0,0),每次移动(dx,dy)的偏移量,已知,机器人走的图形是一个多边形,求这 ...

  8. python修饰器各种实用方法

    This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...

  9. Idempotent --------幂等

    1.在某二元运算下,幂等元素是指被自己重复运算的结果等于它自己的元素.例如,乘法下唯一两个幂等实数为0和1.

  10. 高并发秒杀系统方案(集成Mybatis和Redis)

    1.集成Mybatis 第一步,添加依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...