1. Write a script
The script can accept more than one file path as a parameter;
Displays the number of rows held by each file;
Shows how many files have been executed for the number of rows this time.
#!/bin/bashfor file in $*; Do lines= ' wc-l $file | Cut-d '-f1 ' echo ' $file has $lines lines. " Doneecho "$# files." Running results: [[email protected] test]# sh 1.sh/etc/inittab/etc/inittab has lines.1 files.
2. Calculates the sum of the sum and odd numbers of all even numbers within 100, respectively
#!/bin/basheve=0odd=0for ((i=1;i<=100;i++));d o if ' let i%2 ', then let odd=odd+i else let Eve=eve+i fidoneecho " Odd and $odd, even and $eve "Run results: [[email protected] test]# sh 2.sh odd and 2500, even and 2550
3. Calculate the sum of the IDs of all users on the current system
#!/bin/bashdeclare-i idsum=0for i in ' cut-d:-f3/etc//passwd ';d o #由于51cto关键字限制, remove a '/' Let Idsum=idsum+idoneecho $i DSum operation Result: [[email protected] test]# sh 3.sh 68489
4, new 10 user tuser401-tuser410, and beg their ID of the sum
#!/bin/bashdeclare-i idsum=0for i in {401..410};d o useradd tuser$i userid= ' id-u tuser$i ' let IDSUM=IDSUM+USERIDDONEEC Ho "id sum: $idsum" Run result: [[email protected] test]# sh 4.sh ID sum:5045[[email protected] test]# tail/etc//passwd #由于 51cto keyword limit, remove a '/' tuser401:x:500:500::/home/tuser401:/bin/bashtuser402:x:501:501::/home/tuser402:/bin/ bashtuser403:x:502:502::/home/tuser403:/bin/bashtuser404:x:503:503::/home/tuser404:/bin/bashtuser405:x:504:504 ::/home/tuser405:/bin/bashtuser406:x:505:505::/home/tuser406:/bin/bashtuser407:x:506:506::/home/tuser407:/bin/ bashtuser408:x:507:507::/home/tuser408:/bin/bashtuser409:x:508:508::/home/tuser409:/bin/bashtuser410:x:509:509 ::/home/tuser410:/bin/bash
5. Write a script
Create user tuser501-tuser510;
Create directory/tmp/dir-current date time;
In/tmp/dir-current datetime, create 10 empty files in the directory file101-file110
Change the owner of the file101 to tuser501, and so on, and always change the owner of the file110 to tuser510.
#!/bin/bashmkdir -p /tmp/dir/' date +%y-%m-%d ' cd /tmp/dir/' date +%Y-%m-%d ' for i in {01..10};d O useradd tuser5$i mkdir file1$i chown tuser5$i file1$idone Running Results:[[email protected] test]# sh 5.sh [[email protected] test]# tail /etc//passwd #由于51cto关键字限制, remove a '/' tuser501:x:510:510::/home/tuser501:/bin/bashtuser502:x:511:511::/home/tuser502:/bin/bashtuser503:x:512:512: :/home/tuser503:/bin/bashtuser504:x:513:513::/home/tuser504:/bin/bashtuser505:x:514:514::/home/tuser505:/bin/ bashtuser506:x:515:515::/home/tuser506:/bin/bashtuser507:x:516:516::/home/tuser507:/bin/bashtuser508:x:517:517 ::/home/tuser508:/bin/bashtuser509:x:518:518::/home/tuser509:/bin/bashtuser510:x:519:519::/home/tuser510:/bin/ bash [[email protected] test]# ls -l /tmp/dir/2017-07-03/Total Dosage 40drwxr-xr-x 2 tuser501&Nbsp;root 4096 7 Month 3 14:40 file101drwxr-xr-x 2 tuser502 Root 4096 7 Month 3 14:40 file102drwxr-xr-x 2 tuser503 root 4096 7 Month 3 14:40 file103drwxr-xr-x 2 tuser504 root 4096 7 Month 3 14:40 file104drwxr-xr-x 2 tuser505 root 4096 7 Month 3 14:40 file105drwxr-xr-x 2 tuser506 root 4096 7 Month 3 14:40 file106drwxr-xr-x 2 tuser507 root 4096 7 month 3 14:40 file107drwxr-xr-x 2 tuser508 root 4096 7 Month 3 14:40 file108drwxr-xr-x 2 tuser509 root 4096 7 Month 3 14:40 file109drwxr-xr-x 2 tuser510 root 4096 7 Month 3 14:40 file110
6, separate statistics/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions, and/etc/inittab files with # The number of rows and blank lines in the beginning of the row
#!/bin/bashfor file in /etc/rc.d/rc.sysinit /etc/ rc.d/init.d/functions /etc/inittab;do echo "The lines contain # in $file is ' grep -e ' ^# $file | wc -l '. echo "the space lines in $file is ' grep -e " ^[[:space :]]*$ " $file |wc -l '." Done run Result: [[[Email protected] test]# sh 6.sh the lines contain # in /etc/rc.d/rc.sysinit is 44.the space lines in /etc/rc.d/rc.sysinit is 103.The lines contain # in /etc/rc.d/init.d/functions is 43.The space lines in /etc/rc.d/init.d/functions is 106.the lines contain # in /etc/inittab is 25.the space lines in /etc/inittab is 0.
7. Displays the user name, UID, and the UID of all the default shell-bash users on the current system
#!/bin/bashgrep "/bin/bash$"/etc//passwd | cut-d:-f1,3 #由于51cto关键字限制, remove a '/' declare-i sum=0for userID in ' grep '/bin/bash$ '/etc//passwd | cut-d:-f3 ';d o #由于51cto关键字限制, remove a '/' Let Sum=sum+useriddoneecho "$sum" Run Results: [[email protected] test]# sh 7.sh root:00
8. Display the user name of the user with additional groups on the current system, and how many such users are in total
[[email protected] test]# egrep ' [^:]$ '/etc/group | Cut-d:-f4 | Sort-u | Egrep-o ' [[: alnum:]]* ' | Sort-u
9, accept a parameter, this parameter is the user name, if this user exists, then display its ID number
#!/bin/bashread-p "Please enter user name:" Usernameif ID $username &>/dev/null, then echo "$username id ' grep ' ^\< $username \ > "/ETC//PASSWD | cut-d:-f3 ' "Else #由于51cto关键字限制, remove a '/' echo ' Without this user ' fi run result: [[email protected] test]# sh 9.sh Please enter user name: Mysqlmysql ID 496[[email protected] test]# sh 9.sh Please enter user name: 123 No this user
10, through the command line to pass two integer parameters to the script, the script can return its large person
#!/bin/bashread-p "Please enter two integers:" A1 a2if [$a 1-gt $a 2];then echo $a 1else echo $a 2fi run result: [[email protected] test]# sh 10.s h Please enter two integers: one 1616[[email protected] test]# sh 10.sh Please enter two integers: 78 1678
11, the command line to pass any number of integers to the script, the script can return its large person
#!/bin/bashdeclare-i num=0for i in [e-mail protected];d o if [$i-gt $num];then let num=i fidoneecho "maximum number: $num" Run Result: [ [Email protected] test]# sh 11.sh 7978 991 31 3321 32 Maximum: 7978[[email protected] test]# sh 11.sh 78 991 31 3321 32 Maximum number: 3 321
12, given a file path through the command line, and then judge: if there is a blank line in this file, the total number of blank lines is displayed, otherwise, no blank lines are displayed
#!/bin/bashif grep "^[[:space:]]*$" $ &>/dev/null; then echo "$ Have $ (grep" ^[[:space:]]*$ "$ | wc-l) Blank lines. " else echo "No blank lines." Fi operation Result: [[email protected] test]# sh 12.sh/etc//passwd #由于51cto关键字限制, remove a '/' No blank lines. [Email protected] test]# sh 12.sh/etc/inittab No blank lines. [Email protected] test]# SH 12.sh/etc/rc.sysinit/etc/rc.sysinit has 103 blank lines.
13. Pass a parameter to the script:
If the argument is quit, the display says you want to exit;
If the parameter is yes, the display says you want to continue;
Any other parameter, it is said to be unrecognized.
#!/bin/bashcase $ inquit| Q) echo "Exit program";; yes| Y) echo "continue";; *) echo "unrecognized";; Esac Run Result: [[email protected] test]# sh 13.sh unrecognized [[email protected] test]# sh 13.sh quit quit program [[email protected] test]# SH 1 3.sh Yes continue
14. Pass a user name to the script:
If this user's ID number is 0, then the display says this is the administrator;
If this user's ID number is greater than or equal to 500, the display says this is a normal user
Otherwise, it is said that this is the system user;
#!/bin/bashif id $1 &>/dev/null ;then userid= ' grep ' ^\<$1\ > " /etc//passwd | cut -d: -f3 " #由于51cto关键字限制, remove a '/' if [ $userid -eq 0 ];then echo "$1 is the administrator." elif [ $userid -ge 500 ];then echo "$1 is a normal user. " else echo "$1 is a system user." fielse echo "No this user." Fi Run result:[[email protected] test]# sh 14.sh mysql mysql is the system user. [[Email protected] test]# [[email protected] test]# sh 14.sh mysql mysql is a system user. [[email protected] test]# sh 14.sh rootroot is an administrator. [[email protected] test]# sh 14.sh nfsnobodynfsnobody is an ordinary user. [[email protected] test]# sh 14.sh Nfsnobo "No this user."
15, given a user, if its shell is/bin/bash and its ID number is greater than or equal to 500, then it is a logon to a normal user; otherwise, it will be displayed as a non-logged-on user or administrator.
#!/bin/bashif id $1 &> /dev/null ;then userid= ' grep ' ^\<$1\ > " /etc//passwd | cut -d: -f3 ' #由于51cto关键字限制, remove a '/' Usershell= ' grep ' ^\<$1\> ' /etc//passwd | cut -d: -f7 ' #由于51cto关键字限制, remove a '/' if [ $userid -ge 500 ] && [ "$usershell" = = "/bin/ Bash " ];then echo " $1 is available for regular users " else echo "$1 is a non-login user or administrator" fielse echo "no this user." Fi Run result:[[email protected] test]# sh 15.sh rootroot is a non-login user or Administrator [[email protected] test]# sh 15.sh mysqlmysql is a non-login user or Administrator [[email protected] test]# sh 15.sh nfsnobodynfsnobody is available to login to ordinary users [[email protected] test]# sh 15. Sh nfsnobo no such user.
16, write a script, if a user does not exist, add
#!/bin/bashif ID $ &>/dev/null; then echo "$ already exists." Else useradd echo "Add user $." Fi run Result: [[email protected] test]# sh 16.sh mysqlmysql already exists. [[Email protected] test]# sh 16.sh mylinux Add user mylinux. [[email protected] test]# tail-1/etc//passwd #由于51cto关键字限制, remove a '/' Mylinux:x:500:500::/home/mylinux:/bin/bash
17. Add 10 Users: tuser501-tuser510; If the user does not exist, it is added; if present, the user is displayed; Shows how many users have been added altogether.
#!/bin/bashdeclare -i num=0for i in {501..510}; Do if id tuser$i &> /dev/null ;then echo "tuser$i already exists" else useradd tuser$i echo "Add user tuser$i" let num++ fidoneecho "Add $num users." Run results:[[email protected] test]# sh 17.sh Add user tuser501 Add users tuser502 Add user tuser503 Add user tuser504 Add user tuser505 Add user tuser506 Add user tuser507 Add users tuser508 Add a user tuser509 add a user tuser510 add 10 users. [[email protected] test]# sh 17.sh tuser501 already exists tuser502 already exists tuser503 Existing tuser504 already exists tuser505 already exists tuser506 already exists tuser507 already exists tuser508 already exists tuser509 An existing tuser510 already exists to add 0 users.
18, add 10 Users: tuser601-tuser610, add if the user does not exist, add success with green display, and if present, show this user in red ; Shows how many users have been added altogether.
#!/bin/bash#declare -i count=0for i in {601..610}; do if id tuser$i &> /dev/null; then echo -e "\033[31mtuser $i \033[0m exists. " else useradd tuser$i echo -e "add user \033[32mtuser$i\033[0m successfully." let count++ fidoneecho "Total add $count users. " Run result:[[email protected] test]# sh 18.sh add user tuser601 Successfully.add user tuser602 successfully.add user tuser603 successfully.add user tuser604 successfully.add user tuser605 successfully.add user Tuser606 successfully.add user tuser607 successfully.add user tuser608&nbsP;successfully.add user tuser609 successfully.add user tuser610 successfully. Total add 10 users. [[email protected] test]# sh 18.sh tuser601 exists.tuser602 exists.tuser603 exists.tuser604 exists.tuser605 exists.tuser606 exists.tuser607 exists.tuser608 exists.tuser609 exists.tuser610 exists. Total add 0 users.
19. Pass the user name to the script
Determine if this user's shell is/bin/bash, and if so, show this user as Basher
Otherwise, this user is displayed as a non-basher
#!/bin/bash#usershell= ' grep ' ^$1\> '/etc//passwd | cut-d:-f7 ' #由于51cto关键字限制, remove a '/' if [' $userShell ' = = '/bin/bash ']; Then echo "basher" else echo "not basher" fi run result: [[email protected] test]# sh 19.sh mysqlnot basher[[email protected] Test ]# SH 19.sh mylinuxbasher
20. Given a file path
Determine if the file exists, does not exist, the file is not present, and the script is finished directly;
If the file is a normal file, it is displayed as "regular";
If the file is a directory, it is displayed as "directory";
If the file is a linked file, it is displayed as "symbolic file";
Otherwise, it is displayed as "unknown type."
#!/bin/bashif [ ! -e $1 ];then echo "FILE&NBSP;NOT&NBSP;EXIT&NBSP;." exit 5fiif [ -L $1 ];then echo "Symbolic file." elif [ -d $1 ];then echo "directory." elif [-f $1 ];then echo "Regular file." else echo "Unknown type." Fi run Result: [[Email protected] test]# sh 20.sh /etc/directory. [[email protected] test]# sh 20.sh /etc/rc.d/rc1.d/k92iptables symbolic file. [[email protected] test]# sh 20.sh 12312file not exit .
This article is from the "Wind and Drift" blog, please be sure to keep this source http://yinsuifeng.blog.51cto.com/10173491/1944179
Iv. Shell Programming Exercises (1-20)