Linux Experiment 4_shell Programming (2)

Source: Internet
Author: User
Tags echo command echo display

First, the contents of the experiment

Four blocks: 1, Shell variable basic usage and general symbol use. 2, Shell script analysis. 3. Shell script writing. 4, modify the configuration file to customize the personalized operating system or write the implementation of the task of automated processing.

Second, the experimental steps

1. Shell variable basic usage and general symbol usage

(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

About multiple spaces, the first with the echo $x results always show only a space, Internet access, it should be written as echo "$x", which is related to shell parsing rules.

The shell parsing process:

    1. Substitution variables
    2. Execute command
    3. The parameters after the command are divided by ifs, with "" and "surrounded by the part as a parameter of the command.

The Echo $tmp is executed as follows:

    1. Replacement variable: echo a b D
    2. Execute command: Echo a b D
    3. Parameters A, B, d after the echo command is divided by ifs

So the print out is a b d, and a number of spaces show only one.

About this ifs, it's a set-type environment variable, and here are some notable things to note.
1. The default value of IFS is: blank (including: Space, tab, and New line), and its assii code is printed in hexadecimal: 0a (see shell script below).
2. IFS's whitespace processing is not the same as other characters, the left and right sides of the pure white space will be ignored, a number of consecutive white space is treated as an IFS.

(3) In the shell command Terminal input likes= (Cosmos Galaxy Moon); Likes[6]=mars, then use echo to display the expression

①${likes[*]}

Displays all non-empty elements of the likes array, returning as a string

②${likes[@]}

Displays all non-empty elements of the likes array, with each array element as a separate string

③${# likes[*]}④${# likes[@]}⑤${# likes}

Displays the number of elements already set in the array likes

⑥${# Likes[0]}

Displays the length of the first element of the likes array, likes[0] cosmos, length 6

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

① ${name:-hello}

Function: If name is not NULL, the reference is the contents of the variable name, and if it is null then output-the contents of the following string

(5) in the Shellshell Shell command terminal input name= '/usr/share/doc/apg/php.tar.gz ', and then use echo to display the expression ① and ② values to observe the results respectively. Observation results.
Modify the value of name, let Name= '/etc/apt/sources.list.d ', use echo to display the values of the expression ① and ② respectively, and observe the effect of writing the expression.

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

Command ①②. *, the pattern can be matched. Any subsequent characters, a% means that the minimum number of matches is removed, so a suffix is shown, respectively. Tar,.list. Two% means that the long match is removed and the long match removes all suffixes, so only the file name is displayed.

(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.3 section)

The pattern in ${name#*/} is */, which matches any character plus/. #表示去掉最少匹配部分, # #表示去掉做多部分, so the first name value is only removed/, when you use # #时, you can remove all the characters that were included/before X11. So is the 2nd name.

(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. Script Analysis in Shell

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

#!/bin/bash

count=$#
Cmd=echo
While [$count-GT 0]
Do
cmd= "$cmd \$ $count"
count= ' Expr $count-1 '
Done
Eval $cmd

Run the script in the following way:

① 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

① what is the function of the entire shell script?

The function of the entire script is to output the parameters in reverse.

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

The 8th line refers to the number of input parameters to cmd, the 9th line refers to each cycle, the number of $count decreased by 1, 11th line refers to the run cmd command

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

Run count equals parameter

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

Value of 3

(2) Write the shell script file ex2.sh using Vim/gedit, which reads as follows: (Textbook P145 4.10)

#!/bin/bash

Is_user_found=0
Date +%f
If who |grep "^$1";
is_user_found=1;
Write $ <<limitstring
Hello
Limitstring
Fi

If [$IS _user_found-eq 0]; Then
echo "User $ not found."
Fi

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?

Who represents the user who is viewing the currently logged on system, grep "^$1" means retrieving the value of the first parameter of the parameter list, Kyo in the command, so the result is displayed as Kyo

②line8~10 function?

Line8~10 is to redirect the first parameter to limitstring and send Hello Kyo to it.

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

#!/bin/bash

suffix=backup--' Date +%y%m%d-%h%m '
for script in *.sh; Do
Newname= "$script. $suffix"
echo "Copying $script to $newname ..."
CP $script $newname
Done

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?

Back up all the. sh files. The new backup file name is the original file name + Date + Time

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

#!/bin/bash
function NL () {
While read X
Do
((++line))
echo "$line $x"
Done
}
Line=0
If [$#-eq 0];then
NL
Else
While ["$"] && [-F "]
Do
Nl<$1
Shift
Done
Fi

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 the file given in the parameter by row, preceded by a line number in front of each line.

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

When the condition test is met, the script executes the function NL function.

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

If the incoming file parameter name is not false and the file is a regular file, the NL method is called to read the file. After reading, the parameter position is shifted to the left, while the judgment in while, if true, then use NL to continue reading the file represented by the second 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) 4th Chapter Study Questions 4.9

#!/bin/bash

Dir=$1
Shift
While [$#-ne 0] && [-F "$#"]
Do
CP $ $dir
Shift
Done

(2) 4th Chapter Study Questions 4.11


Dir=$1
Shift
While ["$"] && [-F "]
Do
File=$1
Cat-n $
Shift
Done

(3) 4th Chapter Study Questions 4.12

#!/bin/bash
Dir=$1
For file in *.c
Do
MV $file $dir
Done
LS $dir
~

(4) 4th Chapter Study Questions 4.18

#!/bin/bash
Read X
echo $x | Cut-c $1-$2 $x

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

In any case is an error, this character range is wrong???

(5) Guess the number game

#!/bin/bash
randomnumber=$ ((random%100))
echo "Write any number in 1-100"
Read X
While [$x-ne $randomNumber]
Do
If [$x-lt $randomNumber]
Then
echo "Opps,try It Bolder"
Else
echo "Opps,try it Less"
Fi
echo "Try Again"
Read X
Done
echo "gotcha!"
~

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.

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

Summary:

This experiment learned a lot of things, many things or do it yourself, four o ' 7:30, lay.

Linux Experiment 4_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.