1.if 是单分支语句,使用格式如下: if condition ; then statement ….. fi 2.if … else 是双分支语句,使用格式如下: if condition ; then
#!/bin/bash
declare -i sum=0
for I in {1..100}; do
if [ $[$I%3] -eq 0 ]; then
let sum+=$I
fi
done
echo " the sum is :$sum"
#!/bin/bash
# echo "exercise"
declare -i sum1=0
declare -i sum2=0
for I in {1..100}; do
if [ $[$I%2] -eq 0 ]; then
let sum1+=$I
else
let sum2+=$I
fi
done
echo " the even sum is :$sum1"
echo " the oddnumber sum is :$sum2"
#!/bin/bash
file1=/var/log/*
for file in $file1 ; do
if [ -f $file ]; then
echo "$file is common file"
elif [ -d $file ]; then
echo "$file is directory file"
else
echo "$file is unknow"
fi
done
#!/bin/bash
file=/etc/passwd
bsh='/bin/bash'
nobsh='/sbin/nologin'
use=`cat $file | cut -d: -f1`
declare -i d1=0
declare -i d2=0
for I in $use ; do
s=`grep "^$I:" $file | cut -d: -f7`
if [ "$s" = $bsh ] ; then
let d1=$d1+1
muser=$I,$muser
elif [ "$s" = $nobsh ] ; then
let d2=$d2+1
suser=$I,$suser
fi
done
echo "BASH,$d1 users ,they are:"
echo $muser
echo
echo "NOLOGIN,$d2 users ,they are:"
echo $suser
#!/bin/bash
file=/tmp/maintenace
if [ -e $file ]; then
rm -rf $file &> /dev/null
fi
touch $file
cat >> $file << EOF
172.16.0.6
172.16.0.17
172.16.0.20
EOF
bnet=172.16
for net in {0..254} ; do
for host in {1..254} ; do
if ping -c1 -W1 $bnet.$net.$host &> /dev/null ; then
echo "$bnet.$net.$host is up."
elif grep "$bnet.$net.$host$" $file &> /dev/null ;then
echo "$bnet.$net.$host is under maintenance."
else
echo "$bnet.$net.$host state is unknow."
fi
done
done
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo " Come on ,the user you input unexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your choice:" op
case $op in
U|u)
id -u $username;;
G|g)
id -g $username;;
S|s)
grep "^$username>" /etc/passwd | cut -d: -f7;;
Q|q)
exit 8 ;;
*)
echo "input option wrong ,quit"
exit 9
esac
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo "Come on ,you input user notexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your option:" op
while true; do
case $op in
U|u)
id -u $username
break
G|g)
id -g $username
break
S|s)
grep "^$username>" /etc/passwd | cut -d: -f7
break
Q|q)
exit 7 ;;
*)
read -p "Wrong option,Enter a right option:" op ;;
esac
done
#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && exit 2
until bash -n $1 &> /dev/null ; do
read -p " Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3
*)
vim $1
esac
done
#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && echo "Quit!" && exit 9
until bash -n $1 &> /dev/null ; do
read -p " Grammar wrong please enter Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3
*)
vim $1
bash -n $1 &> /dev/null
val=$?
[ "$val" -ne 0 ] && echo "xiu gai bu cheng Gong. "
esac
done
#!/bin/bash
[ ! -f $1 ] && echo "Wrong scripts." && exit 4
bash -n $1 &> /dev/null
valu=$?
until [ $valu -eq 0 ] ; do
read -p "Q|q to quit ,others to edit:" op
case $op in
Q|q)
echo "Quit."
exit 9
*)
vim $1
bash -n $1 &> /dev/null
valu=$?
esac
done
#!/bin/bash
who | grep "^redhat>" &> /dev/null
reval=$?
until [ $reval -eq 0 ] ;do
sleep 5
who | grep "^redhat>" &> /dev/null
reval=$?
done
echo "redhat is logged on."
#!/bin/bash
until who | grep "^redhat>" &> /dev/null ; do
sleep 5
done
echo "redhat is logged on"
#!/bin/bash
declare -i I=1
while [ $I -le 20 ] ; do
l=linuxer$I
let I++
! id $l &> /dev/null && useradd $l &> /dev/null && echo "the user:$l" | passwd --stdin $l &> /dev/null && echo "a dd user $l successfully" || echo " The user $l is exit. "
d=`id -u $l`
g=`id -g $l`
echo " $l ,UID:$d,GID:$g "
done
--结束END--
本文标题: bash 编程中循环语句用法
本文链接: https://www.lsjlt.com/news/18171.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
2024-04-29
2024-04-29
2024-04-18
2024-04-17
2024-04-11
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0