Shell Program Design (1)

Source: Internet
Author: User
Tags sorts

2.4 pipelines and redirection
2.4.1 redirection
File descriptor redirection description
0 <standard input
1> standard input
2> standard error output
> Append
2> & 1 standard output and error output to the same file
Specifically, you can use/dev/null to effectively discard all output information.
...>/Dev/null 2> & 1

Show command> File
More/less <File

2.4.2 MPs queue: connect processes so that they can run simultaneously.

PS-XO comm | sort | uniq | grep-V sh | more
"|" Is followed by the output
So that data streams can be automatically coordinated between them.
$ Cat mydate. c
Dfdfkj
Afljir
Clkjfoi
While
$ Cat mydate. c | sort
Afljir
Clkjfoi
Dfdfkj
It can be seen that sort not only sorts directories and files, but also sorts rows in a file, but not in rows.
While
$ Cat mydate. c | sort |> mydate. c
The display is blank, because you have overwritten the content of this file before reading mydate. C, so it cannot appear in the pipeline
The output file is immediately created and written to the ship when the command is executed by the ship.

2.5 Shell
There are two methods for shell scripts: enter a series of commands for Interactive Shell execution, as shown above; Save the commands to a file and call the file as a program

Wildcard
* Any string
? Single string
[ABC...] any single character in square brackets
[^ ABC...] match any single character except square brackets
{AB, DCD,...} allows any string to be grouped in a collection.
For example, the value of my {Ma, fa} se is mymase myfase.

2.5.1 interactive program
Directly input commands in shell, but they cannot be saved and are not reusable.

2.5.2 script
1. Create

#: Annotator
#! /Bin/sh: Tell the system that the parameter/bin/sh is used to execute the program in this file.
Exit: Make sure that the program returns a meaningful exit code. When you call this script from another script program and check whether the execution is successful, the exit code is very important.
$ File first: used to check the file type
First: POSIX shell script text executable

2.5.3 run
A./bin/sh Script Name
B. chmod + x Script Name
Script Name
Or, the./script name indicates the complete relative path of the script program and ensures that the system will not accidentally execute another command in your script file.
If the command is not found at this time, it is because the shell path is not set as the command to be searched and executed in the current directory.
Method 1: Path = ¥ path :.
Method 2: add the Script Name To. bash_profile and log out again.

3.6 shell syntax

2.6.1 variable
You do not need to declare variables before using them. It is viewed and stored in strings by default.
Add $ access content before the variable name. However, when assigning a value to a variable, you only need the variable name.

1. Use quotation marks
Ke = ma LD
Ld: no input files
Quotation marks are required for spaces.
$ KE = "Ma lD"
$ Echo $ Ke
Ma LD

$ Read ke waiting for Input
DKF klfdj Kle Input
$ Echo $ Ke
DKF klfdj Kle Original output

#! /Bin/sh
Myvar = "Hi there"
Echo $ myvar // hi there
Echo "$ myvar" // hi there
Echo "myvar" // myvar
Echo '$ myvar' // $ myvar
Echo 'myvar '// myvar
Echo/$ myvar // $ myvar
Echo/myvar // myvar
Echo enter some text // enter some text
Read myvar // wait for Input
// Enter How do you do
Echo '$ myvar' now equals $ myvar // $ myvar now equals how do you do
Exit 0

From the above experiment, we can know:
1. Use "" for variable reference and do not use it.
2. Variables without $ in "" are only strings.
3 ''is used to output the strings.
4/has the same effect ''.

2. Environment Variables
$ Home: Home Directory of the current user
$ Path: A list of directories separated by colons used to search for commands
$ PS1 Command Prompt
$ Ifs field delimiter, space, tab, and enter
$0 shell script name
$ # Number of parameters passed to the script
$ Process ID of the shell script

3 parameter variables

$1, $2,... parameters of the script program
$ * List all parameters in a variable. The parameters are separated by the first character in IFS.
$ @ Is the same as $ *. IFS is not used.

$ Ifs =''
$ Set Foo bar Bam
$ Echo $ @
Foo bar Bam
$ Echo "$ @"
Foo bar Bam
$ Echo $ *
Foo bar Bam
$ Echo "$ *"
Foobarbam

$ Unset IFS
$ Echo $ IFS

$ Echo "$ *"
Foo bar Bam

If you want to access the parameters of the script program, use $ @.

Sa = "hello "//
Echo $ SA // hello
Echo "The program $0 is now running" // name of the biangliang shell script
Echo "the second parameter was $2" // bar
Echo "the first parameter was $1" // foo
Echo "the parameter list was $ *" // Foo bar Baz
Echo "the script is now complete"

$/Bin/sh Bianliang Foo bar Baz

2.6.2 Conditions
The shell script tests the exit code of any command called from the command line.
Test/[Boolean judgment command and "[" end "]"

If [-F/bin/bash] // keep the distance
Then
Echo "file/bin/bash exists"
Fi

If [-D/bin/bash]
Then
Echo "/bin/bash is a directory"
Else
Echo "/bin/bash is not a directory"
Fi

The execution result is:
$/Bin/sh panduan
File/bin/bash exists
/Bin/bash is not a directory

2.6.3 Control Structure

Echo "Please answer yes or no: Is it morning? "// Shell uses echo-E, bash uses echo-N to remove line breaks
Read timeofday

If [$ timeofday = "yes"] // you must keep the distance from if [$ timeofday; if [$ timeofday = "yes"
Then
Echo "good morning"
Elif [$ timeofday = "no"] // [test the content of timeofday. The test result is determined by if
Then
Echo "Good afternoon"
Else
Echo "sorry, $ timeofday is not recognized. Enter yes or no! "
Exit 1
Fi
Exit 0

The seemingly perfect program has many hidden risks. After entering directly without entering any data
[: 15: Yes: Unexpected Operator
[: 15: No: Unexpected Operator
Sorry, is not recognized. Enter yes or no!
The problem is that $ timeofday becomes an empty string after entering
Therefore, change $ timeofday to "$ timeofday"

4. For statement
The for loop is often used together with the shell file name extension.
For variable in values_table
Do
... $ Variable
Done

The for loop is often used together with the shell file name extension.
For file in *. Sh

There is
For foo in 10
Echo "Here wo go"
This can be executed once
For foo in 1 2 3 4 5 6 7 8 9 10
Echo "Here wo go"
But it can be executed 10 times.
While
For foo in 1 2 3 5 8 9 41 10 1 0
Echo "Here wo go"
Still 10 executions

 

Reference: Linux programming (V3)

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.