#!/bin/bash #Write a script#1. Set the value of the variable file to/etc/passwd#2. Say hello to each user in/etc/passwd and say what the ID is.#shaped like:#Hello,root,your UID is 0. FILE=/etc/passwd COUNT= ' Wc-l${file}| Cut-f1-d" "` forIinch' Seq${count}` DoUSER= ' Head-n${i} ${file}| Tail-n 1 | Cut-f5-d":"' ID= ' Head-n${i} ${file}| Tail-n 1 | Cut-f4-d":"' echo Hello,${user}, your UID is${id} Done
#!/bin/bash #Write a script#1. Switch working directory to/var#2. Greet each file or subdirectory in the/var directory in turn, as follows:#Hello,log#3. Statistics of multiple files in/var directory, and displayedWork_path=/var NUM=0CD${work_path} #Switch working directory forFILEinch' ls ' DoEcho Hello,${file} #like each file or subdirectory , in turn, a question markLet Num+=1#Let is doing numerical operationsDid echo the number of files is${num} #Displays the total number of files
#!/bin/bash #Write a script#1. Set the value of the variable file to/etc/passwd#2. Use the 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 fileFILE=/etc/passwd file_tmp=/tmp/mypasswd forIinch2 4 6 10 13 15 DoEcho ' head-N${i} ${file}| Tail-n 1 | Tee-a${file_tmp}' done
#!/bin/bash #Write a script#pass two integers to the script, let the script calculate and display the two integers separately and, difference, product, quotient if[ $#-lt 2]Then Echo${0}Number number exit fi echo first number is${1}Echo Second number is${2}Echo$(( $+ $)) echo$[ $- $] Echo$[ $* $] Echo$[ $/ $]
#!/bin/bash #write a script:#1. Create a directory/tmp/scripts#2. Switch the working directory to 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 the owner of the files and subdirectories inside to Gino#5. Change the permissions of other users of the files in test and its subdirectories to no permissionsFOLDER=/tmp/Scripts Folder_source=/etc/PAM.Dif[!-D${folder}]#determine if the directory exists, does not exist, creates a directoryThen mkdir$FOLDERfi cd${folder} #switch to working directoryCp-rf$FOLDER _source./Test Chown-R Gino./test#Change Ownerchmod./test#Change Permissions
#!/bin/bash #Write a script#1. Display the current system date and time, and then create 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. List files or directories in the current directory that begin with a,x or 6#6. List the files or directories under the current directory that begin with a letter followed by an arbitrary number and then any length characterFOLDER=/tmp/lstest Date#Show current time and dateif[!-D${folder}]#determines whether the directory exists and creates if it does not existThen mkdir${folder}fi cd${folder} if[!-D"a1d"] then mkdir a1d fiif[!-D"b56e"] then mkdir b56e fiif[!-D"6test"] then mkdir 6test fiif[ !- F "XY"] then touch xy fiif[ !- F "x2y"] then touch x2y fiif[ !- F "732"] then touch732fi ls [ax6]*ls [[: alpha:]][[:d igit:]]*
(reproduced) Shell script exercises