Linux Lab three shell programming (1)

Source: Internet
Author: User
Tags aliases gz file root access

1. Practice command history, aliases, and special character usages in the shell (4th. 4.2, Section ~4.5)

(1) Defining aliases

The definition of an alias is that it is often enclosed in single quotation marks to prevent the shell from ambiguity about the content, such as whitespace and special characters.

(2) Special character usage

1. General wildcard characters

* (asterisk): matches any character 0 or more times (the dot in the file name and the slash in the pathname must show a match)

? (question mark): matches any one character

[Character Group]: matches any one of the characters defined by the character group

! (exclamation point): If it is immediately after the opening parenthesis of a square bracket [, the character that is not listed in this group of characters

2. Pattern expressions (between modes with | Separated

* (schema table): matches 0 or more occurrences of a pattern in a given pattern table

+ (Pattern table): Matches 1 or more occurrences of a pattern in a given pattern

? (Schema table): matches any one of 0 or 1 occurrences in the pattern table

@ (Mode table): The pattern given once in the matching pattern table

! (schema table): Out of a pattern in a given pattern table, it can match anything else.

3. Quotation marks

Double quotes: Characters enclosed in double quotation marks are the most common characters except $, inverted quotation marks ('), and backslashes (\)

Single quotes: The enclosed characters are all normal characters

Inverted quotation marks: the enclosed string is interpreted by the shell as the command line

Note: The inner layer needs to use a backslash \ escape form as ' \ ' if the inverted quotation mark is nested

2. Shell Command Application Exercise

(1) The use of the following documents is briefly reviewed through the Help information
1/etc/passwd Password file


2/etc/shadow a shadow password file


3/etc/group defines all the groups in the system including name, password, group ID, user list


4/etc/gshadow a shadow group file

(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
1 ID user group, UID, and all groups to which the user belongs
2 id-u valid User ID
3 id-u root User ID with root access
4 id-u Beimingfeng User ID with user name North wind



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

1.

Which Python:which is to find the executable file through the PATH environment variable to that route, so the basic function is to find the executable file

The Whereis python:whereis command is used for program name search and can only search binaries, man files, and source code files. If the argument is omitted, all information is returned.

The Locate Python:locate command can quickly find files when searching for a database, but the database may not be updated to search for information that has been deleted.

Find/usr/bin-name python: Search for Python under the/usr/bin folder with hard drive lookup based on conditional filtering, slower

The 2.grep command is a powerful text search tool that uses regular expressions to search for text and print matching lines

The four commands were:

Query for matching rows with any length character after/ect/passwd with a root,beimingfeng,^user of three cases

Query for matching rows with arbitrary length numbers under/ECT/PASSWD

Query for matching lines with any length of letters under/ECT/PASSWD

Query for matching rows with a string of 0-9 digits longer than 4, under/ECT/PASSWD

3

sudo apt install gimp: Install GIRP
Which GIMP: Displays the directory where GIMP is located under Usr/bin
sudo apt remove gimp: uninstalling GIMP
Which GIMP: Displays the directory where GIMP is now

4

Ls-dl/root--time-style=long-iso
Ls-dl/root--time-style=long-iso | Cut-d '-f1,8

Cut is used to intercept information, the above information can be seen as a total of 8 fields, the title of 1 and 8 domain

5

Ls-l--time-style=long-iso
Ls-l--time-style=long-iso-t time New in front
Ls-l--time-style=long-iso-t-R First Sort by time, then in reverse order

Sort-T: Sort by file and directory change time-r: Sorts and outputs the list of directory contents in the reverse order of the filenames;

6

Ls/usr/share/man querying files and folders in the/usr/share/man/directory
Ls/usr/share/man | grep man[1-8] Search for a file or folder in the/usr/share/man/directory followed by any number of 1-8
Ls/usr/share/man/man1 viewing files or folders under the/usr/share/man/man1 directory


Where: detailed type information for file/usr/share/man/man1/ls.1.gz ls.1.gz file
mkdir ~/temp; cp/usr/share/man/man1/ls.1.gz ~/temp copy files from/usr/share/man/man1 directory ls.1.gz to the newly created folder Temp
CD ~/temp; Ls
sudo gzip-d ls.1.gz; LS Decompression file ls.1.gz

7

Ls–l/home | grep "^d" | Wc–l View the/home directory, match the file that starts with D, and then count the numbers.

I don't know why this is happening.

8

sudo adduser user7 Add a user named User7, self-setting password and some basic information
Ls/home | Tee Users | Wc-l View and then count the number of users in the/home directory

(4) write the corresponding shell command as required
1 Search for files under directory/usr/include signal.h exists (hint: find command)

Find/usr/include-name signal.h


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.

Grep-n-E "*bufsiz*"/usr/include/* 2>/dev/null


3 in the user name password file/etc/passwd, look for the logged-in shell for Bash user information record and display the line number (hint: use grep and $ in regular expression)

Add $ at different locations and the results don't change


4 Intercept the 1th column (group name) and 3rd column (group ID) from the/etc/group file, and sort by the numeric size of the group ID number from small to large
Cat/etc/passwd|cut-d ': '-f1,3 | Sort-n-K 2-t ': '

5) Experience awk, sed usage: Execute commands sequentially to observe execution results
1 cp/etc/apt/sources.list t1; Less T1 copies the sources.list to T1 and displays the contents of the T1 on a split screen


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

1./etc/apt/sources.listIs the apt configuration file used by the package management tool to record the location of the package warehouse, as well as the files that are located there /etc/apt/sources.list.d/*.list .

File type (Archive type)

The first word of an deb entry deb-src indicates the type of package file that was acquired.

which

Deb
The file type is a binary precompiled package, typically the type of file we use.
Deb-src
The file type is the source code used to compile the binary package.
Warehouse address (Repository URL)

The second word of an entry is the address of the repository where the package resides. We can change the warehouse address for other locations closer to their own image to improve download speed.

Common Image Address List:

Debian
Https://www.debian.org/mirror/list
Ubuntu
http://wiki.ubuntu.org.cn/Source list
Release version (distribution)

Following the warehouse address is the release version. The distribution has two classifications, one is the specific code for the release, such as,, and so on, and the xenial trusty other is the release precise type of the distribution, such as, oldstable stable , testing and unstable .

In addition, there may be further designations after the release, such as xenial-updates , trusty-security , stable-backports etc.

Package Classification (Component)

After the release of the package is the specific classification, you can have one or more.

2.sed is a stream editor, which is a very useful tool in text processing and can be used perfectly in conjunction with regular expressions. 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.

3.awk is a programming language used to process text and data under Linux/unix. The data can come from standard input (stdin), one or more files, or the output of other commands. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under Linux/unix. It is used in the command line, but more is used as a script. Awk has many built-in features, such as arrays, functions, and so on, which are the same as the C language, and flexibility is the biggest advantage of awk.

Sed exercises:

1 printing odd lines of a T1 file

2 Add yellow to the next line of lines that start with Apple

awk Exercises:

1. Print the second-to-last field of this string

2. Print a file T1 the second and third fields of each row

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

The Curl:curl command is a file transfer tool that works under the command line using URL rules. It supports file uploads and downloads, so it is a comprehensive transfer tool, but traditionally, it is customary to call curl as a download tool. As a powerful tool, curl supports a wide range of protocols including HTTP, HTTPS,ftp, post, cookies, authentication, downloading some files from specified offsets, user agent strings, speed limits, file size, progress bar, and more. To do the web process and data retrieval automation, curl can wish a helping hand.

Curl Exercises:

Download a single file and print the output to standard output (STDOUT) by default

The Wget:wget command is used to download files from the specified URL. Wget is very stable, it in the bandwidth is very narrow and unstable network has a strong adaptability, if it is because of network download failure, wget will continue to try, until the entire file download is complete. If the server interrupts the download process, it will again be linked to the server to continue downloading from where it stopped. This is useful for downloading large files from servers that have limited link times.

Wget Exercises:

Use wget to download individual files

3. Write a shell script that is executed in 4 different ways (see Chapter 4th, Textbook/Courseware).

(1) Exercise 1
1th, using Vi/vim/gedit or another editor, write the shell script ex1.sh, which reads as follows:


The function of this script is to back up normal files that have been modified within the current directory for the last 1 days (that is, 24 hours).
Use the backup system's current datetime as the file name and compress the archive. The error message is written to Error_info during the backup.
Then view the backup document and the error message separately.
5 attempts (the online and book method types are slightly different):

This permission is not enough to know what the problem tried to put the script in/home under the 4 methods are all insufficient permissions (chmod or not).

(2) Exercise 2
1th, using Vi/vim/gedit or another editor, write the shell script ex2.sh, which reads as follows:

The 4th chapter introduces four ways to run the script.

1 Partial method prompt Insufficient permissions

2 program freezes, does not continue running

4. Experimental summary

This experiment compared to the first two times the time spent greatly increased, did a little more serious, or learned a little things. One night to do or too urgent, some things just a little look on the past.

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