Practice One: Write a script1The value of the set variable file is/etc/passwd2. Turn to/etc/Say hello to each user in passwd and say what the ID is like: (Hint: line= ' Wc-l/etc/passwd | Cut-d" "-F1 ') Hello,root,your UID is 0. 3. Statistics a how many user answers one: #!/bin/Bash File="/etc/passwd"LINES= ' Wc-l $file | Cut-d" "-F1 ' forIinch' Seq1$LINES '; Douserid= ' head-$I $file | Tail-1|cut-d:-F3 ' username= ' head-$I $file | Tail-1|cut-d:-F1 ' echo"Hello $username, your UID is $userid"Done Echo"There is $LINES users"answer two: #!/bin/Bash File=/etc/passwd let Num=0 forIinch' Cat $file '; Dousername= ' echo"$I"| Cut-d:-F1 ' userid= ' echo"$I"| Cut-d:-F3 ' Echo"Hello, $username, your UID is $userid"Num=$[$num +1] Done Echo"There is $num users"Exercise Two: Write a script1. Switch working directory to/var 2. Turn To/each file or subdirectory in the Var directory say hello, like this: (Hint: forFILEinch/var/* ; or for FILE in ' Ls/var ';) Hello,log 3. Statistics/var directory has a number of files, and displays the answer: #!/bin/bash Cd/var let num=0 for I in ' ls/var/* ';d o echo "Hello $I" num=$[$num +1] Done echoes "The number of files is $num" Exercise three: Write a script 1. Set Variable F The value of Ile is/etc/passwd 2. Use a loop to read the 2,4,6,10,13,15 line of the file/etc/passwd and display its contents 3. Save these lines to the/tmp/mypasswd file answer: #!/bin/bash File= "/etc/passwd" for I in 2 4 6 15;do exec 3>/tmp/mypasswd line= ' head-$I $file | Tail-1 ' echo ' $line "echo" $line ">&3 exec 3>&-done exercise four: Write a script to pass two integers to the script, let the script separate Calculate and display the two integers and, the difference, the product, the quotient answer is as follows: Vim test.sh #!/bin/bash echo "First number $" (Indicates the output A number) echo "Second number $" (indicates the output of the second digit) echo "$ (($1+$2))" (Output two sum) echo "$[$1-$2]" (output two number difference) echo "$[$1*$2" (output two number of product) echo "$[$1 /$2] "(output two quotient): Wq (means save and Exit VI Editor) chmod +x test.sh (for test.sh)./test.sh 2 3 (pass two parameters and execute script job one: Write a script: 1. Create a directory/tmp/scripts 2. Switch work Recorded in this directory 3. Copy the/ETC/PAM.D directory to the current directory and rename it to test 4. Change the current directory's test and its contents of the file and subdirectories to Redhat 5. Change the permissions of other users of the files in test and its subdirectories to No Permission answer: #!/bin/bash mkdir-v/tmp/scripts cd/tmp/scripts cp-r/etc/pam.d./test chown-r Redhat./test chmod-r o=---./test Job Two: Write a script 1. Displays the current system date and time, and then creates the directory/tmp/lstest 2. Switch working directory to/tmp/lstest 3. Create a directory A1d,b56e,6test 4. Create an empty file xy,x2y,732 5. Lists the files or directories that begin with a,x or 6 in the current directory 6. Lists the files or directories under the current directory that begin with a letter followed by an arbitrary number with any length of characters Answer: #!/bin/bash date mkdir-pv/tmp/lstest cd/tmp/lstest mkdir a1d b56e 6test Touch XY x2y 732 ls [ax6]* ls [[: alpha:]][[:d igit:]]* Job Three: Write a script to add 10 users user1 to User10, but requires only if the user does not exist To add the answer: #!/bin/Bash for I in ' seq 1 ';d o cut-d:-f1/etc/passwd |grep "user$i" 2>>/tmp/etc.err | | Useradd user$i Done Job Four: Write a script to test 192.168.0.151 to 192.168.0.254 If all hosts are online, if online, the "IP is up" displays as If the result is not online, the "IP is down" answer is displayed: #!/bin/bash for I in ' SEQ 151 254 ';d o ping-c1-w1 192.168.0. $I &>/dev/nu ll && echo "192.168.0. $I is up" | | echo "192.168.0. $I is down" done
Shell Script Example Collection (exercise summary)