加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell编程—for循环

发布时间:2020-12-15 23:12:57 所属栏目:安全 来源:网络整理
导读:shell循环 shell循环的分类 1、for 2、while 3、until for循环结构 for 变量 in 列表; do 循环体done 1、求1加到100的和 #!/bin/bash# sum of 1 to 100Sum=0for i in {1..100};do Sum=$(($Sum+$i))doneecho "Sum is $Sum" 2、依次向/etc/passwd中的每个用户
shell循环

shell循环的分类

1、for

2、while

3、until

for循环结构

for 变量 in 列表; do 
    循环体
done

1、求1加到100的和

#!/bin/bash
# sum of 1 to 100

Sum=0
for i in {1..100};do
        Sum=$(($Sum+$i))
done
echo "Sum is $Sum"

2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,例如:
Hello,root,your shell: /bin/bash

#!/bin/bash
#

UserNum=`wc -l /etc/passwd | cut -d‘ ‘ -f1`

for i in `seq 1 $UserNum`; do
        UserName=`head -$i /etc/passwd | tail -1 | cut -d‘:‘ -f1`
        UserShell=`head -$i /etc/passwd| tail -1 |cut -d‘:‘ -f7`
        echo "Hello,$UserName,your shell: $UserShell"
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读