Experiment 3: Shell Programming (1)

 shell command application exercise

(1) View the help information and briefly describe the purpose of the following files 

/etc/passwd records the user's account

/etc/shadow is an optional encrypted password file (requires sudo permissions for viewing)

/etc/group holds the names of all groups in the system

/etc/gshadow security group account information

View the file descriptions of several commands, and use cat to view the meaning of each field

 

 

 

 

(2) Enter the following commands in turn and observe the operation results. Combined with the help information viewed by man id, point out each item

Command function

1 id Displays the ID of the user, and the ID of the group to which they belong.

2 id -u displays the current user ID

3 id -u root Display root user ID

4 id -u zhaoyi Display zhaoyi user ID

Explanation under the help of man: Print the real and valid user and group IDs.

id - print real and effective user and group IDs

Try to use id under root

(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

1 which python which is to find executable files in this path through the PATH environment variable, so the basic function is to find executable files

whereis python finds all files related to python files

locate python locate command to quickly find relevant files when searching the database

Find /usr/bin -name python performs disk search in the specified directory

(Note: which, whereis, locate, find all have the function of retrieval, combined with the execution results, find help information, and summarize the differences)

 

Compared with find, whereis is very fast to find, because the Linux system records all the files in the system in a database file. When using whereis and locate, which will be introduced below, it will find data from the database. Instead of searching by traversing the hard disk like the find command, the efficiency will naturally be very high.
However, the database file is not updated in real time. By default, it is updated once a week. Therefore, when we use whereis and locate to find files, we sometimes find data that has been deleted, or just create a file, but cannot find it. The reason It is because the database file has not been updated.

2.grep -n -E 'root|zhaoyi|^user*' /etc/passwd: /etc/passwd -n is to display the line number, the function of this command is to find the line with root or zhaoyi in the /etc/passwd file The first line, where * means repeated zero times or more ^ means it must appear at the beginning of the line
grep -n -E ' [[:digit:]] ' /etc/passwd: Find lines containing pure numbers in the /etc/passwd file
grep -n -E ' [[:alpha:]] ' /etc/passwd: find lines containing pure letters in the /etc/passwd file
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

In the experiment, the last few instructions understand the meaning but no display, it is being solved ------------ has been solved, there must be no spaces in the '' in the command line

 

3 sudo apt install gimp install software gimp

which gimp queries the location of gimp

sudo apt remove gimp uninstall gimp

which gimp queries the location of gimp, there are no query results at this time

 

4 ls -dl /root --time-style=long-iso示

ls -dl /root --time-style=long-iso | cut -d' ' -f1,8

Comparative observation

s -dl /root --time-style=long-iso Display file information, time is displayed in full IOS time format

ls -dl /root --time-style=long-iso | cut -d' ' -f1,8 Use spaces as separators to display fields 1,8 in the output before the pipeline

 

5 ls -l --time-style=long-iso

ls -l --time-style=long-iso -t

ls -l --time-style=long-iso -t -r

Comparative observation

ls -l --time-style=long-iso,: ls -l is to list the details of the file, the time is set to long-iso format, the default sorting
ls -l --time-style=long-iso -t: The most recent time is at the top
ls -l --time-style=long-iso -t -r: The time is at the back

6ls /usr/share/man: Display the contents of the /usr/share/man directory

ls /usr/share/man | grep man[1-8]: Search the /usr/share/man directory for man1~man8
ls /usr/share/man/man1: Display the directory
file under /usr/share/man/man1 /usr/share/man/man1/ls.1.gz (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, copy ls.1.gz to
cd ~/temp under temp; ls: Go to the temp directory and view all file directories, this time there is a suffix .gz
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

 

7 ls -l /home | grep "^d" | wc -l View lines starting with d under home and count the number of lines

 

8 sudo adduser user7 creates user user7

ls /home | tee users | wc -l put home

Here I have created user7 and I found it difficult to delete

Still not deleted, then manually delete

 

(4) Write the corresponding shell command as required 

1 Search for the existence of the file signal.h in the directory /usr/include (hint: find command)

2 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: 1 use grep and wildcard *; 2 use error message redirection and special device file /dev/null)

 

There may be some problems with the following redirection. There is no error message in this program, so it is difficult for me to judge whether it is correct or not. I will study it later.

The verification is correct, I use 1> to redirect to a file and then check the file to find that the rules match

I feel that the processing speed of the following method is faster than that of the first method, because it is directly searched and no parameters are passed. This is just my guess and will be verified later.

3 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 regular expressions)

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

(Hint: Combine cut, pipeline and sort commands)

# -n is to sort by number, -r is in reverse order, -k is to specify the column that needs to be sorted, -t specifies that the column separator is a colon

(5) Experience awk, sed usage: execute commands in sequence and observe the execution results

1 cp /etc/apt/sources.list t1; less t1

2 sed -e "s/#.*//g" t1

3 sed -e "s/#.*//g" t1 | awk '{if (length != 0) print $0}'

4 tail -5 /etc/passwd | awk -F: '{print $1}'

5 tail -5 /etc/group | tee t2

awk 'BEGIN{print "file t2"} {print "line" NR ":" $0} END {print "over"}' t2

A preliminary study of several commands:

sources.list: information such as system address updates in linux. In my understanding, this file can be changed, because often due to network reasons, the source needs to be changed to the source of NetEase or the source of Ali

sed: I haven't used it before, a brief description on the Internet: sed is a stream editor. It is a very useful tool in text processing. It can be used perfectly with regular expressions and has extraordinary functions. When processing, the currently processed line is stored in a temporary buffer, called "pattern space", and then the sed command is used to process the contents of the buffer. After the processing is completed, the contents of the buffer are sent to the screen. The next line is then processed, and this repeats until the end of the file. The file contents are not changed unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.

Awk: Awk is a powerful text analysis tool. It is particularly powerful when analyzing data and generating reports. Simply put, awk reads the file line by line, and slices each line with spaces as the default separator. The open part is then subjected to various analysis and processing.

curl is an open source file transfer tool that works from the command line using URL syntax.

wget is a free tool that automatically downloads files from the Internet. It supports downloading through the three most common TCP/IP protocols: HTTP, HTTPS, and FTP, and can use HTTP proxy. The name "wget" comes from the combination of "World Wide Web" and "get".

The so-called automatic download means that wget can continue to execute in the background after the user exits the system until the download task is completed.

 

 practise

Combined with the running results, analyze the script code, and summarize the following contents in the experimental report:

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

The -n option can set the read command to record the specified number of input characters. When the input characters reach 11, it will automatically exit and assign the input data to the variable.

2 Write the function of line14

Compare the same string in the filename with the content in the array code, and redirect the same string to the major to provide a basis for conditional statement judgment

 

 

Summary: Very good experimental design, this experiment is really fulfilling, I learned a lot of commands, and every detail is involved. It is very helpful to me, and it is much more efficient than messing around with no direction. Next time I must do the experiment earlier, this time the experiment will not be done until the last day, the procrastinator is really, okay, come on! Regular expressions don't know how to find and practice by yourself! ! !

Guess you like

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