shell脚本结构
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脚本结构的更多相关文章
- Shell脚本、Shell脚本结构、date命令的用法、变量
1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构: #!/bin/bash 以#!/bin/bash开头,即以/bin/ba ...
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ...
- shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...
- Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...
- shell脚本结构示例1
2013年以来自己因为偷懒,少写了很多东西,今年计划把以前积累的总结出来. 先从shell开始写起吧. 干了快3年游戏运维,期间经常会写一些shell本,不少脚本其实有很多可以复用的部分. 按照自己的 ...
- shell脚本结构化语句
本文中记录一下shell中的两种循环语句:for和while for循环 for循环是linux shell中最常用的结构,for循环有三种结构:1.列表for循环.2.不带列表for循环.3.C风格 ...
- shell脚本介绍 shell脚本结构和执行 date命令用法 shell脚本中的变量
- 第三部分shell编程3(shell脚本编写1)
做监控和备份最多 1. shell脚本是什么它是一种脚本语言,并非编程语言可以使用一些逻辑判断.循环等语法可以自定义子函数是系统命令的集合shell脚本可以实现自动化运维,大大增加我们的工作效率 第一 ...
- 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令
许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then ...
随机推荐
- 第七篇——Struts2的接收参数
Struts2的接收参数 1.使用Action的属性接收参数 2.使用Domain Model接收参数 3.使用ModelDriven接收参数 项目实例 1.项目结构 2.pom.xml <pr ...
- js 星星效果思路
//星星的效果思路 1.获取需要修改的元素 ul li 跟p 布局 2.给li 加移入事件 更改提示框显示, 3.给li 加移出事件 更改提示框隐藏 4.给li加索引值代表自己的序号 5.在li移入时 ...
- LATCH_VARIANT = Magna compile
after: LATCH_VARIANT = Magna in m_project_cfg.mak of PG_.. 1. first error: SED tmp__.dep/bin/sh: / ...
- Docker Swarm volume 数据持久化
Docker Swarm volume 数据持久化 volume 是将宿主级的目录映射到容器中,以实现数据持久化. 可以用两种方式来实现: volume 默认模式:工作节点宿主机数据同步到容器内. v ...
- Linux 系统状态、常用快捷键
1.查看系统状态 ()df -h #查看分区状态. ()mount #查看文件系统类型. ()du -h -max-depth= 目录名 #查看目录文件大小,批量查找. ()du -sh #查看目录文 ...
- zabbix 配置维护
Centos 6.5, Zabbix 3.0.4 在配置了邮件报警后,如果正常的软硬件变更(比如发版)也不停的发邮件肯定很烦,这个时候就需要在操作前挂上维护: 浏览器登录zabbix后台,Config ...
- linux基础之awk
gawk - pattern scanning and processing language 基本用法: gawk [options] 'program' FILE... program: PATT ...
- python基础知识点四
网络编程(socket) 软件开发的架构: 两个程序之间通讯的应用大致通过从用户层面可以分为两种: 1是C/S,即客户端与服务端,为应用类的,比如微信,网盘等需要安装桌面应用的 2是B/S,即浏览器与 ...
- Java中的CAS实现原理
一.什么是CAS? 在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令. 它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新 ...
- sublime设置html在浏览器打开
1.快捷键 Ctrl+Shift+P输入:pcip选择第一个 2.输入:View In Browser安装此插件 3.菜单栏Preferences->Key Bindings 输入:[{ &q ...