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. C++ const用法 尽可能使用const

    C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就应该明确使用const,这样可以获得编译器的帮助. 1.c ...

  2. Software Testing 1 —— 有关编程错误的经历

    最令我印象深刻的程序错误几乎都是那些细节,具体的记不清了,因为真的很细.他们不会报正常的错,要么是时而可以正常运行,时而不能正常运行但是没有报错,比如闪退或者持续运行没有输出:要么是报的错误意义很宽泛 ...

  3. TP无限回复

    引入文件和css样式 <script src="__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> ...

  4. wingIDE Pro6 破解教程

    亲测wingIDE pro6.0.6-1激活成功 算号器下载 激活的时候选择第三项 打开算号器,获得license id 把算号器里的license id输入到第一步的输入框里 continue得到r ...

  5. 【Linux】Tomcat安装及一个服务器配置多个Tomcat

    安装环境 :Linux(Ubuntu 版) 安装软件 : apache-tomcat-9.0.0.M1.tar.gz(下载地址http://tomcat.apache.org/) 步骤一 Tomcat ...

  6. liunx 常用命令学习笔记

    通过linux 命令pwd:显示当前所在的目录ls:显示当前目录下的文件cd:切换路径 cd..返回上一级路径mkdir:新建目录rmdir:删除目录 touch:新建文件rm:删除文件 gedit: ...

  7. pssh批量管理

    因为公司金融项目正式上线,有等保的要求,所有的线上服务器对操作过历史命令都要记录下来,需要修改一部分的配制文件.总共有300多台Linux服务器,总不能一台一台去改吧.首先想到是ansble,salt ...

  8. mysql获取随机字符串和随机数的方法

    在我们开发的过程中,我们可能会需要在表中随机生成一些数据以供我们进行相应的测试. 就像我之前发的“mysql创建存储过程向数据表中加入规定条数的数据” 那么我们应该怎样生成随机的字符串和随机数字呢? ...

  9. lvs 初始 第一章

    Linux Virtual Server 第一章  初识 一 . 介绍 LVS集群采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动 ...

  10. 剑指offer(30)连续子数组和的最大值

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...