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

(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 , then use Echo displays the values of the following expressions, respectively , and combine the results , writes the function of an expression.

${likes[*]} ${likes[@]} ${#likes [*]} ${#likes [@]} ${#likes} ${#likes [0]}

${likes[*]}: Get non-empty array element

${likes[@]}: Get non-empty array element

${#likes [*]}: Get the number of non-empty elements in the array

${#likes [@]}: Gets the number of non-empty elements in the array

${#likes}: Gets the length of the element labeled 0 in the array

${#likes [0]}: Gets the length of the element labeled 0 in the array

As follows:

(4) in the Shell Command Terminal input Name=phoenix , then use Echo To display an expression , observation Results ; then enter the command unset name , re-enter expression observation results. Combine two results to write the function of the expression.

${name:-hello}

Expression function: If the name variable is not empty, take the value of the variable name, otherwise the value is Hello

As follows:

(5) Enter Name= '/usr/share/doc/apg/php.tar.gz ' in the shell command terminal and use echo to display the expression separately and the values, observe the results.

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

${name%%.*} ${name%.*}

${name%%.*}:name the end of the value if and '. ' Match, remove the most matched part, and the rest is the value of the expression

The value of ${name%.*}:name if and '. ' Match, remove the least matched part, and the rest is the value of the expression

As follows:

(6) Enter name=/usr/bin/x11 at the shell command terminal and use echo to display the expression separately and the values, 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.3 section)

${name#*/}: If the name value begins with '/', then the least matched portion is removed, and the remaining value of the expression

${name##*/}: If the name value begins with '/', the portion that matches the most is removed, and the value of the expression is left.

As follows:

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

address= ' http://www.cnblogs.com/xyz/p/8569001.html ' how to get their blog home address through a string match : homepage= ' http://www.cnblogs.com/xyz '

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

Use a homepage=${address%/*};homepage=${homepage%/*} to get the homepage address of the blog

As follows:

2. Shell Scripting Analysis

(1) Use Vim/gedit to write the shell script file ex1.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:. 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?

Outputs the input string in reverse order, with the number of loops as the input parameter

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

Line 8th: Append the value of $ and count to the CMD variable

Line 9th: Count self minus 1

Line 11th: Scan two times, first replace $ and the value after it with the corresponding location parameter, the second scan executes the display command

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

2 plays

3 plays

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

echo $

The results are as follows:

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

At the command terminal input. Ex2.sh JSJ, (when actually executing, replace the JSJ with the login user name on your computer) to combine the results and help information to answer the following questions.

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

First find the user information of the current user, and then find and enter the same user information in the results of the first parameter

②line8~10 function?

REDIRECT the first parameter of the input string to limitstring to send Hello Huxingheng, and then display the user information that was found

The results are as follows:

(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 ending with '. Sh ', named by: Original filename + ". backup-"+ year + month + day +"-"+ hours + minutes"

( 4) Use Vim/gedit Writing Shell script File ex4.sh, the contents are as follows:

in the Shell command Terminal points run the script 3 times to see the results of the run.

① First 1 Times Run:  . Ex4.sh

② First 2 Times Run:            . Ex4.sh/etc/apt/sources.list

③ First 3 Times Run:            . Ex4.sh/etc/apt/sources.list ex4.sh

combined with running results, the analysis shell scripting features. Answer:

What is the function of NL in line4-10 ?

Read the file, output the value of the line+1 (the line number here) and the line contents

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

Call function NL, because the parameter is not passed, so the output is empty and cannot be terminated

Line17-line21, what is the function of this part of the script ?

Use the loop and function NL to display the contents of all the files entered by the input string to the screen

(when the input string parameter is not empty and the input string argument is a file, call the loop, call the function NL, pass the argument to the first argument, then destroy the first parameter, take the second argument as the first argument, and then loop)

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) 4th Chapter Study Questions 4.9

Program:

#!/bin/bash

count=$#

For ((i=2;i<= $count; i++))

Do

Eval CP \$ $i $

Done

(2) 4th Chapter Study Questions 4.11

Program:

#!/bin/bash

function NL () {

While read X

Do

echo "$x"

Done

}

If [$#-le 1];then

echo "Error"

Else

Fn=$1

While ["$"] && [-F "$FN $"]

Do

NL < $

Shift

Done

Fi

(3) 4th Chapter Study Questions 4.12

Program:

#!/bin/bash

For file in *.c;do

MV $file/home/huxingheng/cfile

Done

Ls-s/home/huxingheng/cfile

(4) 4th Chapter Study Questions 4.18

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

Program:

#!/bin/bash

Echo-n "Please input a string:"

Read Str

echo $str | Cut-c $1-$2

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

Code:

#!/bin/bash

Read-p "Please input a number between 0 and:" x

temp=$ ((random%100))

While [$x-ne$temp]

Do

echo "fail! Try it again! "

If [$x-gt $temp];then

Read-p "Please input anumber smaller:" x

Else

Read-p "Please input anumber larger:" X

Fi

Done

If [$x-eq $temp];then

echo "Congratulations!"

Fi

Summary: This experiment took a long time to complete it, unfortunately, did not complete it in the time required for the exam. Do the process used in the knowledge points feel very strange, only when they found the lack of knowledge points, almost learning while doing, although deepened the impression, but this exam is definitely unqualified, but also need to strengthen the practice and knowledge points summary.

--the end--

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.