sdxh0506 发表于 2018-8-30 08:14:38

15_Shell语言———for循环实例演示(二)

  例6:写一个脚本,使用for循环显示用户userA、userB和userC各自的id号;
  使用 id -u 命令可以显示用户的id号,故此脚本可以写成:
  # nano showID.sh
#!/bin/bash  

  
for I in A B C; do
  
id -u user$I
  
done
  
unset I
  —————————————执行结果—————————————
# ./showID.sh  
3006
  
3007
  
3008
  如果能指明ID号具体属于哪个用户,用户体验会更好,故可对此脚本加以改进:
#!/bin/bash  

  
for I in A B C; do
  
echo "user$I: `id -u user$I`"
  
done
  
unset I
  —————————————执行结果—————————————
# ./showID.sh  
userA: 3006
  
userB: 3007
  
userC: 3008
  例7:写一个脚本,使用for循环分别统计/usr、/var、/bin、/sbin目录下各自的文件个数;
  使用ls 命令可以列出目录下的所有文件,在将结果通过管道传递给wc命令,即可以计算出文件个数,故此脚本可以写成:
  # nano countDir.sh
#!/bin/bash  

  
for Dir in /usr /var /bin /sbin;do
  
echo -e “\t$Dir: `ls $Dir | wc -l`”
  
done
  
unset Dir
  —————————————执行结果—————————————
  
# ./countDir.sh
  
      /usr: 12
  
      /var: 21
  
      /bin: 113
  
      /sbin: 284
  例8:写一个脚本, 使用for循环,分别将userA、userB和userC加入到testgrp组中,以其为额外组;
  testgrp这个组不存在,故需要先建立该组。建立该组应该在循环体外面,因为如果建在循环体里面,该组将被创建三次,这是不合理的,故此脚本可以写成:
  # nano testgrpAdd.sh
#!/bin/bash  
groupadd testgrp
  

  
for User in userA userB userC; do
  
usermod -a -G testgrp $User
  
done
  
unset User
  —————————————执行结果—————————————
# ./testgrpAdd.sh  
# id userA
  
uid=3006(userA) gid=3006(userA) groups=3006(userA),5002(testgrp)
  
# id userB
  
uid=3007(userB) gid=3007(userB) groups=3007(userB),5002(testgrp)
  
# id userC
  
uid=3008(userC) gid=3008(userC) groups=3008(userC),5002(testgrp)
  例9:写一个脚本,使用for循环,将"Hello Linux","Hello World!", "GNU is Not Unix". 三行内容添加至/tmp/test.txt文件;添加完成后,显示/tmp/test.txt文件的内容;
  这里可以将要显示的内容作为变量进行循环;向已存在的文件中追加内容可以使用 >>, 故此脚本可以写成:
  # nano showHello.sh
#!/bin/bash  
for Line in "Hello Linux" "Hello World!" "GNU is Not Unix"; do
  
echo $Line >> /tmp/test.txt
  
done
  

  
cat /tmp/test.txt
  
unset Line
  —————————————执行结果—————————————
# ./showHello.sh  
Hello Linux
  
Hello World!
  
GNU is Not Unix
  例10:写一个脚本,使用for循环,复制/etc/fstab、/etc/inittab和/etc/rc.d/init.d/functions文件至/tmp目录中,并重命名为原有的名称之后加当前日期,如第一个文件为/tmp/fstab-2013-07-15;
  重命名为原有名称之后加当前日期,可以使用 “date+%F”的形式,故此脚本可以写成:
  # nano cpFile.sh
#!/bin/bash  
for File in /etc/fstab /etc/inittab /etc/rc.d/init.d/functions; do
  
cp $File /tmp$File-`date +%F`
  
done
  
unset File
  ———————————————执行结果——————————————
# ./cpFile.sh  
# ll /tmp/etc
  
total 12
  
-rw-r--r--. 1 root root860 May 12 10:41 fstab-2014-05-12
  
-rw-r--r--. 1 root root884 May 12 10:41 inittab-2014-05-12
  
drwxr-xr-x. 3 root root 4096 May 12 10:41 rc.d
  
# ll /tmp/etc/rc.d/init.d
  
total 20
  
-rw-r--r--. 1 root root 18586 May 12 10:41 functions-2014-05-12
  
#
  执行上述脚本时有可能报错,因为File变量保存的是路径,复制后仍然是路径中的名字,如果 /tmp没有/etc目录及其子目录,将无法保存。如果对该脚本进行改进,在复制时加上文件的基名,就能解决这个问题
#!/bin/bash  
for File in /etc/fstab /etc/inittab /etc/rc.d/init.d/functions; do
  
FileName=`basename $File`
  
cp $File /tmp/$FileName-`date +%F`
  
done
  
unset FileName
  
unset File
  ———————————————执行结果——————————————
# ./cpFile.sh  
# ll /tmp
  
drwxr-xr-x. 3 root      root       4096 May 12 10:41 etc
  
-rw-r--r--. 1 root      root      860 May 12 10:53 fstab-2014-05-12
  
-rw-r--r--. 1 root      root      18586 May 12 10:53 functions-2014-05-12
  
-rw-r--r--. 1 root      root      884 May 12 10:53 inittab-2014-05-12
  如果只考虑/etc/fstab /etc/inittab这两个目录,可以将上述脚本简单写成以下形式:
#!/bin/bash  
for File in fstab inittab; do
  
cp /etc/$File /tmp/$File-`date +%F`
  
done
  
unset File
  ———————————————执行结果——————————————
# ll /tmp  
drwxr-xr-x. 3 root      root       4096 May 12 10:41 etc
  
-rw-r--r--. 1 root      root      860 May 12 10:57 fstab-2014-05-12
  
-rw-r--r--. 1 root      root      884 May 12 10:57 inittab-2014-05-12
  例11:写一个脚本,显示当前系统上所有默认shell为/bin/bash的用户名、ID号及其在/etc/passwd文件中的行号;
  使用grep -n “/bin/bash” /etc/passwd 可以带行号显示该文件的所有以/bin/bash 为默认shell的行,然后再使用cut -d: -f命令切割出相应的段。此脚本如下:
  # nano showBash.sh
#!/bin/bash  
for Bash in `grep -n "/bin/bash" /etc/passwd`;do
  
      echo $Bash >> bash_user_id_line.txt
  
done
  
#注意这里创建了一个临时文件bash_user_id_line.txt供cut命令使用
  
cut -d: -f1,2,4 bash_user_id_line.txt
  
rm bash_user_id_line.txt
  
# 当cut命令使用完临时文件后删除之
  
unset Bash
  ———————————————执行结果——————————————
# ./showBash.sh  
1:root:0
  
34:centos:500
  
35:hbase:503
  
36:openstack:504
  
37:hadoop:506
  
38:ubuntu:1000
  
39:debian:1001
  
40:gentoo:3000
  
Distribution:/home/gentoo
  
41:slackware:1003
  
42:archlinux:2000
  
Weight
  
Distribution:/home/archlinux
  
43:sysuser:496
  
44:sysuser1:495
  
46:moregrp:2002
  
47:redis:2004
  
48:mandriva:2008
  
51:tom:3002
  
54:user2:3004
  
55:user3:3005
  
56:userA:3006
  
57:userB:3007
  
58:userC:3008
  
1:root:0
  
34:centos:500
  
35:hbase:503
  
36:openstack:504
  
37:hadoop:506
  
38:ubuntu:1000
  
39:debian:1001
  
40:gentoo:3000
  
Distribution:/home/gentoo
  
41:slackware:1003
  
42:archlinux:2000
  
Weight
  
Distribution:/home/archlinux
  
43:sysuser:496
  
44:sysuser1:495
  
46:moregrp:2002
  
47:redis:2004
  
48:mandriva:2008
  
51:tom:3002
  
54:user2:3004
  
55:user3:3005
  
56:userA:3006
  
57:userB:3007
  
58:userC:3008
  例12:写一个脚本,显示/etc/passwd文件中第1、3、6、12个用户的用户名、ID和shell;
  如果要显示具体哪一行文本,可以使用head -Num 命令选出前Num行,然后将结果传递给tail -1命令,即能显示指定的行,随后使用cut命令切割出具体的字段; 为了使用户体验更好,可以将每个用户的每个字段独立存为一个变量,分别显示,此脚本书写如下:
  # nano show_num_line.sh
for Line in 1 3 6 12; do  
UserName=`head -$Line /etc/passwd | tail -1 | cut -d: -f1`
  
Uid=`head -$Line /etc/passwd | tail -1 | cut -d: -f3`
  
Shell=`head -$Line /etc/passwd | tail -1 | cut -d: -f7`
  

  
echo "$Line User: $UserName, UID is $Uid, Shell: $shell"
  
done
  
unset UserName
  
unset Uid
  
unset Shell
  
unset Line
  ———————————————执行结果——————————————
# ./show_num_line.sh  
1 User: root, UID is 0, Shell:
  
3 User: daemon, UID is 2, Shell:
  
6 User: sync, UID is 5, Shell:
  
12 User: games, UID is 12, Shell:
  例13:写一个脚本, 显示/etc/passwd文件中第1、3、6、12个用户的用户名、ID和基本组的组名;
  此例子和上一个例子相似,不同的是取基本组的组名,可以使用 id -gn 命令
  # nano show_user_grp.sh
#!/bin/bash  

  
for Line in 1 3 6 12; do
  
UserName=`head -$Line /etc/passwd | tail -1 | cut -d: -f1`
  
Uid=`head -$Line /etc/passwd | tail -1 | cut -d: -f3`
  
GroupName=`id -gn $UserName`
  
echo "$Line $UserName, $Uid, $GroupName"
  
done
  ———————————————执行结果——————————————
# ./show_user_grp.sh  
1 root, 0, root
  
3 daemon, 2, daemon
  
6 sync, 5, root
  
12 games, 12, users


页: [1]
查看完整版本: 15_Shell语言———for循环实例演示(二)