Lab Four Shell programming (2)

Source: Internet
Author: User
Tags echo display

First, the experimental steps

Basic usage of 1.shell variables and usage of common symbols

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

Export ps= $HOME

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

(3) In the shell command Terminal input likes= (Cosmos Galaxy Moon); Likes[6]=mars, and then use Echo to display the following expression, respectively
Value, and combine the results to write the function of the expression.

Likes= (Cosmos Galaxy Moon); Likes[6]=mars:like are arrays, with elements of Cosmos Galaxy Moon, and the 5th element Mars
①${likes[*]}: Displays all non-empty elements in the likes array

②${likes[@]}: Displays all non-empty elements in the likes array

③${#likes [*]}: Statistics likes number of non-empty elements

④${#likes [@]}: Count the number of elements in likes

⑤${#likes}: Count all elements in likes

⑥${#likes [0]}: Displays the length of the element (Cosmos) in the array like subscript 0

(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,
Re-enter the expression ① observe the results. Combine two results to write the function of the expression.

Assign Phoenix to name

First echo ${name:-hello}, the name value is Phoenix, which outputs Phoenix. The unset then deletes the name, Echo ${name:-hello} again, and outputs hello.

(5) Enter Name= '/usr/share/doc/apg/php.tar.gz ' in the shell command terminal, and then use echo to display the values of the expression ① and ② respectively.
Observation 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.
Combine the results to write the function of the expression

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

${name%%.*}: Deletes the longest portion of the. Match from the tail of name and returns the remainder

${name%.*}: Deletes the smallest part of the. Match from the tail of name and returns the remainder

Principle Ibid.

(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##*/}

${name#*/} removes the smallest part from the header of name and/or matches, and then returns the remaining part

${name##*/} removes the longest part from the header of name and/or matches, and then returns the remaining 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.

2. Shell Scripting Analysis

(1) Writing shell script files using Vim/gedit ex1.sh

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?

Answer: Output all parameters in reverse order.
What is the function of ② line 8th, line 9th, and 11th?

A: The 8th line: After the parameter value of cmd ' echo ', append the $ sign (by \ Protection not parsed) + input parameter number (count=$#, here's $ #是参数个数. )

Line 9th: Number of Arguments -1,expr command for numeric operations

Line 11th: Execute the command in cmd

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

A: The first 0 times, the input parameter is 0 does not execute

Second time 2 times, best wishes, two parameters, Count starts at 2, performs a while change 1, performs a second change 0,cmd to echo $

Third time 3 times, God bless u principle Ibid.
④ run. Ex1.sh God bless u, while the loop body is executed for the 1th time, the value of the variable cmd after the 8th line executes?

Answer: Echo $

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

At the command terminal input. ex2.sh CY

Answer the following questions.
①line6, who | grep "^$1" function?

Answer: Determine if the user does not exist and the input parameter matches
②line8~10 function?

A. Redirect the first parameter Cy to limitstring and send it hello Cy

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

At the command terminal input. Ex3.sh, combined with results and help information, answer the following questions.
① Displays the result of the run.

② what is the function of this script?

Copy all files in the current directory and append backup--date-time after new file name

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

Answer: Save user input parameters to x, add line number output
②line14, when a condition test is satisfied, what is the function of the script execution?

Answer: Execute function NL
③LINE17-LINE21, what is the function of this part of the script?

A: Pass parameters to the NL call, then shift to delete this parameter, and the rest of the arguments to the left one bit.

3. Shell

(1) 4th Chapter 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) 4th Chapter 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) 4th Chapter 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) 4th Chapter Study Questions 4.18

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 number game
The specific requirements 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.

Second, summary and experience

Although the teacher said in class, "see if you can write in two hours, because the test is almost as difficult as this experiment" but I Leng spent 5 hours ... Always step into the hole when writing a script. It means I'm not proficient in Linux shell programming. I wish I could have practiced more skillfully before the exam.

Lab Four Shell programming (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.