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. ubuntu常用软件命令

    解压zip软件 unzip  xxx.zip -d解压到指定目录 清理磁盘空间 sudo apt-get autoremove sudo apt-get clean sudo dpkg --list ...

  2. JavaScript知识精简

      JS单线程,同步,一次执行某一段代码,等到前一个程序执行完毕再执行.,阻塞,安全. 多线程,异步,不用等到前一个程序执行完毕就执行. 数据类型 JavaScript 是 弱类型 语言,但并不是没有 ...

  3. qemu 对虚机的地址空间管理

    转载:http://huchh.com/2015/06/22/qemu-%E5%AF%B9%E8%99%9A%E6%9C%BA%E7%9A%84%E7%BA%BF%E6%80%A7%E5%9C%B0% ...

  4. 虚拟机下Linux安装jdk

    1.利用共享文件夹复制本地硬盘下(H:/share)的压缩包到指定目录 cp jdk-8u161-linux-x64.tar.gz /soft/jdk 2.进入/soft/jdk目录下,解压jdk到当 ...

  5. shell批量修改mysql用户密码

    需求 现在有这么一个需求, 需要大批量修改用户的密码, 需要注意的规则是: 必须添加的字符: *$#sjdKw% 用户名的第一位+*$#sjdKw%+用户名的最后一位,比如用户名chenglee,密码 ...

  6. 算法(第四版)C# 习题题解——1.1

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 善用 Ctrl + F 查找题 ...

  7. 批量操作RunTime之获取的Dic换成Model

    方法一: // // AlinkDeviceInfo.m //// // Created by Vivien on 2018/10/12. // Copyright © 2018年 . All rig ...

  8. Codeforces 939C - Convenient For Everybody

    2018-03-03 http://codeforces.com/problemset/problem/939/C C. Convenient For Everybody time limit per ...

  9. Codeforces Round #466 (Div. 2) -A. Points on the line

    2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...

  10. 彻底删除windows残留启动引导

    在win7/Win8系统下安装其他系统或者一键重装系统后,安装的系统删除或者一键重装文件删除了,在windows启动管理器中还残留了启动引导选项,影响开机效率. 在系统配置中有些"引导&qu ...