1. Review exercises
general wildcard
curly braces {}
Backticks, single quotes, double quotes
output redirection > >>
input redirect < <<
user-defined variable
Two Shell Command Exercises
(1) View the help information and briefly describe the purpose of the following files
①/etc/passwd (hint: view the description of this file through man 5 passwd)
/etc/passwd stores the password file. Each user in the file has a corresponding record line, which records some basic attributes of the user, including: login name, optional encrypted password, digital user id, digital group id, username and comment fields, user home directory, optional user command interpreter.
②/etc/shadow (hint: check the description of this file through man 5 shadow)
/etc/shadow is a shadow of the passwd file. The record lines in the /etc/shadow file correspond to those in /etc/passwd, but only the system administrator can modify and view them.
③/etc/group (hint: view the description of this file through man 5 group)
/etc/group stores information about local user groups
④/etc/gshadow (hint: check the description of this file through man 5 gshadow)
/etc/gshadow is the encrypted information file of /etc/group. For example, user group management passwords are stored in this file.
(2) Enter the following commands in turn to observe the running results. Combined with the help information viewed by man id, point out the function of each command
id: The id command can display the real and effective user ID (UID) and group ID (GID)
id -u only show user id
id -u root is to view the user ID of root, which is 0
id -u hadoop is to view the user ID of user hadoop, which is 1000
(3) Enter the following commands in sequence at the shell command terminal, observe the execution results, and understand the specific functions implemented by each command
which python
whereis python
locate python
find /usr/bin -name python
(Explanation: which, whereis, locate, find all have the function of retrieval, combined with the execution results, find help information, and summarize the differences)
which to see the location of the executable
whereis to view the location of the file
locate cooperates with the database to view the file location
find actually searches the hard disk to query the file name
②grep -n -E 'root|jsj|^user*' /etc/passwd (hint: extended regular expression)
grep -n -E '[[:digit:]]' /etc/passwd
grep -n -E '[[:alpha:]]' /etc/passwd
grep -n '[0-9]\{4,\}' /etc/group (hint: look for group information records whose GID is 4 digits or more in the user group file)
(Note: with option -E means extended regular expression; without option -E is normal regular expression)
The grep command can search text through regular expressions and print the matched lines; the option -n can print the line number, and -E can use multiple regular expressions.
③sudo apt-get install gimp
which gimp
sudo apt-get remove gimp
which gimp
sudo apt-get install gimp command can install gimp
sudo apt -get remove gimp command to uninstall gimp
which gimp is used to see where the files are.
④ls -dl/root --time-style=long-iso
ls -dl /root --time-style=long-iso | cut -d' ' -f1,8
(Note: there are spaces in the quotation marks after the option -d of the cut command; prompt: compare and observe the results of the previous line)
The option -d can customize the delimiter, -f displays the content in the specified field, the above command specifies the space as the delimiter, and displays the content in the 1st and 8th fields.
⑤ls -l --time-style=long-iso
ls -l --time-style=long-iso -t
ls -l --time-style=long-iso -t -r (Description: execute in sequence, compare and observe, combined with help options)
⑥ls /usr/share/man
ls /usr/share/man | grep man[1-8]
ls /usr/share/man/man1
file /usr/share/man/man1/ls.1.gz (combined with the execution results, review the contents of the gzip command in section 2.8.1)
The file command can identify the file type, then create a new directory temp , copy ls.1.gz to the temp directory, and decompress the gzip command.
⑦ls –l /home | grep "^d" | wc –l
ls-l finds files in the /home directory, grep "^d" filters files whose lines start with d , and wc -l counts the number of lines
⑧sudo adduser user7
ls /home | tee users | wc -l (hint: enter cat users to observe the contents of the file)
sudo adduser command is used to add new users
(4) Write the corresponding shell command as required
①Search for the existence of the file signal.h in the directory /usr/include (hint: find command)
② Find the line containing BUFSIZ in all files in the /usr/include directory, and display the line number. Requirements: Only the found results are displayed on the screen, and error messages are filtered.
(Hint: ① use grep and wildcard *; ② use error message redirection and special device file /dev/null)
③ Find the user information record whose login shell is bash in the user name and password file /etc/passwd, and display the line number
(hint: use grep and $ in regex)
④ Intercept the first column (group name) and the third column (group id) from the /etc/group file, and sort according to the numerical value of the group id number from small to large. (Hint: Combine cut, pipeline and sort commands)
(When submitting the document, write the commands and screenshots of the functions it implements, including screenshots of commands and results)
(5) Experience the usage of awk and sed: execute commands in sequence and observe the execution results
①cp/etc/apt/sources.listt1; less t1
②sed -e "s/#.*//g"t1
③sed -e "s/#.*//g"t1| awk '{if (length != 0) print $0}'
④tail -5 /etc/passwd | awk -F: '{print $1}'
⑤tail -5 /etc/group | tee t2
awk 'BEGIN{print "file t2"} {print "line" NR ":" $0} END {print "over"}' t2
(When submitting documents, consult the web and summarize the following:
① The purpose of the file /etc/apt/sources.list, understand its content
/etc/apt/sources.list is the record used by the package management tool apt
② Command tool sed function, screenshots show 2~3 sed command exercises that you have tried yourself, and make necessary statements about the specific functions
sed can replace characters, replace hello with hi in the exercise above
③ Command tool awk function, screenshots show 2~3 awk command exercises that you have tried yourself, and make necessary statements for specific functions)
Line matching statement awk ' ' can only use single quotes
Each line is split by space or tab, and output 1, 4 items in the text;
awk -F: -F is equivalent to the built-in variable FS, specifying the split character, use ' , ' to split in the exercise above
(6) Consult the network or help, and experience the usage of commands curl and wget.
(When submitting documents, consult the web and summarize the following:
② Command the curl function of the tool, take screenshots to show the exercises you have tried, and make necessary statements
In Linux, curl is a file transfer tool that uses URL rules to work under the command line. It is a very powerful http command line tool. It supports file uploading and downloading, and is a comprehensive transfer tool.
(1) Install curl
(2) When using curl without any options, such as curl http://www.baidu.com , a GET request will be sent by default to get the link content to the standard output.
(3) Only display the http header, not the file content, use the -I option
(4) To display the http header and file content at the same time, use the -i option.
(5) Save the link to a file. Use the > symbol to redirect output into a local file.
②Command the wget function of the tool, take screenshots to show the exercises you have tried, and make necessary statements
wget is a tool for downloading files. It is used under the command line. The wget tool has complete functions, supports breakpoint download function, supports FTP and HTTP download methods, supports proxy server and is convenient and simple to set up.
(1) Use wget to download a single file, a progress bar will be displayed during the download process, including (download completion percentage, downloaded bytes, current download speed, remaining download time)
(2) Download with wget -O and save with a different file name. In the exercise below, download a file and save it with the name download1
3. Write a shell script and execute it in 4 ways (see Chapter 4 Teaching Materials/Courseware).
(1) Exercise 1
Step 1, use vi/vim/gedit or other editor, write the shell script ex1.sh, the content is as follows:
The function of this script is to back up ordinary files in the current directory that have been modified within the last day (that is, within 24 hours). Use the current date and time of the backup system as the file name, and compress the archive. Error information during backup is written to error_info. Then check the backup file and the error message respectively.
In step 2, try the four methods described in Chapter 4, run the script, and understand the shell commands used in this example.
(2) Exercise 2
Step 1, use vi/vim/gedit or other editor to write the shell script ex2.sh, the content is as follows:
Try the four methods described in Chapter 4 to run the script.
If it prompts "the file major_code.txt cannot be found", please copy the major_code.txt in the public mail experiment folder to the directory where ex1.sh is located, and execute it again.
Note: the file major_code.txt, please do not make any changes, including the format.
Combined with the running results, analyze the script code, and summarize the following contents in the experimental report:
② Write out the read command option -n11 function in line3
read is used to read the variable value from the keyboard, 11n means there are 11 bits.
-n11 means the input ends after 11 characters are entered
② Write the function of line14
Line14:grep $code $filename >t1 && read x major < t1
Function: search code and filename redirect to t1, and then output from t1 to major;
$code: Professional number, which is the 5-8th of the 11 student numbers.
$filename是major_code.txt.
4. Experimental summary
Through this lab exercise, I learned about shell programming and practiced a lot of shell commands. There are many kinds of shell commands, many of which need to be practiced repeatedly by yourself, and various mistakes will often occur in the process of practice, such as less spaces and so on. Shell programming is fun, but takes more time to experience to understand.