Experiment 3 - shell programming

  In addition to appearing as an interpreter, the shell is also a high-level programming language with variables, keywords, and various control statements.

Command History

history displays a list of historical commands, including its own history commands

 

history 50 displays the last 50 lines of history commands

 

/ect/passwd User account information

/etc/shadow optional encrypted password file

/etc/passwd - Backup file for /etc/passwd

/etc/group A text file that defines groups on the system

/etc/gshadow shadowed group file

 

id prints the real and effective user and group IDs

id -u print only valid user IDs

 

which: displays the directory where the system command is located, directly locates the absolute path of the command, while viewing the directory where the command is located, you can also see the alias of the command

whereis: Displays the directory where the command is located and the path of the command's help documentation. While viewing the directory where the command is located, you can also see the location of the command's help documentation.

locate: find files and directories, much faster than find

find: is a general command to find files, it can be used to find the path where all files are located

 

grep: search text using regular expressions and print matching lines

-n Indicates the column number before displaying the column that conforms to the template style.

-E converts the template style to normal notation for use

 

gimp

 

Display file directory information, detailed information, and time according to iso standard output

Truncate the first and eighth fields of the previous instruction with a space as a delimiter

 

 

 

 

 

 

 

 gzip

It can be used to compress large, less-used files to save disk space, and can also be used with the tar command to form the compressed file format in the Linux operating system. There are two advantages, awareness can reduce the space, and the second is that when transferring files over the network, it can reduce the transfer time.

 

Find files starting with d and count the number of lines

 

Add a new user

 

(4) Write the corresponding shell command as required

① Search for the existence of the file signal.h in the directory /usr/include (hint: find command)

 

② Find the line containing 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 error messages are filtered. (Hint: ① use grep and wildcard *; ② use error message redirection and special device file /dev/null)

 

③ Find the user information record whose login shell is bash in the user name and password file /etc/passwd, and display the line number page 3/5 (hint: use grep and $ in regular expressions)

 

④ Intercept the first column (group name) and the third column (group id) from the /etc/group file, and sort them according to the numerical value of the group id number from small to large. (Hint: Combine cut, pipeline and sort commands)

 

 

(5) Experience the usage of awk and sed: execute the commands in sequence and observe the 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 $0}'

 

④ tail -5 /etc/passwd | awk -F: '{print $1}'

 

⑤ tail -5 /etc/group | tee t2 awk 'BEGIN{print "file t2"} {print "line" NR ":" $0} END {print "over"}' t2

 

 

(6) Check the network or help to experience the usage of commands curl and wget.

1. curl (file transfer tool)

Common parameters are as follows:
-c, --cookie-jar: write cookie to file
-b, --cookie: read cookie from file
-C, --continue-at: resume from breakpoint
-d, -- data: transmit data in http post mode
-D, --dump-header: write header information to file
-F, --from: simulate http expression to submit data
-s, --slient: reduce output information
-o, -- output: output information to the file
-O, --remote-name: according to the file name on the server, there is a local
--l, --head: only return header information
-u, --user[user:pass]: Set http authentication user and password
-T, --upload-file: upload file
-e, --referer: specify reference address
-x, --proxy: specify proxy server address and port
-w, --write-out: output Specify the format content
--retry: the number of
retries --connect-timeout: specify the maximum time/s for trying to connect

 

2. wget (file download tool)

Common parameters are as follows:
2.1 Startup parameters
-V, --version: display the version number
-h, --help: view help
-b, --background: go to the background to execute after startup
2.2 Logging and input file parameters
-o, - -output-file=file: write records to file
-a, --append-output=file: append records to file
-i, --input-file=file: read url from file to download
2.3 Download parameters
-bind-address=address: specify the local address
-t, -tries=number: set the maximum number of attempts to connect
-c, -continue: then download the files that have not been downloaded
-O, -output-document=file: Write the download content to the file file
-spider: do not download the file
-T, -timeout=sec: set the response timeout time
-w, -wait=sec: the interval between two attempts
--limit-rate=rate: Limit the download rate
-progress=type: set the progress bar
2.4 Directory parameters
-P, -directory-prefix=prefix: save the file to the specified directory
2.5 HTTP parameters
-http-user=user: set the http username
-http-passwd=pass : set http password
-U, --user-agent=agent: masquerade agent
-no-http-keep-alive: close http active links, become permalinks
-cookies=off: do not use cookies
-load-cookies=file: before starting session Load cookies from file -save - cookies = file
: save cookies to file at session end , --accept=list: semicolon separates list of downloaded extensions -R, --reject=list: semicolon separates list of not downloaded extensions -D, --domains=list: semicolon separates downloaded domains list --exclude-domains=list: semicolon-separated list of domains not to be downloaded







 

3. Write a shell script and execute it in 4 ways (refer to Chapter 4 Teaching Materials/Courseware).

practice one

 

 

 

 

Exercise 2

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214493&siteId=291194637