BourneShell and shell programming (2)

Source: Internet
Author: User
Article title: BourneShell and shell programming (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Copyright:
This article is a handout of the LINUX elective course of Dalian University of Technology. You are welcome to repost it, but this document is prohibited from any commercial or profit-making activities. Please retain this copyright statement when reprinting.
Author: he binwu, hbwork@dlut.edu.cn, Dalian University of Technology Network Center
Source code :--------------------------------------------------------------------------------
Bourne Shell and Shell programming (2)
H. interactive reading data from the keyboard
Use the read statement in the following format:
Read var1 var2... varn
Read will not replace variables, but will delete unnecessary spaces until the first line break (press enter) is encountered, and the input values are assigned to the corresponding variables in turn.
Example:
$ Read var1 var2 var3
Hello my friends
$ Echo $ var1 $ var2 $ var3
Hello my friends
$ Echo $ var1
Hello
$ Read var1 var2 var3
Hello my dear friends
$ Echo $ var3
Dear friends <-when redundant variables are input, the remaining content of the input value is assigned to the last variable.
$ Read var1 var2 var3
Hello friends
$ Echo $ var3
<-Var3 is empty
$
In shell script, you can use the read statement for interactive operations:
...
# Echo-n no line feed after the output result of message
Echo-n "Do you want to continue: Y or N"
Read ANSWER
If [$ ANSWER = N-o $ ANSWER = n]
Then
Exit
Fi
I. case structure: the structure is clearer than the elif-then structure.
Compare if-then statements:
If [variable1 = value1]
Then
Command
Command
Elif [variable1 = value2]
Then
Command
Command
Elif [variable1 = value3]
Then
Command
Command
Fi
The corresponding case structure:
Case value in
Pattern1)
Command
Command ;;
Pattern2)
Command
Command ;;
...
Patternn)
Command;
Esac
* The case statement only executes the first matching mode.
For example, use the case statement to create a menu and select shell script
# Display a menu
Echo _
Echo "1 Restore"
Echo "2 Backup"
Echo "3 Unload"
Echo
# Read and excute the user's selection
Echo-n "Enter Choice :"
Read CHOICE
Case "$ CHOICE" in
1) echo "Restore ";;
2) echo "Backup ";;
3) echo "Unload ";;
*) Echo "Sorry $ CHOICE is not a valid choice
Exit 1
Esac
In the preceding example, * indicates the default matching action. You can also use logical operations in case mode as follows:
Pattern1 | pattern2) command
Command ;;
In this way, users can enter numbers or uppercase letters in the preceding example.
Case "$ CHOICE" in
1 | R) echo "Restore ";;
2 | B) echo "Backup ";;
3 | U) echo "Unload ";;
*) Echo "Sorry $ CHOICE is not a valid choice
Exit 1
Esac
(5) cyclic control
<1> while loop:
Format:
While command
Do
Command
Command
Command
...
Done
Example: calculate the square of 1 to 5
#! /Bin/sh
#
# Filename: square. sh
Int = 1
While [$ int-le 5]
Do
Sq = 'expr $ int \ * $ Int'
Echo $ sq
Int = 'expr $ int + 1'
Done
Echo "Job completed"
  
$ Sh square. sh
1
4
9
16
25
Job completed
<2> until cycle structure:
Format:
Until command
Do
Command
Command
....
Command
Done
Example: calculate the square of 1-5 using the until structure
#! /Bin/sh
Int = 1
Until [$ int-gt 5]
Do
Sq = 'expr $ int \ * $ Int'
Echo $ sq
Int = 'expr $ int + 1'
Done
Echo "Job completed"
<3> use shift to process parameters with an indefinite length
In the above example, we always assume that the command line parameter is unique or has a fixed number, or use $ * to pass the entire command line parameter to shell script for processing. The number of parameters is not fixed and you want
The shift command is required for separate processing. Shift can be used to move the location parameters of the command line in sequence,
That is, $2-> $1, $3-> $2. the first position parameter $1 before the shift will not exist after the shift.
Example:
#! /Bin/sh
#
# Filename: shifter
Until [$ #-eq 0]
Do
Echo "Argument is $1 and 'expr $#-1 'argument (s) remain"
Shift
Done
$ Shifter 1 2 3 4
Argument is 1 and 3 argument (s) remain
Argument is 2 and 2 argument (s) remain
Argument is 3 and 1 argument (s) remain
Argument is 4 and 0 argument (s) remain
$
When shift is used, every shift is performed, $ # minus 1. with this feature, you can use the until loop
In the following example, a shell script is used to calculate the sum of integers:
#! /Bin/sh
# Sumints-a program to sum a series of integers
#
If [$ #-eq 0]
Then
Echo "Usage: sumints integer list"
Exit 1
Fi
Sum = 0
Until [$ #-eq 0]
Do
Sum = 'expr $ sum + $1'
Shift
Done
Echo $ sum
$ Sh sumints 324 34 34 12 34
438
$
Another reason for shift is that the location parameter variable of the Bourne Shell is $1 ~ $9, so the location variable
You can only access the first nine parameters. However, this does not mean that a maximum of nine parameters can be entered on the command line. If you want to access the parameters after the first nine parameters, you must use shift.
In addition, you can add an integer after shift to perform multiple shifts at a time, for example:
Shift 3
<4>. for loop
Format:
For var in arg1 arg2... argn
Do
Command
....
Command
Done
Example:
$ For letter in a B c d e; do echo $ letter; done
A
B
C
D
E
Operations on all files in the current directory:
$ For I in *
Do
If [-f $ I]
Then
Echo "$ I is a file"
Elif [-d $ I]
Echo "$ I is a directory"
Fi
Done
Calculate the sum of all integers on the command line:
#! /Bin/sh
Sum = 0
For INT in $ *
Do
Sum = 'expr $ sum + $ Int'
Done
Echo $ sum
<6> exit from the Loop: break and continue commands
Break immediately exits the loop
Continue ignores other commands in this loop and continues the next loop
In shell
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.