echo $? 代表上一次命令的状态返回值,‘0’则代表为真《执行成功》,‘非零’则代表为假《执行失败》。

shell脚本: <判断老男孩的年纪>

[root@bogon mnt]# vim oldboy_age.sh
#!/bin/bash
oldboy_age=34 read -p 'his age is :' age if [ $age -eq $oldboy_age ];then
echo 'I always believe you'
elif [ $age -gt $oldboy_age ];then
echo 'try smaller again'
else
echo 'try bigger again'
fi
~
[root@bogon mnt]# chmod +x userdel.sh
[root@bogon mnt]# ./oldboy_age.sh
his age is :12
try bigger again

Part 循环结构

while 循环 

while (条件)                       

do
动作
done

需要无限循环时我们会选择while :

: 永远为真,作为条件可无限循环

《while循环来判断上面老男孩的年纪》

[root@bogon mnt]# vim oldboy_age.sh
#!/bin/bash
oldboy_age=34
while :
do
read -p 'his age is :' age

if [ -z $age ];then   #如果输入为空,则继续输入.
       continue
    fi

if [ $age -eq $oldboy_age ];then
echo 'I always believe you'
break
elif [ $age -gt $oldboy_age ];then
echo 'try smaller again'
else
echo 'try bigger again'
fi
done
~
[root@bogon mnt]# ./oldboy_age.sh
his age is :34
I always believe you

FOR循环  -------- 《批量创建系统用户》

[root@bogon mnt]# vim useradd.sh
#!/bin/bash
for i in {1..10}
do
useradd user$i
done
[root@bogon mnt]# chmod +x useradd.sh
[root@bogon mnt]# ./useradd.sh

--------------------《批量删除用户》

[root@bogon mnt]# vim userdel.sh

!/bin/bash
for i in {1..10}
do
userdel user$i
done [root@bogon mnt]# chmod +x userdel.sh
[root@bogon mnt]# chmod +x userdel.sh
[root@bogon mnt]# ./userdel.sh

练习题:

(1) 测试那些IP能ping通网络

[root@bogon mnt]# ./ping.sh
#!/bin/bash
for i in {1..50};
do
ping -c1 192.168.61.$i &> /dev/null #将ping的过程记录到该文件夹下,不显示在桌面
if [ $? -eq 0 ];then #判断上次命令是否ping成功,$?表示返回上次的命令值
echo "192.168.61.$i successful"
echo "192.168.61.$i" >> /tmp/a.txt #将ping通的IP记录到/tmp/a.txt下
fi
done
[root@bogon mnt]# chmod +x ping.sh
[root@bogon mnt]# ./ping.sh

(2)  查看特定目录下的文件类型《目录文件/链接文件/普通文件有多少?》

[root@bogon mnt]# vim file.sh
if
[ -z file_name ];then
continue
else
break
fi
done
for i in $(ls $file_name)
do
if [ -h $file_name/$i ];then
((link_file+=1))
elif [ -f $file_name/$i ];then
((regular_file+=1))
elif [ -d $file_name/$i ];then
((directory_file+=1))
fi
done
echo "lianjiewenjian: $link_file"
echo "putongwenjian: $regular_file"
echo "directorywenjian: $directory_file"
[root@bogon mnt]# chmod +x file.sh
[root@bogon mnt]# ./file.sh

简单的for循环:

[root@bogon mnt]# vim for.sh

#!/bin/bash
for ((i=1;i<=9;i++))
do
echo $i
done
[root@bogon mnt]# ./for.sh

用户简单登录的测试

[root@bogon mnt]# vim mingling.sh

#!/bin/bash
username='jason'
password=''
tag='true'
while $tag
do
read -p 'username:' name
read -p 'password:' passwd
if [[ $name = $username ]] && [[ $passwd = $password ]];then
echo 'login sucessful'
while $tag
do
read -p '>>> :' cmd
if
[[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done [root@bogon mnt]# chmod +x mingling.sh
[root@bogon mnt]# ./mingling.sh

 九九乘法表

[root@bogon mnt]# vim chengfa.sh
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[j*i] "
done
echo
done [root@bogon mnt]# chmod +x chengfa.sh
[root@bogon mnt]# ./chengfa.sh

shell脚本结构的更多相关文章

  1. Shell脚本、Shell脚本结构、date命令的用法、变量

    1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构:   #!/bin/bash  以#!/bin/bash开头,即以/bin/ba ...

  2. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  3. shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...

  4. Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...

  5. shell脚本结构示例1

    2013年以来自己因为偷懒,少写了很多东西,今年计划把以前积累的总结出来. 先从shell开始写起吧. 干了快3年游戏运维,期间经常会写一些shell本,不少脚本其实有很多可以复用的部分. 按照自己的 ...

  6. shell脚本结构化语句

    本文中记录一下shell中的两种循环语句:for和while for循环 for循环是linux shell中最常用的结构,for循环有三种结构:1.列表for循环.2.不带列表for循环.3.C风格 ...

  7. shell脚本介绍
 shell脚本结构和执行
date命令用法
 shell脚本中的变量

  8. 第三部分shell编程3(shell脚本编写1)

    做监控和备份最多 1. shell脚本是什么它是一种脚本语言,并非编程语言可以使用一些逻辑判断.循环等语法可以自定义子函数是系统命令的集合shell脚本可以实现自动化运维,大大增加我们的工作效率 第一 ...

  9. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

随机推荐

  1. [Python] Print input and output in table

    Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...

  2. 如何相互转换逗号分隔的字符串和List【转】

    将逗号分隔的字符串转换为List 方法 1: 利用JDK的Arrays类 String str = "a,b,c"; List<String> result = Arr ...

  3. 如何在java List中进行模糊查询

    比如我有下面这样一个List,里面存放的是多个Employee对象.然后我想对这个List进行按照Employee对象的名字进行模糊查询.有什么好的解决方案么? 比如我输入的查询条件为“wang”,那 ...

  4. linux下git服务器安装

    git服务器配置http://www.cnblogs.com/dee0912/p/5815267.html git教程https://www.liaoxuefeng.com/wiki/00137395 ...

  5. HTML常用标签定义,用法及例子

    1.HTML注释 <!--...--> 定义:使用注释可对代码进行解释,不会显示在浏览器中. <!--这是一段呢注释.注释不会在浏览器中显示.--> <p>段落标签 ...

  6. python报错之OSError

    刚刚练一下xlrd模块的时候,竟然报错了! 百度了一下,才知道,是自己直接复制粘贴导致的:最好直接搜索\u202a才找到解决办法 所以只需从写换个地方复制路径或则重新手动输入一次就解决了 参考链接:h ...

  7. Linux基础命令---lpstat查看打印任务

    lpstat lpstat指令用来显示当前任务.打印机的状态.如果没有参数,那么就显示打印队列. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.openSUSE. ...

  8. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  9. Vue基础进阶 之 实例方法--生命周期

    在上一篇博客中我们知道生命周期的方法: 生命周期: vm.$mount:手动挂载Vue实例: vm.$destroy:销毁Vue实例,清理数据绑定,移除事件监听: vm.$nextTick:将方法中的 ...

  10. ORA-55617解决方法

    昨天一测试环境出现异常ORA-55617: Flashback Archive "XXXXX" runs out of space and tracking on "XX ...