Experiment three shell programming (1)

 

One, shell command application practice

1. Briefly describe the purpose of the following documents

① /etc/passwd

    passwd is a password file used to manage user passwords. Ordinary users can usually only modify their own password information.

② /etc/shadow

    shadow is a shadowed password file that contains password information and optional age information for system accounts. Only administrators can view changes.

③ /etc/group

    The group file is the configuration file of the user group, including users and user groups, and can show which user group or which user groups the user belongs to, because a user can belong to one or more different user groups; the same user group users have similar characteristics.

④ /etc/gshadow

    /etc/gshadow is the encrypted information file of /etc/group, such as user group (Group) management password is stored in this file. /etc/gshadow and /etc/group are two complementary files; for large servers, for many users and groups, it is extremely necessary to customize some permission models with complex relational structures and to set user group passwords.

2. Enter the following commands in sequence 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 jsj View the ID of the user jsj, the result is that there is no user jsj.

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: find the executable file named python in the system PATH directory
whereis python: whereis command can be used to find binary (command), source file, man file. Unlike which, this command can be searched through the file index database instead of PATH, so the search is wider than which
locate python: find files through the database. This command can find any file you specify to find, and you can enter only part of the file name.
find /usr/bin -name python: Find the file named python in the /usr/bin directory by directly searching the hard disk.

 

② grep -n -E 'root|cy|^user*': /etc/passwd -n is to display the line number, the function of this command is to find the line with root or cy as the first line in the /etc/passwd file, among which * means repeat more than zero times, ^ means must appear at the beginning of the line.
grep -n -E ' [[:digit:]] ' /etc/passwd: Find lines in the /etc/passwd file that contain pure digits.
grep -n -E ' [[:alpha:]] ' /etc/passwd: Find lines in the /etc/passwd file that contain pure letters.
grep -n '[0-9]\{4,\}' /etc/group Because no -E is added, use curly braces and escape characters \{\}, [0-9] is equivalent to [[: digit:]], find pure numbers, 4 means more than 4 digits.

③ sudo apt install gimp: Install the software gimp.
which gimp: Query the location of gimp.
sudo apt remove gimp: uninstall gimp.
which gimp: query the location of gimp, there are no query results at this time.

④ ls -dl /root --time-style=long-iso: The file directory information, detailed information and time are displayed in the complete IOS time format under /root.
ls -dl /root --time-style=long-iso | cut -d' ' -f1,8: Use spaces as separators to cut the first and eighth fields in the previous instruction.

⑤ ls -l --time-style=long-iso,: ls -l is to list the detailed information of the file, the time is set to long-iso format, and the default sorting.
ls -l --time-style=long-iso -t: Sort by time based on the previous command.
ls -l --time-style=long-iso -t -r: Sort by time on the basis of the previous command, but change to sort from far to near.

⑥ ls /usr/share/man: Display the directory under
/usr/share/man ls /usr/share/man | grep man[1-8]: Search for man1~man8 based on the previous command
ls /usr/ share/man/man1: Display the directory
file /usr/share/man/man1/ls.1.gz under /usr/share/man/man1 (combined with the execution results, review the contents of the gzip command in section 2.8.1)
mkdir ~/temp ; cp /usr/share/man/man1/ls.1.gz ~/temp: create a temp folder in the root directory and copy ls.1.gz to temp
cd ~/temp; ls: go to the temp directory , view all file directories, there is a suffix .gz at this time
sudo gzip -d ls.1.gz; ls: decompress and view the directory, at this time .gz disappears, indicating that the compressed package has been decompressed and deleted.

⑦ ls -l /home | grep "^d" | wc -l: Find lines starting with d in the home directory and count the number of lines.

 

⑧ sudo adduser user7: Create user user7.
ls /home | tee users | wc -l: Find the user name in the home directory and count the number of rows.

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.

 

③ Find the user information record whose login shell is bash in the username and password file /etc/passwd, and display the line number

④ Intercept the first column (group name) and the third column (group id) from the /etc/group file, and sort them according to the numerical value of the group id number from small to large.

 

5. Experience the usage of awk and sed: execute the commands in sequence and observe the execution results
① cp /etc/apt/sources.list t1; 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

Observe the execution results and summarize the following:

① Purpose of the file /etc/apt/sources.list: /etc/apt/sources.list is a configuration file used by the package management tool apt to record the location of the software package warehouse, and saves the address of the source server for ubuntu software updates.

② Command tool sed function, the screenshot shows 2~3 sed command exercises that you have tried yourself, and make necessary statements about the specific functions:

Delete lines containing "2":

Replace the first line with 0000000:

③ Command tool awk function, screenshots show 2~3 sed command exercises that you have tried yourself, and make necessary statements about the specific functions

Count the number of users in /etc/passwd:

Count the total size occupied by all files in the current directory:

Among them, in size=size+$5, $5 represents the fifth column of each row, and the fifth column is the size, so write it like this.

6. Consult the network or help to experience the usage of commands curl and wget

① Command tool curl function 

curl+url, display the html of the URL.

Get website cookies

save page

② Command tool wget function

Download a single file:

 

http:

Test whether the website can be accessed normally:

 

2. Write a shell script and execute it in 4 ways (refer to Chapter 4 Teaching Materials/Courseware).

 (1) Exercise 1

Method 1: bash<filename

Method 2: bash filename [ arguments ]

Method 3: filename

Way 4: .filename

 (2) Exercise 2

Method 1: bash<filename

Method 2: bash filename [ arguments ]

Method 3: filename

Way 4: .filename

 ① Write out the function of the read command option -n11 in line3:

read is used to read the variable value from the keyboard, 11n means there are 11 bits.

② Write the function of line14

$code: major number, from the point of view of line6, it is the 5~8 digits of the 11-digit student number. $filename is the major_code.txt of line11. This line of code redirects the major_code and file name to t1, and t1 is used as the output of the previous command , and then input it as a command. According to the command grep $code of t1, read the professional name from major_code.txt to major.

 

3. Summary and experience

    Through this linux experiment, I stepped into a new field - shell programming. There are many kinds of linux commands, the functions are small but complete, and they are very powerful when assembled. They can realize various functions and build a new world. However, the process of the experiment is very laborious, and there are often fewer spaces or some strange errors. I came into contact with a lot of unknown commands and gained a lot of new knowledge. I realize that I still have many shortcomings. Unfortunately, I don't have much time to improve myself, and I need to work harder.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906683&siteId=291194637