One of the Linux Shell Scripting Learning and combat

Source: Internet
Author: User


1. Create 10 files in the/test directory using a For loop such as: 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, in the/test directory to quickly create 10 file methods such as: Test-1,test-2,... test-10
[[email protected] test]# Touch test-{1..10}
[email protected] test]# LL
Total 0
-rw-r--r--1 root root 0 Mar 10:36 test-1
-rw-r--r--1 root root 0 Mar 10:36 test-10
-rw-r--r--1 root root 0 Mar 10:36 test-2
-rw-r--r--1 root root 0 Mar 10:36 test-3
-rw-r--r--1 root root 0 Mar 10:36 test-4
-rw-r--r--1 root root 0 Mar 10:36 test-5
-rw-r--r--1 root root 0 Mar 10:36 test-6
-rw-r--r--1 root root 0 Mar 10:36 test-7
-rw-r--r--1 root root 0 Mar 10:36 test-8
-rw-r--r--1 root root 0 Mar 10:36 test-9

3, change the above file name test to Bruce (with for Loop)
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, the above file name of Bruce quickly changed to network

[Email protected] test]# Rename test network *bruce*
[email protected] test]# LL
Total 0
-rw-r--r--1 root root 0 Mar 10:36 network-1
-rw-r--r--1 root root 0 Mar 10:36 network-10
-rw-r--r--1 root root 0 Mar 10:36 network-2
-rw-r--r--1 root root 0 Mar 10:36 network-3
-rw-r--r--1 root root 0 Mar 10:36 network-4
-rw-r--r--1 root root 0 Mar 10:36 network-5
-rw-r--r--1 root root 0 Mar 10:36 network-6
-rw-r--r--1 root root 0 Mar 10:36 network-7
-rw-r--r--1 root root 0 Mar 10:36 network-8
-rw-r--r--1 root root 0 Mar 10:36 network-9

5, Batch create 10 system account test01,test02,... test10 and set a password
(1), first understand the method of generating ordinal numbers
[Email protected] test]#echo {01..10}
Geneva, the
[Email protected] test]#seq 10
1
2
3
4
5
6
7
8
9
Ten
[Email protected] test]#Seq-w 10
01
02
03
04
05
06
07
08
09
Ten


(2), based on the ordinal number to generate user account and password information in turn
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
[Email protected] ~]#./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.
[Email protected] ~]# 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
Description: Build test01,test02,... test10 user success!


6. Delete users in bulk
userdelsh.sh
#!/bin/bash
For username in ' cat/etc/passwd| grep test*|awk-f: ' {print '} '
Do
Userdel-r $username
Done
chmod +x userdelsh.sh
[Email protected] ~]#./userdelsh.sh
[Email protected] ~]# Cat/etc/passwd|grep test*
Description: Delete test01,test02,... test10 user success!


7, Batch create 10 accounts bruce01,bruce02,... Bruce10 and set a random password
createusernew.sh
#!/bin/bash
For i in ' seq-w 10 '
Do
Passwd= ' echo $RANDOM | md5sum | Cut-c 1-8 '
# #注意: After a random number is defined as a variable, its value remains the same during subsequent references!
Useradd bruce$i && echo "$PASSWD" | passwd--stdin bruce$i
Echo-e "user:bruce$i \ Password: $passwd" >>/tmp/userandpw.log
Done
chmod +x createusernew.sh

[Email protected] ~]#./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.
[Email protected] ~]# SU-BRUCE01
[Email protected] ~]$ 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
[Email protected] ~]$ SU-BRUCE02
Password:
[[Email protected] ~]$ exit
Description: Batch create user and generate random number password verification and login successfully!

This article is from the "Bruce_tan" blog, make sure to keep this source http://380281.blog.51cto.com/370281/1758365

One of the Linux Shell Scripting Learning and combat

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.