Linux shell脚本学习和实战之一


1、在/test目录下使用for循环创建10个文件如:test-1,test-2,...test-10
mkdirsh.sh
#!/bin/bash
for i in `seq 10`
do
[ ! -d /test ] && mkdir -p /test
touch /test/test-$i
done
chmod +x mkdirsh.sh

2、在/test目录下快速创建10个文件的方法如:test-1,test-2,...test-10
[root@Titu-BackupFS test]# touch test-{1..10}
[root@Titu-BackupFS test]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-1
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-10
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-2
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-3
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-4
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-5
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-6
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-7
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-8
-rw-r--r-- 1 root root 0 Mar 30 10:36 test-9

3、将以上文件名的test改为bruce(用for循环)
renamesh.sh
#!/bin/bash
[ -d /test ] && cd /test
for file in `ls test*`
do
mv $file `echo $file | sed 's#test#bruce#g' `
done
chmod +x renamesh.sh


4、将以上文件名的bruce快速改为network

[root@Titu-BackupFS test]# rename test network *bruce*
[root@Titu-BackupFS test]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-1
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-10
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-2
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-3
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-4
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-5
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-6
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-7
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-8
-rw-r--r-- 1 root root 0 Mar 30 10:36 network-9

5、批量创建10个系统帐号test01,test02,...test10并设置密码
(1)、首先理解序号数字的生成方法
[root@Titu-BackupFS test]# echo {01..10}
01 02 03 04 05 06 07 08 09 10

[root@Titu-BackupFS test]# seq 10
1
2
3
4
5
6
7
8
9
10

[root@Titu-BackupFS test]# seq -w 10
01
02
03
04
05
06
07
08
09
10


(2)、基于序号数字来依次生成用户帐户和密码信息
createusersh.sh
#!/bin/bash
for i in `seq -w 10`
do
useradd test$i && echo "test$i" | passwd --stdin test$i
done
chmod +x createusersh.sh
[root@Titu-BackupFS ~]# ./createusersh.sh
Changing password for user test01.
passwd: all authentication tokens updated successfully.
Changing password for user test02.
passwd: all authentication tokens updated successfully.
Changing password for user test03.
passwd: all authentication tokens updated successfully.
Changing password for user test04.
passwd: all authentication tokens updated successfully.
Changing password for user test05.
passwd: all authentication tokens updated successfully.
Changing password for user test06.
passwd: all authentication tokens updated successfully.
Changing password for user test07.
passwd: all authentication tokens updated successfully.
Changing password for user test08.
passwd: all authentication tokens updated successfully.
Changing password for user test09.
passwd: all authentication tokens updated successfully.
Changing password for user test10.
passwd: all authentication tokens updated successfully.
[root@Titu-BackupFS ~]# cat /etc/passwd | grep test*
test01:x:502:502::/home/test01:/bin/bash
test02:x:503:503::/home/test02:/bin/bash
test03:x:504:504::/home/test03:/bin/bash
test04:x:505:505::/home/test04:/bin/bash
test05:x:506:506::/home/test05:/bin/bash
test06:x:507:507::/home/test06:/bin/bash
test07:x:508:508::/home/test07:/bin/bash
test08:x:509:509::/home/test08:/bin/bash
test09:x:510:510::/home/test09:/bin/bash
test10:x:511:511::/home/test10:/bin/bash
说明:生成test01,test02,...test10用户成功!


6、批量删除用户
userdelsh.sh
#!/bin/bash
for username in `cat /etc/passwd| grep test*|awk -F: '{print $1}'`
do
userdel -r $username
done
chmod +x userdelsh.sh
[root@Titu-BackupFS ~]# ./userdelsh.sh
[root@Titu-BackupFS ~]# cat /etc/passwd|grep test*
说明:删除test01,test02,...test10用户成功!


7、批量创建10个帐号bruce01,bruce02,...bruce10并设置随机密码
createusernew.sh
#!/bin/bash
for i in `seq -w 10`
do
passwd=`echo $RANDOM | md5sum | cut -c 1-8`
##注意:将随机数定义为变量后,在后续引用过程中其值保持不变!
useradd bruce$i && echo "$passwd" | passwd --stdin bruce$i
echo -e "user:bruce$i \t password:$passwd" >> /tmp/userandpw.log
done
chmod +x createusernew.sh

[root@Titu-BackupFS ~]# ./createusernew.sh
Changing password for user bruce01.
passwd: all authentication tokens updated successfully.
Changing password for user bruce02.
passwd: all authentication tokens updated successfully.
Changing password for user bruce03.
passwd: all authentication tokens updated successfully.
Changing password for user bruce04.
passwd: all authentication tokens updated successfully.
Changing password for user bruce05.
passwd: all authentication tokens updated successfully.
Changing password for user bruce06.
passwd: all authentication tokens updated successfully.
Changing password for user bruce07.
passwd: all authentication tokens updated successfully.
Changing password for user bruce08.
passwd: all authentication tokens updated successfully.
Changing password for user bruce09.
passwd: all authentication tokens updated successfully.
Changing password for user bruce10.
passwd: all authentication tokens updated successfully.
[root@Titu-BackupFS ~]# su - bruce01
[bruce01@Titu-BackupFS ~]$ cat /tmp/userandpw.log
user:bruce01     password:277e5794
user:bruce02     password:e0ed3fdb
user:bruce03     password:1936a6d4
user:bruce04     password:dd10be6e
user:bruce05     password:842e552b
user:bruce06     password:ed8d2b54
user:bruce07     password:50da7bcf
user:bruce08     password:0463a374
user:bruce09     password:f29e9d10
user:bruce10     password:6e88cb6a
[bruce01@Titu-BackupFS ~]$ su - bruce02
Password:
[bruce02@Titu-BackupFS ~]$ exit
说明:批量创建用户并生成随机数密码验证并成功登录!

你可能感兴趣的:(linux,shell,脚本学习)