Lab four shell (2)

Source: Internet
Author: User

    1. Iv. Steps of the experiment
1. Shell   basic usage of variables and usage of common symbols

This section requires writing out the shell commands that implement the requirements , showing

(1) Change the main prompt to the user's home directory name

( Hint: reference material 4.6.8 section Environment variables PS1 and home usage)

(2) Assign the string DOS file c:>\ $student \* to the variable x and display it

(Tip: Note the choice of quotation marks while ensuring that multiple spaces, $, *, and so on in the string are displayed exactly as they are)

(3) in the Shell command Terminal input likes= (Cosmos Galaxy Moon); Likes[6]=mars, and then uses echo to display the values of the following expressions, together with the result, to write out the function of the expression.

①${likes[*]}

②${likes[@]}

③${#likes [*]}

④${#likes [@]}

⑤${#likes}

⑥${#likes [0]}

(Hint: Refer to the textbook 4.6.2 section and section 4.6.3)

①${likes[*]} represents the value of all non-empty elements in the array likes

②${likes[@]} represents the value of all non-empty elements in the array likes

③${#likes [*]} indicates the number of elements in the array likes

④${#likes [@]} indicates the number of elements in the array likes

⑤${#likes} indicates the character length of the first 0 elements in the array likes

⑥${#likes [0]} indicates the character length of the 0 element in the array likes

(4) In the shell command Terminal input Name=phoenix, and then use the Echo display expression ①, observe the results, and then enter the command unset name, and thenenter the expression ① observation results. Combine two results to write the function of the expression.

①${name:-hello}

(Tip: Refer to the textbook, section 4.7)

①${name:-hello} indicates that if name is empty, output Hello, otherwise The value of name

(5) Enter Name= '/usr/share/doc/apg/php.tar.gz ' at the shell command terminal, then use echo to display the values of the expression ① and ② respectively , and observe the results.

Modify The value of name, let name= '/etc/apt/sources.list.d ', and again use echo to display the values of the expression ① and ② respectively , and observe the results. Combining the results, write out the function of the expression.

①${name%%.*}②${name%.*}

(Hint: reference material 4.6. Section 3)

①${name%%.*}. * matches the end value of name , then the matching portion is removed, the value of the remainder is output, and The Percent is the most matched portion of the minus part.

②${name%.*}.* matches the end value of name , the matching portion is removed, the value of the remainder is output, andthe% is the least matched part of the minus part.

(6) Enter name=/usr/bin/x11 at the shell command Terminal, and then use echo to display the values of the expression ① and ② respectively , and observe the results. Modify The value of name, let name= '/etc/apt/sources.list.d ', and again use echo to display the values of the expression ① and ② respectively , and observe the results. Combining the results, write out the function of the expression.

①${name#*/}②${name##*/}

(Hint: reference material 4.6. Section 3)

①${name#*/} / * matches the beginning of name , then the matching portion is removed, the value of the remainder is output,# means that the minus part is the least matched part

②${name##*/} / * matches the beginning of name, then the matching portion is removed, the value of the remainder is output,# # represents the most matched portion of the minus part.

(7) the address of the blog post page that is known to be submitted by a classmate is as follows:

Address= ' http://www.cnblogs.com/xyz/p/8569001.html ' through string matching, how to get its blog home address: homepage= ' http://www.cnblogs.com/xyz '

write The shell command implementation that gets the variable homepage from the address of the variable.

(Tip: Refer to the pre-order exercise (5))

2. Shell   Script Analysis

(1) Write the shell script file ex1.sh using Vim/gedit, which reads as follows: (Textbook P145 Study Questions 4.8)

Run the script 3 times at the Shell command Terminal to see the results of its operation.

① run for the 1th time:. ex1.sh

② run for the 2nd time:. ex1.sh Best wishes

③ Run for the 3rd time:. ex1.sh God Bless u

Combine 3 run results, analyze script code, summarize the following in the lab report:

① what is the function of the entire shell script?

What is the function of ② line 8th , Line 9 , line 11?

③ each run, the while loop in the script executes several times, respectively?

④ run . ex1.sh God Bless u when the while loop body is executed for the 1th time, the value of the variable cmd after the 8th line executes?

① output the input parameters in reverse

② Line 8 refers to the number of input parameters to cmd, the 9th line refers to each cycle, the number of $count decreased by 1, and the 11th line is the run cmd command.

③ First time 0 times, second 2 times, third time 3 times

④echo

(2)

Use Vim/gedit to write the shell script file ex2.sh, which reads as follows: (Textbook P145 4.10)

At the command terminal input . ex 2 . SH J s J, (when actually executing, replace JSJ with the login user name on your computer) combined with the results and help information, answer the following questions.

①line6, who | grep "^$1" function?

②line8~10 function?

① determine if there are users that match the input parameters

② redirects the first parameter cy to limitstring and sends Hello Hadoop to it

(3) use vim/gedit to write the shell script file ex3.sh, which reads as follows:

① Displays the result of the run.

At the command terminal input . ex3.sh, combined with results and help information, answer the following questions.

② what is the function of this script?

② The function of the script is to copy all the files in the current directory and append them to the file name. backup--Current date-current time

(4) use vim/gedit to write the shell script file ex4.sh, which reads as follows:

Run the script 3 times at the Shell command Terminal to see the results of its operation.

① run for the 1th time:. ex4.sh

② run for the 2nd time:. ex4.sh /etc/apt/sources.list

③ Run for the 3rd time:. ex4.sh /etc/apt/sources.list ex4.sh

Analyze the shell script function with the result of running . Reply:

What is the function of NL in ①line4-10?

reads input parameters into X , adds one line number per read, and outputs line number and x value

②line14, when a condition test is satisfied, what is the function of the script execution?

Execute function NL

③LINE17-LINE21, what is the function of this part of the script?

pass parameters to the NL calls, and then shift removes this parameter, and the remaining parameters move left one

3. Shell   Script Writing

This section requires that the shell script that implements the corresponding requirements be posted and the execution and results of the script are displayed

(1) Chapter 4 Study Questions 4.9

Write a shell script that copies the second positional parameter and the file specified by each subsequent parameter to the directory specified in the first positional parameter.

(2) Chapter 4 Study Questions 4.11

Prints some files under the given directory, the first parameter indicates the directory where the files are located, and the remaining parameters are the names of the files to be printed.

(3) Chapter 4 Study Questions 4.12

use A for loop to move the. c file under the current directory to the specified directory and sort by file size to display the contents of the specified directory after the move.

(4) Chapter 4 Study Questions 4.18

(Tip: Use the Cut command and the appropriate options to consider the location parameters)

Design a program cuts, which reads data by standard input, obtains data bounded by the first parameter N and the second parameter m, both N and m are integers, that is, all characters (including both characters) from the nth character to the M character in the input string. For example:

$cuts 11 14

This was a test of cuts program (input)

Test (Result)

(5) Guess the specific requirements of the number game are as follows:

① Write a function random0_100 generate random numbers between 0~100

② users from the keyboard input guessing numbers, if wrong, prompting the user to guess the number is too large or small, the user continues to guess until the right to guess.

(Hint: Shell internal system variable random generates an integer number between 0~32767)

Five, summary and experience

in the experiment, I found a lot of commands I still can not master, often forget, so I want to more familiar with the book and PPT on the order.

Lab four shell (2)

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.