Lab Three: Shell programming (1)

Source: Internet
Author: User

Shell Command Application Exercise

(1) The use of the following documents is briefly reviewed through the Help information

/ETC/PASSWD records the user's account

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

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

/etc/gshadow Security group account information

View the file description for several commands and use cat to view the meaning of each field

(2) Enter the following command to observe the results of the operation. Combined with the man ID to see the Help information, pointed out that each article

Command function

1 ID Displays the ID of the user and the ID of the owning group.

2 Id-u Display the current user ID

3 Id-u root Displays the root user ID

4 id-u zhaoyi display Zhaoyi User ID

Explanation with the help of man: printing is really and effectively a user and group ID.

Id-print Real and effective user and group IDs

Try to use ID again under root

(3) In the shell command terminal, enter the following command, observe the execution results, understand the specific functions of each command implementation

The 1which python which is the PATH environment variable to find the executable file within it, so the basic function is to find the executable file

Whereis python will look up files related to Python files

Locate python Locate command to quickly find relevant files when searching for a database

Find/usr/bin-name python for disk lookups in the specified directory

(Description: Which,whereis, locate, find all have the function of retrieving, combine execution result, find help information, summarize its difference)

Whereis looks very fast compared to find, because the Linux system records all the files in the system in a single database file, and when you use Whereis and the locate described below, the data is looked up from the database, not like the Find command, By traversing the hard drive to find, the efficiency will naturally be very high.
However, the database file is not updated in real time and is updated once a week by default, so when we use Whereis and locate to find files, we sometimes find data that has been deleted, or just created the file, but cannot find it because the database file is not updated.

2.grep-n-E ' root|zhaoyi|^user* '/etc/passwd:/etc/passwd-n is the line number that is displayed in the/etc/passwd file to find a line with the beginning of root or Zhaoyi, where * means repeat more than 0 times ^ indicates must appear at the beginning of the line
Grep-n-E ' [[:d igit:]] '/etc/passwd: Find rows containing pure numbers in/etc/passwd file
Grep-n-E ' [[: Alpha:] '/etc/passwd: Find lines containing pure letters in/etc/passwd file
Grep-n ' [0-9]\{4,\} '/etc/group because it does not add-e, so use curly braces with escape character \{\}, [0-9] equivalent to [[:d Igit:]], find the pure number, 4 means more than 4

In the experiment, the following instructions understood the meaning but did not display, is resolving----------has been resolved, in the command line "must not have spaces

3 sudo apt install gimp installation 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 Show

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 is delimited by a space, displaying 1, 8 fields in the output result of the pipeline line

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 a list of file details, the time is set to Long-iso format, the default sort
Ls-l--time-style=long-iso-t: The most recent arrangement of time in front
Ls-l--time-style=long-iso-t-R: Time recently in the back

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

Ls/usr/share/man | grep Man[1-8]:/usr/share/man Directory Search MAN1~MAN8
Ls/usr/share/man/man1: Displays the directory under/usr/share/man/man1
File/usr/share/man/man1/ls.1.gz (combined with execution results, review2.8.1 section gzip command content)
mkdir ~/temp; cp/usr/share/man/man1/ls.1.gz ~/temp: Create temp folder under root directory, copy ls.1.gz to Temp
CD ~/temp; LS: Go to the temp directory and look at all the file directories with a suffix. gz
sudo gzip-d ls.1.gz; LS: Unzip and view the directory, at which time. GZ disappears, indicating that the package has been unzipped and deleted

7 Ls–l/home | grep "^d" | Wc–l in Home view lines starting with D and counting the number of rows

8 sudo adduser user7 create user User7

Ls/home | Tee Users | Wc-l put the home under

Here I set up user7 I found it hard to delete

Still not deleted, and then manually removed by

(4) write the corresponding shell command as required

1 Search for files under directory/usr/include signal.h exists (hint: find command)

2 Find the line containing the 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 the error messages are filtered.

(Tip: 1 using grep and wildcards *; 2 using error message redirection and special device files/dev/null)

There may be some problems with subsequent redirects, there is no error message in this program, so it is difficult to determine whether it is correct, and so on later

Verify correctly, I redirect to a file with 1> and then view the file discovery rule that matches

The following method in the processing speed I feel faster than my first, because he directly looked for, did not pass parameters, this is just my guess, and later also to verify.

3 in the user name password file/etc/passwd, look for the login shell to bash user information record, and display the line number

(Hint: use grep and $ in regular expressions)

4 Intercept the 1th column (group name) and 3rd column (group ID) from the/etc/group file and sort by the size of the group ID number from small to large.

(Hint: Use cut, pipe line and sort command in combination)

#-N is sorted by numeric size,-R is in reverse order,-K is the field that specifies the sort of love that needs to be sorted,-t specifies that the field delimiter is a colon

(5) Experience awk, sed usage: Execute commands sequentially to observe 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 $} '

4 tail-5/etc/passwd | Awk-f: ' {print '} '

5 Tail-5/etc/group | Tee T2

awk ' begin{print ' file T2 '} {print ' line ' NR ': ' $} END {print ' over '} ' T2

A tentative study of several commands:

Sources.list:linux update the system address and other information, in my understanding this file can be changed, because often because of network reasons, need to change the source of NetEase or Ali source

SED: Not used before, the online brief: SED is a stream editor, it is a very medium of text Processing tool, can perfect the use of regular expression, the function is extraordinary. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

Awk:awk is a powerful text analysis tool that is particularly powerful when analyzing data and generating reports, and simply saying that awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

Curl is an open source file Transfer tool that works with URL syntax in the command line mode.

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

The so-called automatic download, refers to wget can after the user exits the system in the background to continue execution until the download task is complete.

Practice

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

1 Write out Line3 in the Read command option-n11 function

The-n option sets the read command to record the specified number of input characters, automatically exits when the input character reaches 11, and assigns the input data to the variable.

2 Write the function of LINE14

The same string is compared to the content in filename in the array code, and the same string is redirected to major, providing the basis for judging the conditional statement

Summary: Very good experimental design, the experiment is really very substantial, learned a lot of orders, all the details are involved, for me to help a lot, more than their own direction of the mess fiddling efficiency is much higher. Next time I must do a little earlier experiment, this experiment to the last day to do, procrastination patients really, OK, come on! Regular expression did not understand their own to find practice exercise!!!

Lab Three: Shell programming (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.