标签:until
脚本编程控制结构
顺序
选择
if
case
循环
for
while
until
while格式:
while CONDITION;do
statment
done
进入循环:条件满足
退出循环:条件不满足
until 格式:
until CONDITION;do
statment
done
进入循环:条件不满足
退出循环:条件满足
#!/bin/bash # read -p "Input Something:" STRING until [ $STRING == ‘quit‘ ]; do echo $STRING | tr ‘a-z‘ ‘A-Z‘ read -p "Input Something:" STRING done
#!/bin/bash # until who | grep "root" &> /dev/null ;do echo " root not login." sleep 5 done echo "root is login."
for的结构:
1、
for 变量 in ;do
循环体
done
2、
for ((expr1;expr2;expr3));do
循环体
done
#!/bin/bash # declare SUM=0 for I in {1..100};do let SUM+=$I done echo $SUM declare SUM2=0 for ((J=1;J<=100;J++));do let SUM2+=$J done echo $SUM2
使用for语句的两种格式,计算1到100之间的算数
#!/bin/bash #for2.sh declare SUM=0 for ((I=2;I<=100;I+=2));do let SUM+=$I done echo $SUM
计算100以内偶数的和
awk ‘pattern{action}‘ file
print $1
$NF:最后一个字段
-F: 指定分隔符为:
fdisk -l 2>/dev/null | grep "^Disk /dev/[sh]d[a-z]" | awk -F: ‘{print $1}‘
找出服务器上的所有硬盘
写一个脚本(前提:请为虚拟机新增一块硬盘,假设它为/dev/sdb),为指定的硬盘创建分区:
1、列出当前系统上所有的磁盘,让用户选择,如果选择quit则退出脚本;如果用户选择错误,就让用户重新选择;
2、当用户选择后,提醒用户确认接下来的操作可能会损坏数据,并请用户确认;如果用户选择y就继续,n就退出;否则,让用户重新选择;
3、抹除那块硬盘上的所有分区(提示,抹除所有分区后执行sync命令,并让脚本睡眠3秒钟后再分区);并为其创建三个主分区,第一个为20M,第二个为512M, 第三个为128M,且第三个为swap分区类型;(提示:将分区命令通过echo传送给fdisk即可实现)
#!/bin/bash # echo -e "\033[31mInitial a disk ...\033[0m" echo -e "\033[31mWARNING....\033[0m" echo "You have the following disk" fdisk -l | grep "^Disk /dev/[sh]d[a-z]" | awk -F: ‘{print $1}‘ | awk ‘{print $2}‘ read -p "Enter your choice:" CHOICE if [ $CHOICE == ‘quit‘ ];then echo "Quting...." exit 0 fi until fdisk -l | grep "^Disk /dev/[sh]d[a-z]" | grep "^Disk $CHOICE" &> /dev/null;do read -p "wrong option,Enter your choice aging:" CHOICE done read -p "Will destory all format data,continue? Y/N:" CHOICE2 until [ $CHOICE2 == ‘Y‘ -o $CHOICE2 == ‘N‘ ];do read -p "Will destory all format data,continue? Y/N:" CHOICE2 done if [ $CHOICE2 == ‘N‘ ];then echo "Quiting...." exit 7 else dd if=/dev/zero of=$CHOICE bs=512 count=1 &> /dev/null sync sleep 3 echo ‘n p 1 +20M n p 2 +150M n p 3 +128M t 3 82 w‘ | fdisk $CHOICE &> /dev/null sleep 1 sync mke2fs -j ${CHOICE}1 &> /dev/null sleep 2 mke2fs -j ${CHOICE}2 &> /dev/null sleep 2 mkswap ${CHOICE}3 &> /dev/null fi
标签:until
原文地址:http://leozhenping.blog.51cto.com/10043183/1639748