Lab Four Shell Programming 2

Source: Internet
Author: User

1. basic usage of shell 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[*]} displays all values in the likes array

②${likes[@]} displays all values in the likes array

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

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

⑤${#likes} displays the length of the element in the array like subscript 0 (omitting the representation of subscript [0])

⑥${#likes [0]} displays an array of elements labeled 0

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

(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)

The value of the variable name is Phonenix, not NULL, so the value of ${name:-hello} is the value of name, which is Phonenix

Unset is used to delete a defined variable, the name value is empty after deletion, and the value of ${name:-hello} is Hello

(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)

The content after% or percent is matched to the end of the value of name, and the value of name removes the match to the value of the expression.

% indicates a minimum match, and percent is the maximum match.

(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的值的开头匹配, the value of name removes the value of the expression that matches the result.

#表示最少匹配, # #表示最多匹配.

(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 (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?

1. Reverse the output of a space-delimited string from the command line

2, line eighth output $ $count where $count is a number if the value of the count variable is 3 cmd=$3

The Nineth line is to subtract the count value one at a time

Line 11th eval $cmd cmd=$3 so equal to eval $ $ so that's the third string to enter the entire string

3. Several strings are executed several times

4, the eighth line after the execution of CMD is $

(2)


Writing shell script files using Vim/gedit ex2.sh, (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?

1, find out the current user's name passed to $

2. If the current user is not found, pass the limitingstring value to

(3) using vim/gedit to write the shell script file ex3.sh,


① 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?

This script feature copies all the. sh files under the current folder, with the new file name appended with the original filename. backup– current Time ".

(4) using vim/gedit to write the shell script file ex4.sh,

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?

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

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

1. Output line numbers and variables

2. Perform the NL function if no arguments are entered on the command line

3, when the input of the first parameter is a real file when reading into the read-in and then delete this parameter and continue to read the next parameter

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

(2) Chapter 4 Study Questions 4.11

(3) Chapter 4 Study Questions 4.12

(4) Chapter 4 Study Questions 4.18

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

(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)

Lab Four Shell Programming 2

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.