Shell Programming (1)

Source: Internet
Author: User
Tags curl ftp protocol

Shell Command Application Exercise

(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/shadow (Hint: View this file description description via Man 5 shadow)

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

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

Password file function: This directory stores the operating system user information, which is visible to all users

The shadow file is responsible for all user passwords

Group files are files that the system administrator manages for users and user groups, and all information about the Linux user group is stored in the/etc/group file. A user group is a collection of users with a common feature. The user group profile mainly has/etc/group and/etc/gshadow, where/etc/gshadow is the/etc/group encrypted information 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

①id②id-u③id-u root④id-u JSJ

The D command can display the actual valid user ID (UID) and group ID (GID)

-U or--user displays the user ID.

(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

The locate command is not a real-time lookup, so the results of the lookup are inaccurate, but the lookup is fast. Because it does not look for a directory, it is a database (/var/lib/locatedb), which contains all the local file information. The Linux system automatically creates this database and updates it automatically once a day, so you can't find the latest changed files using the Locate command. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate.

The which command is to find out if a command exists, and where the command is stored.

The Whereis command can only be used to search for program names, and only binary files (parameter- b), man description file (parameter-m), and source code file (parameter- s) are searched. If the argument is omitted, all information is returned.

②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

with Option -E is an extended regular expression, without options-e is a regular regular expression

③sudo apt Install gimp

Which GIMP

sudo apt remove gimp

Which GIMP

Installing GIMP

Find success

Uninstalling GIMP

Find failed

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

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

Ls-dl/root--time-style=long-iso file directory information, details, time information for long-iso format

Ls-dl/root--time-style=long-iso | Cut-d '-f1,8 to intercept the first and eighth drwx-------/root with a space separator

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

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

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

Ls-l--time-style=long-iso Default sort

Ls-l--time-style=long-iso-t by time from near to far

Ls-l--time-style=long-iso-t-R by time from far to near

⑥ls/usr/share/man display its next directory

Ls/usr/share/man | grep man[1-8] Search man1-8 in the selected directory

Ls/usr/share/man/man1

File/usr/share/man/man1/ls.1.gz Show ls.1.gz information in this directory

mkdir ~/temp; Create a Directory

cp/usr/share/man/man1/ls.1.gz ~/temp copy files to the past

CD ~/temp; LS enters the directory and displays

sudo gzip-d ls.1.gz; LS Unzip the file and display

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

Ls-l Find the file in the/home directory,grep "^d" filters the file at the beginning of the line to D ,wc-l Number of rows of statistics

⑧sudo adduser user7 Create user

Ls/home | Tee Users | Wc-l Find user statistics in the home directory

(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
3rd page/All 5 pages
(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)

(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

purpose of file /etc/apt/sources.list:

/etc/apt/sources.list is the package management tool, Apt The configuration file used to record the package warehouse location, save the Ubuntu The address of the source server for the software update.

SED is a Linux instruction that can process and edit text files in accordance with script instructions, similar to awk, except that the SED is simple, the function of column processing is less, awk is complex and the function of column processing is powerful.

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

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

wget is a command tool for downloading files in a Linux system. This is an essential tool for users who frequently want to download some software or restore backups from a remote server to a local server. wget support Http,https and FTP protocol, can use HTTP proxy, can automatically download, support the continuation of the breakpoint, but also very stable.

Write a shell script that is executed in 4 ways (see chapter 4th, Teaching materials/courseware), respectively.

(1) Exercise 1

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

(2) Exercise 2

Line3 the read command option -n11 function: read read variable value N11 represents one bit.

Line14 function:$code: Professional number, is the 5~8 of the number of students , $filename for Major_code.txt, REDIRECT the professional number and file name to T1,T1 as the previous command output, and then as a command input, by T1 's command grep $code, from major_code.txt read the professional name to major.

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.