for 循环练习

发布时间 2024-01-02 22:43:48作者: 胖虎小夫
1、添加10个用户user1-user10,密码为8位随机字符

[root@srehost scripts]#cat FOR_USER.sh
#!/bin/bash
for ((i=1;i<=10;i++));do
useradd USER${i}
PASSWORD=`cat /dev/urandom |tr -d -c "[:alnum:]"| head -c 8`
echo "${PASSWORD}" | passwd --stdin USER${i}
echo "USER${i}:${PASSWORD}" | tee -a USER.LOG
done
[root@srehost scripts]#

[root@srehost scripts]#sh FOR_USER.sh
Changing password for user USER1.
passwd: all authentication tokens updated successfully.
USER1:ZivvypY3
Changing password for user USER2.
passwd: all authentication tokens updated successfully.
USER2:K4AJIZzm
Changing password for user USER3.
passwd: all authentication tokens updated successfully.
USER3:l1T1cGHk
Changing password for user USER4.
passwd: all authentication tokens updated successfully.
USER4:Goz83snZ
Changing password for user USER5.
passwd: all authentication tokens updated successfully.
USER5:UKEXmR3y
Changing password for user USER6.
passwd: all authentication tokens updated successfully.
USER6:F8Wrhsge
Changing password for user USER7.
passwd: all authentication tokens updated successfully.
USER7:WhyiyFVS
Changing password for user USER8.
passwd: all authentication tokens updated successfully.
USER8:S3hI1oB7
Changing password for user USER9.
passwd: all authentication tokens updated successfully.
USER9:EjTov4As
Changing password for user USER10.
passwd: all authentication tokens updated successfully.
USER10:XpaFNiwZ

[root@srehost scripts]#

 

==========================================================================

 

2  判断/var/目录下所有文件的类型

[root@srehost scripts]#cat FILETYPE.sh
#!/bin/bash
read -p "请输入目录名: " DIR
[ -d $DIR ] || { echo "请正确输入目录名称!!"; }
cd $DIR
for FILE in *; do
PRE=`ls -ld ${FILE} | egrep -o "^."`
case $PRE in
d)
echo "${FILE} 是个目录"
;;
l)
echo "${FILE} 是个链接文件"
;;
p)
echo "${FILE} 是个管道文件"
;;
b)
echo "${FILE} 是个块设备"
;;
c)
echo "${FILE} 是个字符设备"
;;
s)
echo "${FILE} 是个安全套接字"
;;
-)
echo "${FILE} 是个普通文件"
;;
*)
echo "other"
esac
done
[root@srehost scripts]#cat filetype.sh
#!/bin/bash
#!/bin/bash
read -p "请输入目录名称: " DIR
[ -d $DIR ] || { echo "请正确输入目录名称!!"; }
cd $DIR

for FILE in * ;do
PREFIX=`ls -ld ${FILE} |grep -E -o "^."`
if [ "${PREFIX}" = 'd' ]; then
echo "${FILE} 是个目录"
elif [ "${PREFIX}" = 'l' ]; then
echo "${FILE} 是个链接文件"
elif [ "${PREFIX}" = 'b' ]; then
echo "${FILE} 是个块文件"
elif [ "${PREFIX}" = 'c' ]; then
echo "${FILE} 是个字符设备"
elif [ "${PREFIX}" = 'p' ]; then
echo "${FILE} 是个管道文件"
elif [ "${PREFIX}" = 's' ]; then
echo "${FILE} 是个安全套接字"
else
echo "${FILE} 是普通文件"
fi
done
[root@srehost scripts]#

[root@srehost scripts]#sh filetype.sh
请输入目录名称: /var
account 是个目录
adm 是个目录
cache 是个目录
crash 是个目录
db 是个目录
empty 是个目录
ftp 是个目录
games 是个目录
gopher 是个目录
kerberos 是个目录
lib 是个目录
local 是个目录
lock 是个链接文件
log 是个目录
mail 是个链接文件
nis 是个目录
opt 是个目录
preserve 是个目录
run 是个链接文件
spool 是个目录
tmp 是个目录
www 是个目录
yp 是个目录
[root@srehost scripts]#ll /var/
total 12
drwxr-xr-x. 2 root root 19 Dec 3 19:13 account
drwxr-xr-x. 2 root root 6 Jun 22 2021 adm
drwxr-xr-x. 17 root root 215 Dec 9 22:19 cache
drwxr-xr-x. 2 root root 6 Aug 28 2021 crash
drwxr-xr-x. 3 root root 18 Dec 3 19:09 db
drwxr-xr-x. 3 root root 18 Dec 3 19:13 empty
drwxr-xr-x. 3 root root 17 Dec 19 21:15 ftp
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 gopher
drwxr-xr-x. 3 root root 18 Dec 3 19:07 kerberos
drwxr-xr-x. 64 root root 4096 Dec 9 22:19 lib
drwxr-xr-x. 2 root root 6 Jun 22 2021 local
lrwxrwxrwx. 1 root root 11 Dec 3 19:04 lock -> ../run/lock
drwxr-xr-x. 18 root root 4096 Jan 2 12:06 log
lrwxrwxrwx. 1 root root 10 Jun 22 2021 mail -> spool/mail
drwxr-xr-x. 2 root root 6 Jun 22 2021 nis
drwxr-xr-x. 2 root root 6 Jun 22 2021 opt
drwxr-xr-x. 2 root root 6 Jun 22 2021 preserve
lrwxrwxrwx. 1 root root 6 Dec 3 19:04 run -> ../run
drwxr-xr-x. 10 root root 109 Dec 4 11:31 spool
drwxrwxrwt. 5 root root 252 Jan 2 12:06 tmp
drwxr-xr-x. 4 root root 33 Dec 9 22:19 www
drwxr-xr-x. 2 root root 6 Jun 22 2021 yp

 

[root@srehost scripts]#sh FILETYPE.sh
请输入目录名: /dev
autofs 是个字符设备
block 是个目录
bsg 是个目录
bus 是个目录
cdrom 是个链接文件
char 是个目录
cl 是个目录
console 是个字符设备
core 是个链接文件
cpu 是个目录
cpu_dma_latency 是个字符设备
disk 是个目录
dm-0 是个块设备
dm-1 是个块设备
dm-2 是个块设备
dm-3 是个块设备
dm-4 是个块设备
dmmidi 是个字符设备
dri 是个目录
fb0 是个字符设备
fd 是个链接文件
full 是个字符设备
fuse 是个字符设备
hidraw0 是个字符设备
hpet 是个字符设备
hugepages 是个目录
hwrng 是个字符设备
initctl 是个链接文件
input 是个目录
kmsg 是个字符设备
log 是个链接文件
loop0 是个块设备
loop-control 是个字符设备
lp0 是个字符设备
lp1 是个字符设备
lp2 是个字符设备
lp3 是个字符设备
mapper 是个目录
mcelog 是个字符设备
mem 是个字符设备
midi 是个字符设备
mqueue 是个目录
my_vg 是个目录
net 是个目录
null 是个字符设备
nvram 是个字符设备
port 是个字符设备
ppp 是个字符设备
ptmx 是个字符设备
pts 是个目录
random 是个字符设备
raw 是个目录
rtc 是个链接文件
rtc0 是个字符设备
sda 是个块设备
sda1 是个块设备
sda2 是个块设备
sdb 是个块设备
sdc 是个块设备
sdd 是个块设备
sde 是个块设备
sdf 是个块设备
sdg 是个块设备
sg0 是个字符设备
sg1 是个字符设备
sg2 是个字符设备
sg3 是个字符设备
sg4 是个字符设备
sg5 是个字符设备
sg6 是个字符设备
sg7 是个字符设备
shm 是个目录
snapshot 是个字符设备
snd 是个目录
sr0 是个块设备
stderr 是个链接文件
stdin 是个链接文件
stdout 是个链接文件
tty 是个字符设备
tty0 是个字符设备
tty1 是个字符设备
tty10 是个字符设备
tty11 是个字符设备
tty12 是个字符设备
tty13 是个字符设备
tty14 是个字符设备
tty15 是个字符设备
tty16 是个字符设备
tty17 是个字符设备
tty18 是个字符设备
tty19 是个字符设备
tty2 是个字符设备
tty20 是个字符设备
tty21 是个字符设备
tty22 是个字符设备
tty23 是个字符设备
tty24 是个字符设备
tty25 是个字符设备
tty26 是个字符设备
tty27 是个字符设备
tty28 是个字符设备
tty29 是个字符设备
tty3 是个字符设备
tty30 是个字符设备
tty31 是个字符设备
tty32 是个字符设备
tty33 是个字符设备
tty34 是个字符设备
tty35 是个字符设备
tty36 是个字符设备
tty37 是个字符设备
tty38 是个字符设备
tty39 是个字符设备
tty4 是个字符设备
tty40 是个字符设备
tty41 是个字符设备
tty42 是个字符设备
tty43 是个字符设备
tty44 是个字符设备
tty45 是个字符设备
tty46 是个字符设备
tty47 是个字符设备
tty48 是个字符设备
tty49 是个字符设备
tty5 是个字符设备
tty50 是个字符设备
tty51 是个字符设备
tty52 是个字符设备
tty53 是个字符设备
tty54 是个字符设备
tty55 是个字符设备
tty56 是个字符设备
tty57 是个字符设备
tty58 是个字符设备
tty59 是个字符设备
tty6 是个字符设备
tty60 是个字符设备
tty61 是个字符设备
tty62 是个字符设备
tty63 是个字符设备
tty7 是个字符设备
tty8 是个字符设备
tty9 是个字符设备
ttyS0 是个字符设备
ttyS1 是个字符设备
ttyS2 是个字符设备
ttyS3 是个字符设备
uhid 是个字符设备
uinput 是个字符设备
urandom 是个字符设备
usbmon0 是个字符设备
usbmon1 是个字符设备
usbmon2 是个字符设备
vcs 是个字符设备
vcs1 是个字符设备
vcs2 是个字符设备
vcs3 是个字符设备
vcs4 是个字符设备
vcs5 是个字符设备
vcs6 是个字符设备
vcsa 是个字符设备
vcsa1 是个字符设备
vcsa2 是个字符设备
vcsa3 是个字符设备
vcsa4 是个字符设备
vcsa5 是个字符设备
vcsa6 是个字符设备
vfio 是个目录
vga_arbiter 是个字符设备
vhci 是个字符设备
vhost-net 是个字符设备
vhost-vsock 是个字符设备
vmci 是个字符设备
vsock 是个字符设备
zero 是个字符设备
[root@srehost scripts]#ll /dev
total 0
crw-r--r-- 1 root root 10, 235 Dec 24 03:41 autofs
drwxr-xr-x 2 root root 360 Dec 25 15:48 block
drwxr-xr-x 2 root root 200 Dec 24 03:41 bsg
drwxr-xr-x 3 root root 60 Dec 24 03:41 bus
lrwxrwxrwx 1 root root 3 Dec 24 03:41 cdrom -> sr0
drwxr-xr-x 2 root root 3300 Dec 25 15:48 char
drwxr-xr-x 2 root root 100 Dec 24 03:41 cl
crw------- 1 root root 5, 1 Dec 24 03:41 console
lrwxrwxrwx 1 root root 11 Dec 24 03:41 core -> /proc/kcore
drwxr-xr-x 6 root root 120 Dec 24 03:41 cpu
crw------- 1 root root 10, 62 Dec 24 03:41 cpu_dma_latency
drwxr-xr-x 7 root root 140 Dec 24 03:41 disk
brw-rw---- 1 root disk 253, 0 Dec 24 03:41 dm-0
brw-rw---- 1 root disk 253, 1 Dec 24 03:41 dm-1
brw-rw---- 1 root disk 253, 2 Dec 24 03:41 dm-2
brw-rw---- 1 root disk 253, 3 Dec 24 03:51 dm-3
brw-rw---- 1 root disk 253, 4 Dec 24 03:51 dm-4
crw-rw----+ 1 root audio 14, 9 Dec 24 03:41 dmmidi
drwxr-xr-x 3 root root 100 Dec 24 03:41 dri
crw-rw---- 1 root video 29, 0 Dec 24 03:41 fb0
lrwxrwxrwx 1 root root 13 Dec 24 03:41 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Dec 24 03:41 full
crw-rw-rw- 1 root root 10, 229 Dec 24 03:41 fuse
crw------- 1 root root 245, 0 Dec 24 03:41 hidraw0
crw------- 1 root root 10, 228 Dec 24 03:41 hpet
drwxr-xr-x 3 root root 0 Dec 24 03:41 hugepages
crw------- 1 root root 10, 183 Dec 24 03:41 hwrng
lrwxrwxrwx 1 root root 12 Dec 24 03:41 initctl -> /run/initctl
drwxr-xr-x 4 root root 280 Dec 24 03:41 input
crw-r--r-- 1 root root 1, 11 Dec 24 03:41 kmsg
lrwxrwxrwx 1 root root 28 Dec 24 03:41 log -> /run/systemd/journal/dev-log
brw-rw---- 1 root disk 7, 0 Dec 25 15:48 loop0
crw-rw---- 1 root disk 10, 237 Dec 25 15:48 loop-control
crw-rw---- 1 root lp 6, 0 Dec 24 03:41 lp0
crw-rw---- 1 root lp 6, 1 Dec 24 03:41 lp1
crw-rw---- 1 root lp 6, 2 Dec 24 03:41 lp2
crw-rw---- 1 root lp 6, 3 Dec 24 03:41 lp3
drwxr-xr-x 2 root root 160 Dec 24 03:41 mapper
crw------- 1 root root 10, 227 Dec 24 03:41 mcelog
crw-r----- 1 root kmem 1, 1 Dec 24 03:41 mem
crw-rw----+ 1 root audio 14, 2 Dec 24 03:41 midi
drwxrwxrwt 2 root root 40 Dec 24 03:41 mqueue
drwxr-xr-x 2 root root 80 Dec 24 03:41 my_vg
drwxr-xr-x 2 root root 60 Dec 24 03:41 net
crw-rw-rw- 1 root root 1, 3 Dec 24 03:41 null
crw------- 1 root root 10, 144 Dec 24 03:41 nvram
crw-r----- 1 root kmem 1, 4 Dec 24 03:41 port
crw------- 1 root root 108, 0 Dec 24 03:41 ppp
crw-rw-rw- 1 root tty 5, 2 Jan 2 22:37 ptmx
drwxr-xr-x 2 root root 0 Dec 24 03:41 pts
crw-rw-rw- 1 root root 1, 8 Dec 24 03:41 random
drwxr-xr-x 2 root root 60 Dec 24 03:41 raw
lrwxrwxrwx 1 root root 4 Dec 24 03:41 rtc -> rtc0
crw------- 1 root root 251, 0 Dec 24 03:41 rtc0
brw-rw---- 1 root disk 8, 0 Dec 24 03:41 sda
brw-rw---- 1 root disk 8, 1 Dec 24 03:41 sda1
brw-rw---- 1 root disk 8, 2 Dec 24 03:41 sda2
brw-rw---- 1 root disk 8, 16 Dec 24 03:45 sdb
brw-rw---- 1 root disk 8, 32 Dec 24 03:45 sdc
brw-rw---- 1 root disk 8, 48 Dec 24 03:41 sdd
brw-rw---- 1 root disk 8, 64 Dec 24 03:41 sde
brw-rw---- 1 root disk 8, 80 Dec 24 03:41 sdf
brw-rw---- 1 root disk 8, 96 Dec 24 03:41 sdg
crw-rw---- 1 root disk 21, 0 Dec 24 03:41 sg0
crw-rw---- 1 root disk 21, 1 Dec 24 03:41 sg1
crw-rw---- 1 root disk 21, 2 Dec 24 03:41 sg2
crw-rw---- 1 root disk 21, 3 Dec 24 03:41 sg3
crw-rw---- 1 root disk 21, 4 Dec 24 03:41 sg4
crw-rw---- 1 root disk 21, 5 Dec 24 03:41 sg5
crw-rw---- 1 root disk 21, 6 Dec 24 03:41 sg6
crw-rw----+ 1 root cdrom 21, 7 Dec 24 03:41 sg7
drwxrwxrwt 2 root root 40 Dec 24 03:41 shm
crw------- 1 root root 10, 231 Dec 24 03:41 snapshot
drwxr-xr-x 3 root root 200 Dec 24 03:41 snd
brw-rw----+ 1 root cdrom 11, 0 Dec 24 03:41 sr0
lrwxrwxrwx 1 root root 15 Dec 24 03:41 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Dec 24 03:41 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Dec 24 03:41 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root tty 5, 0 Dec 24 03:41 tty
crw--w---- 1 root tty 4, 0 Dec 24 03:41 tty0
crw--w---- 1 gdm tty 4, 1 Dec 24 03:41 tty1
crw--w---- 1 root tty 4, 10 Dec 24 03:41 tty10
crw--w---- 1 root tty 4, 11 Dec 24 03:41 tty11
crw--w---- 1 root tty 4, 12 Dec 24 03:41 tty12
crw--w---- 1 root tty 4, 13 Dec 24 03:41 tty13
crw--w---- 1 root tty 4, 14 Dec 24 03:41 tty14
crw--w---- 1 root tty 4, 15 Dec 24 03:41 tty15
crw--w---- 1 root tty 4, 16 Dec 24 03:41 tty16
crw--w---- 1 root tty 4, 17 Dec 24 03:41 tty17
crw--w---- 1 root tty 4, 18 Dec 24 03:41 tty18
crw--w---- 1 root tty 4, 19 Dec 24 03:41 tty19
crw--w---- 1 root tty 4, 2 Dec 24 03:41 tty2
crw--w---- 1 root tty 4, 20 Dec 24 03:41 tty20
crw--w---- 1 root tty 4, 21 Dec 24 03:41 tty21
crw--w---- 1 root tty 4, 22 Dec 24 03:41 tty22
crw--w---- 1 root tty 4, 23 Dec 24 03:41 tty23
crw--w---- 1 root tty 4, 24 Dec 24 03:41 tty24
crw--w---- 1 root tty 4, 25 Dec 24 03:41 tty25
crw--w---- 1 root tty 4, 26 Dec 24 03:41 tty26
crw--w---- 1 root tty 4, 27 Dec 24 03:41 tty27
crw--w---- 1 root tty 4, 28 Dec 24 03:41 tty28
crw--w---- 1 root tty 4, 29 Dec 24 03:41 tty29
crw--w---- 1 root tty 4, 3 Dec 24 03:41 tty3
crw--w---- 1 root tty 4, 30 Dec 24 03:41 tty30
crw--w---- 1 root tty 4, 31 Dec 24 03:41 tty31
crw--w---- 1 root tty 4, 32 Dec 24 03:41 tty32
crw--w---- 1 root tty 4, 33 Dec 24 03:41 tty33
crw--w---- 1 root tty 4, 34 Dec 24 03:41 tty34
crw--w---- 1 root tty 4, 35 Dec 24 03:41 tty35
crw--w---- 1 root tty 4, 36 Dec 24 03:41 tty36
crw--w---- 1 root tty 4, 37 Dec 24 03:41 tty37
crw--w---- 1 root tty 4, 38 Dec 24 03:41 tty38
crw--w---- 1 root tty 4, 39 Dec 24 03:41 tty39
crw--w---- 1 root tty 4, 4 Dec 24 03:41 tty4
crw--w---- 1 root tty 4, 40 Dec 24 03:41 tty40
crw--w---- 1 root tty 4, 41 Dec 24 03:41 tty41
crw--w---- 1 root tty 4, 42 Dec 24 03:41 tty42
crw--w---- 1 root tty 4, 43 Dec 24 03:41 tty43
crw--w---- 1 root tty 4, 44 Dec 24 03:41 tty44
crw--w---- 1 root tty 4, 45 Dec 24 03:41 tty45
crw--w---- 1 root tty 4, 46 Dec 24 03:41 tty46
crw--w---- 1 root tty 4, 47 Dec 24 03:41 tty47
crw--w---- 1 root tty 4, 48 Dec 24 03:41 tty48
crw--w---- 1 root tty 4, 49 Dec 24 03:41 tty49
crw--w---- 1 root tty 4, 5 Dec 24 03:41 tty5
crw--w---- 1 root tty 4, 50 Dec 24 03:41 tty50
crw--w---- 1 root tty 4, 51 Dec 24 03:41 tty51
crw--w---- 1 root tty 4, 52 Dec 24 03:41 tty52
crw--w---- 1 root tty 4, 53 Dec 24 03:41 tty53
crw--w---- 1 root tty 4, 54 Dec 24 03:41 tty54
crw--w---- 1 root tty 4, 55 Dec 24 03:41 tty55
crw--w---- 1 root tty 4, 56 Dec 24 03:41 tty56
crw--w---- 1 root tty 4, 57 Dec 24 03:41 tty57
crw--w---- 1 root tty 4, 58 Dec 24 03:41 tty58
crw--w---- 1 root tty 4, 59 Dec 24 03:41 tty59
crw--w---- 1 root tty 4, 6 Dec 24 03:41 tty6
crw--w---- 1 root tty 4, 60 Dec 24 03:41 tty60
crw--w---- 1 root tty 4, 61 Dec 24 03:41 tty61
crw--w---- 1 root tty 4, 62 Dec 24 03:41 tty62
crw--w---- 1 root tty 4, 63 Dec 24 03:41 tty63
crw--w---- 1 root tty 4, 7 Dec 24 03:41 tty7
crw--w---- 1 root tty 4, 8 Dec 24 03:41 tty8
crw--w---- 1 root tty 4, 9 Dec 24 03:41 tty9
crw-rw---- 1 root dialout 4, 64 Dec 24 03:41 ttyS0
crw-rw---- 1 root dialout 4, 65 Dec 24 03:41 ttyS1
crw-rw---- 1 root dialout 4, 66 Dec 24 03:41 ttyS2
crw-rw---- 1 root dialout 4, 67 Dec 24 03:41 ttyS3
crw------- 1 root root 10, 239 Dec 24 03:41 uhid
crw------- 1 root root 10, 223 Dec 24 03:41 uinput
crw-rw-rw- 1 root root 1, 9 Dec 24 03:41 urandom
crw------- 1 root root 246, 0 Dec 24 03:41 usbmon0
crw------- 1 root root 246, 1 Dec 24 03:41 usbmon1
crw------- 1 root root 246, 2 Dec 24 03:41 usbmon2
crw-rw---- 1 root tty 7, 0 Dec 24 03:41 vcs
crw-rw---- 1 root tty 7, 1 Dec 24 03:41 vcs1
crw-rw---- 1 root tty 7, 2 Dec 24 03:41 vcs2
crw-rw---- 1 root tty 7, 3 Dec 24 03:41 vcs3
crw-rw---- 1 root tty 7, 4 Dec 24 03:41 vcs4
crw-rw---- 1 root tty 7, 5 Dec 24 03:41 vcs5
crw-rw---- 1 root tty 7, 6 Dec 24 03:41 vcs6
crw-rw---- 1 root tty 7, 128 Dec 24 03:41 vcsa
crw-rw---- 1 root tty 7, 129 Dec 24 03:41 vcsa1
crw-rw---- 1 root tty 7, 130 Dec 24 03:41 vcsa2
crw-rw---- 1 root tty 7, 131 Dec 24 03:41 vcsa3
crw-rw---- 1 root tty 7, 132 Dec 24 03:41 vcsa4
crw-rw---- 1 root tty 7, 133 Dec 24 03:41 vcsa5
crw-rw---- 1 root tty 7, 134 Dec 24 03:41 vcsa6
drwxr-xr-x 2 root root 60 Dec 24 03:41 vfio
crw------- 1 root root 10, 63 Dec 24 03:41 vga_arbiter
crw------- 1 root root 10, 137 Dec 24 03:41 vhci
crw------- 1 root root 10, 238 Dec 24 03:41 vhost-net
crw------- 1 root root 10, 241 Dec 24 03:41 vhost-vsock
crw------- 1 root root 10, 61 Dec 24 03:41 vmci
crw------- 1 root root 10, 60 Dec 24 03:41 vsock
crw-rw-rw- 1 root root 1, 5 Dec 24 03:41 zero
[root@srehost scripts]#

 

=====================================================================