Shell Programming (1)

Source: Internet
Author: User

I. Review exercises

General wildcard characters

Curly Braces {}

Inverted quotes, single quotes, double quotes

output redirect > >>

Enter redirection < <<

User-defined variables

Two shell command exercises

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

①/ETC/PASSWD (Hint: View this file description description via man 5 passwd)

/etc/passwd storing the password file, each user in the file has a corresponding record line, it records some basic properties of this user, including: login name, optional encrypted password, digital user ID, number group ID, user name and comment field, user home directory, optional user command interpreter.

②/etc/shadow (Hint: View this file description description via Man 5 shadow)

/etc/shadow is a shadow of the passwd file, and the record line in the/etc/shadow file corresponds to one by one in/etc/passwd, but only the system administrator can modify and view it.

③/etc/group (Hint: View this file description description via Man 5 group)

/etc/group store information about a local user group

④/etc/gshadow (Hint: View this file description description via man 5 Gshadow)

/etc/gshadow is a/etc/group encrypted information file, such as the user group management password is stored in the 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

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 Hadoop is the user ID of the user Hadoop, which is 1000

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

which Python

Whereis python

Locate Python

Find/usr/bin-name python

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

which to view the location of an executable file

Whereis to view the location of a file

Locate View file locations with a database

Find actual search hard disk query file name

②grep-n-E ' root|jsj|^user* '/etc/passwd (hint: extended regular expression)

Grep-n-E ' [[:d igit:]] '/etc/passwd

Grep-n-E ' [[: Alpha:] '/etc/passwd

Grep-n ' [0-9]\{4,\} '/etc/group (hint: Find the GID in the user group file is a 4-digit and above group information record)

(Description: A regular expression with option-e representing an extension;-e is a regular regular expression)

grep commands can search for text through regular expressions and print rows that match, option-N to print line numbers, and-E to use multiple regular expressions.

③sudo Apt-get Install GIMP

Which GIMP

sudo apt-get remove gimp

Which GIMP

sudo apt-get install gimp commands can be installed GIMP

sudo apt-get remove gimp command to uninstall GIMP

which GIMP the location to view the file.

④ls-dl/root--time-style=long-iso

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

(Description: The option of the Cut command-D has spaces in the quotation marks; Hint: compare with previous line results)

option-D to customize the delimiter,-F displays the contents of the specified field, and the above command specifies the space as a delimiter, showing the contents of the 1th and 8th fields.

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

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

Ls-l--time-style=long-iso-t-R (Description: Execute in turn, compare to see, combine Help options)

⑥ls/usr/share/man

Ls/usr/share/man | grep Man[1-8]

Ls/usr/share/man/man1

File/usr/share/man/man1/ls.1.gz (combined with execution results, review2.8.1 section gzip command content)

file command to identify the file type and then create a new directory Temp and Will ls.1.gz Copy to Temp directory, gzip The command is to decompress.

⑦ls–l/home | grep "^d" | Wc–l

ls-l Find in /Home file under the directory, grep "^d" the filter line starts with D the file, wc-l number of rows of statistics

⑧sudo AddUser User7

Ls/home | Tee Users | WC-L (hint: Enter cat users to observe the contents of the file)

sudo adduser command is used to add a new user

(4) write the corresponding 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.

(Tip: ① uses grep and wildcard characters *;② to leverage error message redirection and special device files/dev/null)

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

(Hint: use grep and $ in regular expressions)

④ 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. (Hint: Use cut, pipe line and sort command in combination)

(Commands to write out the functionality of the document, including commands and results, as they are submitted)

(5) Experience awk, sed usage: Execute commands sequentially to observe execution results

①CP/ETC/APT/SOURCES.LISTT1; 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

(When submitting a document, review the network and summarize the following:

① file/etc/apt/sources.list Use, understand its content

/etc/apt/sources.list is the record used by the Package management tool apt

② command tool sed function, showing two or three self-attempted sed command exercises, and make necessary statement of specific functions

sed you can replace the characters with the practice of replacing Hello with Hi

③ Command tool awk function, showing two or three of their own attempts to practice the awk command, and specific functions to make necessary statements

Line Match Statement awk ' ' only single quotation marks can be used

Each line is divided by a space or tab, the output text of 1, 4 items;

awk-f:-F equivalent to built-in variable FS, specifying the split character, used in the practice ' , ' Split

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

(When submitting a document, review the network and summarize the following:

② Command Tool curl function, display the exercises you try and make necessary statement instructions

In Linux, Curl is a file transfer tool that works under the command line using URL rules, and is a powerful HTTP command-line tool that supports file uploads and downloads, and is a comprehensive transfer tool.

(1) Installing Curl

(2) when using curl without any options, such as Curl http://www.baidu.com , by default, a GET request is sent to get the link content to standard output.

 

(3) Show only HTTP headers, not file contents, use-I option

(4) Both HTTP headers and file contents are displayed, using the-I option.

(5) Save the link to a file. Use the > symbol to redirect the output to a local file.

② Command tool wget function, display the exercises you have tried, and make necessary statement instructions

wget is a tool for downloading files, which is used at the command line. wget Tools feature Perfect, support breakpoint download function, support FTP and HTTP download way, support proxy server and easy to set up.

(1) use wget to download a single file, the process will show a progress bar, including (download complete percentage, the downloaded bytes, the current download speed, the remaining download time)

(2) use Wget-o to download and save with a different file name. In practice, download a file and save it by name Download1

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

2nd, try the four ways that the 4th chapter describes, run the script, and understand the shell commands that are used synthetically in this example.

(2) Exercise 2

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

Try the four ways introduced in chapter 4th to run the script.

If you are prompted to "Cannot find file Major_code.txt", copy the Major_code.txt from the public Email experiment folder to the directory where ex1.sh is located and execute it again.

Note: File Major_code.txt, please do not make any changes, including format.

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

② write line3 the read command option-n11 function

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

-n11 indicates input 11 characters and end of input

② Write the LINE14 function

line14:grep $code $filename >t1 && read x major < T1

function: Search code and filename Redirect to T1, then output from T1 to major;

$code: The professional number is 5-8 digits in the 11-digit school number.

$filename is major_code.txt.

Four. Experimental summary

Through this lab exercise, I learned about shell programming and practiced a lot of shell commands. The variety of shell commands, there are many need to practice their own, in the course of practice will often appear a variety of errors, such as less space and so on. Shell programming is fun, but it takes more time to experience understanding.

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.