linux的循环语句,linux _for循环语句

for:语法格式

for 变量 in 列表; do

循环语句

done

for I in 1 2 3 4 5;do

加法运算

done

遍历完成之后,退出:

如何生成列表:

{1..100}

declare  -i sum=0 定义sum为整形

-x 声明成环境变量

`seq [起始数 步进长度] 结束数`

#!/bin/bash

declare -i SUM=0

for I in {1..100};do

letSUM=$SUM+$I

done

echo “The sum is :$SUM.”

2.向每个用户问好

[root@localhost ~]# LINES=`wc -l/etc/passwd | cut -d':' -f1`

[root@localhost ~]# for I in `seq 1 $LINES`;do echo "Hello,`head -$I /etc/passwd |tail -1 |cut -d':' -f1`";done

写一个脚本,

1.设定变量FILE的值为/etc/passwd

2.向每个用户问好,并显示用户的shell。列如Hello,root,your shell :/bin/bash

#!/bin/bash

#

FILE=/etc/passwd

LINE=`wc -l $FILE |cut -d' ' -f1`

for I in `seq 1 $LINE`;do

echo "Hello,`head -$I /etc/passwd |tail -1 |cut -d':' -f1`,yourshell:`head -$I /etc/passwd |tail -1 |cut -d':' -f7`"

done

3 统计一共有多少个用户

只向默认shell 为bash的用户问声好

#!/bin/bash

#

LL=`sed -r -n '/bash/'p /etc/passwd |wc -l`

for I in `seq 1 $LL`; do

echo"Hello,`sed -r -n '/bash$/'p /etc/passwd | head -$I | tail -1 |cut -d':'-f1`"

done

写一个脚本

添加10个用户user1 到user10 ,但是要求只有用户不存在的情况下才能添加:

#!/bin/bash

#

for I in{1..10};do

iduser$I  &>/dev/null

if [ $? -eq 0 ]; then

echo "yonghucunzai"

else

useradd user$I &>/dev/null

echo "user$I" |passwd --stdinuser$I &>/dev/null

echo "add user$I OK"

fi

done

扩展:接受一个参数

Add :添加用户uer1..user10

Del : 删除用户:user1..user10

#!/bin/bash

#

if [ $#-eq 1 ];then

if [ $1 ='add' ];then

for I in {1..10};do

id user$I &>/dev/null

if [ $? -eq 0 ]; then

echo "yonghucunzai"

else

useradd user$I &>/dev/null

echo "user$I" |passwd--stdin user$I &>/dev/null

echo "add user$I OK"

fi

done

elif [ $1= 'del' ];then

for I in {1..10};do

id user$I &> /dev/null

if [ $? -eq 0 ];then

userdel -r  user$I

echo "del user OK"

else

echo "no such user"

exit 1

fi

done

else

echo " add&del"

fi

else

echo "qing churu yige canshu,del&add"

fi

写一个脚本 计算100以内所有能被3整除的正整数的和

#!/bin/bash

#

declare-i M

M=0

for I in{1..100};do

if [ `echo "$I%3"|bc` -eq 0];then

M=$I+$M

else

M=$M

fi

done

echo "sum=$M"

写一个脚本计算100以内所有奇数的和以及所有偶数的和;分别显示之;

#!/bin/bash

#

declare-i M

declare-i J

declare-i B

M=0

J=0

for I in{1..100};do

B=`echo "$I%2"|bc`

if [ $B -eq 0 ];then

M=$I+$M

else

J=$I+$J

fi

done

echo"oushu=$M;jishu=$J"

写一个脚本。分别显示当前系统上所有默认的shell 为bash 的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数显示结果如:

#!/bin/bash

#

M=`sed -n'/bash$/p' /etc/passwd |wc -l`

J=`sed -n'/\/sbin\/nologin$/p' /etc/passwd |wc -l`

for I in`seq 1 $M`;do

echo "`sed -n '/bash$/p' /etc/passwd|head -$I |tail -1 |cut -d':' -f1` is bash"

done

for I in`seq 1 $J`;do

echo "`sed -n '/\/sbin\/nologin$/p'/etc/passwd |head -$I |tail -1 |cut -d':' -f1` is /sbin/nologin"

done

echo "/bash=$M;/sbin/nologin=$J"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值