Linux shell Programming

Source: Internet
Author: User
Tags case statement logical operators rar

1. What is a shell

2. or Hello World program

3. Variables in the shell

3.1 System Variables

3.2 User-defined variables

3.2.1 User-defined variable rules

3.2.3 How the shell uses variables

3.2.3 Global variables vs Local variables

4. Control structure in shell programming

4.1 Determination of pieces of article

4.1.1 Simple Condition Determination

4.1.2 Combination Judgment

4.2 If-else

4.3 for

4.4 While

4.5 Case

5. Functions in the shell

5.1 Function declarations and definitions

5.2 Function calls

6. Shell Script debugging

echo of the 61,000 can

6.2 of TWO commands

7. Resources and Shell script download

<1> What is a shell

The shell plays the role of the operating system kernel and the user's middleman by typing the shell command, then the shell passes the user input and then forwards the request to the operating system's kernel for processing.

1. A system can have more than one shell, you can use the Cat/etc/shells command to view the shell installed in the system, different shells may support the command syntax is not the same. 2. You can view the currently used shell through the echo $SHELL <2>. or Hello World program

First use the Vim editor (or any text editor under Linux) to write the file helloshell.sh (no need to use the. sh suffix name):

#!/bin/bash

echo "Hello Shell";

Save the above file and increase the file execution permissions:

[Email protected]:~/shell$ sudo chmod +x./helloshell.sh

Run the shell program:

[Email protected]:~/shell$./helloshell.sh

Hello Shell

There is no practical meaning through the program above, but the first shell program understands the execution of the shell program.

<3>. Variables in the shell3.1 System Variables

The variables in the shell script under Linnux are divided into "system variables" and "user-defined variables", which can be viewed through the SET command.

[Email protected]:~/shell$ Set

... Omit content

[Email protected]:~/shell$ echo $HOME/home/xuqiang3.2 user-defined variables

The user can customize the variables in the shell, the variables in the shell are no data types, and the shell will automatically convert according to the current environment, for example:

msg= "Hello World"

The above statement defines the variable MSG and sets the initial value for Hello world.

Tip 1. It is important to note that when you define a variable, = There are no spaces on either side

3.2.1 User-defined variable rules

The variable must start with a letter followed by a letter or an underscore, the name of the variable is case-sensitive, and you can define a variable whose value is null.

[Email protected]:~/shell$ vech=[email protected]:~/shell$ Echo $vec

How to use variables in 3.2.2 Shell

If you want to get the value stored in the shell variable, you need to add the $ symbol before the variable name, for example:

[Email protected]:~/shell$ vech= "value" [Email protected]:~/shell$ echo $vech # This would print the value of Vechvalue[emai L protected]:~/shell$ Echo Vech # This would print the string "Vech" Vech

3.2.3 Global variables vs Local variables

The default variables written in the shell are all local variables, and if you reopen the console, the variables will all be lost, and the global variables can be written in the file ~/.BASHRC file.

<4>. Control Structure4.1 Determination of pieces of article

In the shell, the condition is judged by the test command or by the [] implementation, judging by the following conditions:

Mathematical operations:

A-eq B:a = = b
A-ne b:a! = b
A-lt B:a < b
A-le b:a <= b
A-GT b:a > B
A-ge b:a >= b

string comparison:
string1 = string2
String1! = string2
STRING1:STRING1 is not NULL, or is not defined

4.2 Combination Decision

Logical operators:

! : not

Exp1-a exp2:a && b

Exp1-o EXP2:EXP1 | | Exp2

4.3 If-else structure:!/bin/sh#
# See whether arguments is positive
#
If [$#-ne 1]
Then
echo "$0:you must give/supply one integers"
Exit 1
Fi

if test $1-GT 0
Then
echo "Number is Postivie"
Else
echo "Number is negative"
Fi

#!/bin/sh

Osch=0

echo "1. UNIX (Sun OS) "
echo "2. Linux (Red Hat) "
Echo-n "Select your OS choice [1 or 2]?"
Read Osch

If [$osch-eq 1]
Then
echo "You pick up Unix"
Else
#
# Nested IF
If [$osch-eq 2]
Then
echo "You pick up Linux"
Else
echo "What's donot like Unix/linux"
Fi
Fi

#!/bin/sh
#
# Test the IF. Elif. Else
#
If [$1-GT 0]; Then
echo "is positive"
Elif [$1-lt 0]; Then
echo "is negative"
Elif [$1-eq 0]; Then
echo "is zero"
Else
echo "is not a number"
Fi4.4 for!/bin/sh#
# test for
#

For I in 1 2 3 4 5
Do
echo "Welcome $i times"
Done#!/bin/bash

for ((i = 0; I <= 5; i++))
Do
echo "Welcome $i times"

Done

Tip 1. Note The type of shell script used in the program

#!/bin/bash

for ((i = 1; I <= 5; i++))
Do
for (j = 1; J <= 5; ++j))
Do
Echo-n "$i"
Done

# Print a new line
echo ""
Done4.5 while#!/bin/bash

#
# test While Loop
#

N=$1
I=0
While [$i-le 10]
Do
echo "$n * $i = ' expr $i \* $n '"
i= ' expr $i + 1 '

Done

4.6 Case#!/bin/bash

#
# Script to test Case statement
#
action= "Update"
Case $action in
"Update")
echo "Update the DB"
;;
"Select")
echo "SELECT from DB"
;;
"Delete")
echo "Delete from DB"
;;
*)
echo "No Action"
;;

Esac

<5>. function5.1 Function declarations and definitions

The following program defines the function demo, all parameters passed to the function are represented as $*, the first parameter is $ A, the second argument is $, and so on.

#!/bin/bash

Function Demo ()
{
echo "All function args: $*"
echo "The first arg: $ $"
echo "The second arg: $"
echo "The third ARG: $ $"
}

# Call the function

Demo-f Foo Bar

5.2 Function calls (function arguments)

In the shell, passing arguments to functions is done by adding parameters directly after the function call, and in the function, the passed parameters are obtained through $.

<6> script Debuggingecho of the 61,000 can

Shell script debugging is disgusting, here is just to provide some general debugging methods, the simplest is to use the Echo function to print out the value of the variable to achieve debugging purposes.

6.2 of TWO commands

Shell script execution can be performed in the form of./shell-filename.sh, while another form is performed in the form of bash./shell-filename.sh, while executing the script can use the-V or the-x parameter to print the program execution information.

-V: Default print Shell reads content

-X: Print the command after "expand"

For example, the following program:

!/bin/sh# for debug shell script
#
tot= ' expr + $ '
Echo $tot

If you use the-v option, the result is as follows:

[Email protected]:~/shell$ sh-v./debug.sh 2 5

#!/bin/sh## for debug Shell script#tot= ' expr $ + $ ' echo $tot 7 If you use the-X option, the result:

[Email protected]:~/shell$ sh-x./debug.sh 2 5

+ Expr 2 + 5+ tot=7+ Echo 77 <7>. resources and Shell code download

Reference: http://www.freeos.com/guides/lsst/index.html

Shell script Download:/files/xuqiang/shell.rar

Ppt:/files/xuqiang/shell Programming for operating system assistants. rar

See http://www.cnblogs.com/xuqiang/archive/2011/04/27/2031034.html

Linux shell Programming

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.