Experimental three shell programming (1)

Source: Internet
Author: User

One, Shell command application exercises

1. Describe the purpose of the following documents

①/etc/passwd

passwd is a password file that is used to manage the user's password. Ordinary users can usually only modify their own password information.

②/etc/shadow

Shadow is a shadow password file that contains password information for the system account and optional age information. Only administrators can view the changes.

③/etc/group

A group file is a profile of a user group that includes users and groups of users, and can show which user group or groups of users belong to, because one user can belong to one or more different groups of users, and users of the same user group have similar characteristics.

④/etc/gshadow

/etc/gshadow is a/etc/group encrypted information file, such as user group management password is stored in this file. /etc/gshadow and/etc/group are complementary two files; for large servers, for many users and groups, custom some relational structure more complex permissions model, set user group password is very necessary.

2. Enter the following command to observe the results of the operation. Combined with the man ID to see the Help information, point out each command function

The Id:id command can display a true and valid user ID (UID) and group ID (GID)

Id-u Show only User ID

Id-u root to view the user ID of root, which is 0

Id-u JSJ View JSJ This user's ID, the result is no JSJ this user.

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

①which python: Find executable file named Python in System path directory
The Whereis Python:whereis command can be used to locate binaries (commands), source files, and man files. Unlike which, this command can be found by using a file index database instead of path, so the face is much wider than which
Locate Python: Find files from a database. This command can find any file you specify to find, and you can enter only a partial file name.
Find/usr/bin-name python: Find a file named Python in the/usr/bin directory by searching the hard disk directly.

②grep-n-E ' root|cy|^user* ':/etc/passwd-n is the line number that is displayed in the/etc/passwd file to find a line at the beginning of root or CY, where * means more than 0 repetitions, and ^ indicates that the row must appear at the top.
Grep-n-E ' [[:d igit:]] '/etc/passwd: Find rows containing pure numbers in/etc/passwd file.
Grep-n-E ' [[: Alpha:]] '/etc/passwd: Finds lines containing pure letters in the/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.

③sudo apt Install GIMP: installs the software gimp.
Which gimp: Query the location of GIMP.
sudo apt remove gimp: Uninstall GIMP.
Which gimp: Query the location of GIMP, there is no query results at this time.

④ls-dl/root--time-style=long-iso:/root Displays the file directory information, details, and time in full iOS time format.
Ls-dl/root--time-style=long-iso | Cut-d '-f1,8: intercepts the first and eighth fields in the previous instruction with a space delimiter.

⑤ls-l--time-style=long-iso,:ls-l is a list of file details, the time is set to Long-iso format, and the default sort.
Ls-l--time-style=long-iso-t: Sort by time from near to far on the basis of the previous command.
Ls-l--time-style=long-iso-t-R: Sorted by time on the basis of the previous command, but changed from far to near.

⑥ls/usr/share/man: Displays 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: 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.

⑦ls–l/home | grep "^d" | Wc–l: In the home directory, look for rows that begin with D and count the number of rows.

⑧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 appropriate shell command as required

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

② finds the row containing the Bufsiz in all files in the/usr/include directory and displays the line number.
Requirements: Only the found results are displayed on the screen and the error messages are filtered.

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

④ intercepts the 1th column (group name) and 3rd column (group ID) from the/etc/group file and sorts it by the size of the group ID number from small to large.

5. Experience awk, SED usage: Execute commands sequentially to observe 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 $} '
④tail-5/etc/passwd | Awk-f: ' {print '} '
⑤tail-5/etc/group | Tee T2
awk ' begin{print ' file T2 '} {print ' line ' NR ': ' $} END {print ' over '} ' T2

Observe the results of the execution and summarize the following:

① file/etc/apt/sources.list Purpose:/etc/apt/sources.list is the package management tool APT uses the record package warehouse location of the configuration file, saved the Ubuntu Software update source server address.

The ② command tool sed function displays two or three of the SED command exercises that you have tried, and makes the necessary statements about the specific functions:

Delete the line that contains "2":

Change the first line to 0000000:

The ③ command tool awk function displays two or three of the SED command exercises that you have tried, and makes the necessary statement for the specific function.

Number of users in statistics/etc/passwd:

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

Where Size=size+$5, $ $ represents each row in the fifth column, and the fifth column is size, so write this.

6. Check the network or Help to experience command curl and wget usage

① Command Tool Curl function

curl+ URL, display the HTML of the URL.

Get Site Cookies

Save Web Page

② Command tool wget function

To download a single file:

Breakpoint Continuation:

Test your site for normal access:

Second, write a shell script, in 4 ways (see the 4th chapter of the textbook/courseware) are executed separately.

(1) Exercise 1

Way 1:bash<filename

Way two: bash filename [arguments]

Way 3:filename

Way 4:. FileName

(2) Exercise 2

Way 1:bash<filename

Way two: bash filename [arguments]

Way 3:filename

Way 4:. FileName

① write line3 the read command option-n11 function:

Read is used for reading variable values from the keyboard, and the 11n represents 11 bits.

② Write the LINE14 function

$code: Professional number, from the Line6 view is the number of the 5~8 11-digit, $filename is Line11 major_ Code.txt, the line code redirects the professional number and file name to T1,T1 as the preceding command output, and then as a command input, according to T1 's command grep $code, from Major_code.txt to read the professional name to Major.

Three, summary and experience

Through this Linux experiment, I stepped into a new field of--shell programming. Linux commands a wide variety of functions, small but complete, assembled very powerful, can achieve a variety of functions, to build a new world. However, the experimental process is very hard, often less input space or some strange errors. A lot of new knowledge has been gained through the contact with many commands that are not known. Recognize that they have a lot of shortcomings, unfortunately, usually do not have much time to improve themselves, but also need to redouble their efforts.

Experimental 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.