Lab Four Shell programming (2)

Source: Internet
Author: User

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

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

PS1 value restores default values after re-login.

(2) the string DOS file c:>\ $student \* assigning values to variables x , and showed up

If there are spaces in the assigned string, enclose them in double quotation marks or single quotes.

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

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

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

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

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

⑤${#likes}: Displays the number of all elements in the likes array

⑥${#likes [0]}: Displays the first element of the likes array

( 4 ) in Shell Command Terminal input Name=phoenix , and then use Echo Display the expression ①, observe the result, and then enter the command unset name , and then enter the expression ① observe the result. Combine two results to write the function of the expression.

${name:-hello} : If name NOT NULL, output name the value, if name is empty, the output Hello.

When first displayed, name is not empty, so the value of name is displayed, and the second time because unset name cancels the assignment operation, the name value is empty, and a hello is displayed.

( 5 ) in Shell Command Terminal input name= '/usr/share/doc/apg/php.tar.gz ' , and then use Echo display the values of the expressions ① and ②, respectively, and observe the results.

Modify name the value that lets name= '/ETC/APT/SOURCES.LIST.D ' , re-use Echo display the values of the expressions ① and ②, respectively, and observe the results. Combining the results, write out the function of the expression.

${name%%.*} : Looking forward from the end, removing name in all the "." The string with the largest length in the beginning of the string (. tar.gz) and output the modified name

${name%.*} : Looking forward from the end, removing name in all the "." The string with the smallest length in the beginning of the string (GZ) and output the modified name

( 6 ) in Shell Command Terminal input name=/usr/bin/x11 , and then use Echo display the values of the expressions ① and ②, respectively, and observe the results. Modify The value of name, let name= '/etc/apt/sources.list.d ', use echo again display the values of the expressions ① and ②, respectively, and observe the results. Combining the results, write out the function of the expression.

${name#*/} look backwards from the start, remove name in all the "/" The string with the largest length in the ending string (/usr/bin//etc/apt/) and output the modified name

${name##*/} look backwards from the start, remove name in all the "/" The string with the smallest length in the ending string (/   /) and output the modified name

( 7 ) The address of the blog post page that a classmate has submitted Address 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 by variable Address Get the variable Homepage of the Shell command implementation.

Echo ${address%%/p*}: Look forward from the end, removing all strings (/p/8569001.html) of the largest length in the address that begin with "/P".

2. Shell Scripting Analysis

Here's a tutorial on how to post a virtual machine under Ubuntu system and how to share folders with Windows system:

Https://jingyan.baidu.com/album/2fb0ba40541a5900f2ec5f07.html?picindex=8

Before the teacher's code to knock, this time through the online tutorial learned how to share folders.

Shared folder directory path:/mnt/bdshare

A

① what is the function of the entire shell script?

Reverse output Input characters

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

cmd= "$cmd \$ $count": Loop Connect echo $count

count= ' Expr $count-1 ': Number of Parameters-1

Eval $cmd: The eval command will first scan the command line for all substitutions before executing the command. After replacing the value of the $ expression, the result is output.

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

2,3.

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

Two.

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

Send a message to the user who corresponds to the first parameter Hello

Three.

Function: Copies the contents of all files in this directory ending in. sh to a new file with the time suffix added.

Four.

The results do not show up.

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

Read the file and then output line after the line has been added.

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

Execute the NL function

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

All the parameters entered by the input string are connected in the order of the former to the back and output.

3.shell Script Writing

(1)

#!/bin/bash

count=$#

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

Do

Eval CP \$ $i $

Done

(2)

#!/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)

#!/bin/bash

For file in *.c;do

MV $file/home/huxingheng/cfile

Done

Ls-s/home/huxingheng/cfile

(4)

#!/bin/bash

Echo-n "Please input a string:"

Read Str

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

(5)

#!/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 a number smaller:" X

Else

Read-p "Please input a number larger:" X

Fi

Done

If [$x-eq $temp];then

echo "Congratulations!"

Fi

Experimental experience: The experiment took a lot of time, many of the previous knowledge has long been forgotten, a lot of common commands are to go through the previous data to understand. There are also individual topics are really I do not understand, to see the classmate's blog to understand. Now also fast to the final exam, overall, for this course of learning is not very good compared to the course of the previous semester, the final review also need to work hard, although the goal of the course itself is to get started, but feel that their current level has not yet reached the requirements of entry.

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.