Shell programming (one) basic syntax __shell

Source: Internet
Author: User
Tags arithmetic function definition

First, basic syntax 1.1 Introductory example 1.2 shell variable 1.2.1 Permanent variable 1.2.2 temporary variable 1.2.3 single and double quotes 1.2.4 variable management 1.2.5 position variable and special variable 1.3 read command 1.4 Expr Command 1.5 variable test statement 1.6 If Judgment statement 1.7 case flow control 1.8 double Bracket usage 1.9 Loop statement nesting 1.10 break and Continue 1.11 shift command second, Shell function 2.1 function definition 2.2 function parameter Pass 2.3 regular expression

first, the basic grammar 1.1 Introductory Examples

Create a new example01.sh and write the following:

Vim example01.sh
#!/bin/bash #This is to show
what a example looks like echo "This is" we our A

example "
echo #This Inserts Empty line in output
Echo ' We are currently in the following directory '
pwd
echo
echo ' This directory cont Ains the following files "
Ls-lh

First line: #!/bin/bash the shell with bash, the comment begins with a # sign.

Add Permission:

chmod +x  example01.sh

Execution Mode one:

./example01.sh

Execution Mode II:

/opt/bee/shtest/example01.sh

Execution mode three:

SH example01.sh
1.2 Shell Variables 1.2.1 Permanent variable

Permanent variable: A permanent variable is an environment variable whose value does not disappear as the execution of the shell script ends.
For example, path is an environment variable, such as path, Java_home.

echo $JAVA _home
Echo $PATH
1.2.2 Temporary variable

Temporary variables: Shell program definition, its scope is limited to the program that defines it, not visible to other programs.

Considerations for Temporary variables:

1. Begin with a letter or underscore.
2. Letters, numbers, underscores.
3. Case sensitive.
4. There is no limit to the length of the variable name.
5. When you need to use a temporary variable, precede the variable name with the prefix "$".
6. The equal sign cannot have spaces on either side of the variable assignment.
1.2.3 Single and double quotes

The difference between single and double quotes:
1. The content in single quotes is assigned to the variable as it is
2. Double quotes Cancel the function of the space and retain the meaning of the special symbol.

Example

Day=mon
a= ' here is $DAY ' b= ' today is
$DAY '
echo $A 
echo $B

Output results:

$DAY Today is
mon
1.2.4 Variable Management

View all variables:

SET

View a variable

Day= "Mon"
set | grep day

To delete a variable:

Unset Day
1.2.5 Position variables and special variables
Ls-lh

LS is the command name, and the-LH is the positional parameter.

./example01.sh file1 File2 File3

0 is the file name of this program 0 is the program's filename 0 is the program's filename n is the program's nth parameter value

A special variable is a setting that will not be modified when the script is first executed.

∗ All parameters of this program ∗ all parameters of this program * All parameters of this program # The number of parameters in this program
$$ the PID of this process
$! To perform the PID of a previous background program
$? Executes the return value of the previous instruction

#!/bin/bash

echo "$*" indicates that all parameters of the program "
echo" $# represent the number of parameters of the program "


Touch/tmp/a.txt

echo" $$ representing the process ID of

the program " Touch/tmp/b.txt &
echo "$! The PID "echo" $$ that executes the previous background directive
represents the process ID of the program

Output:

[root@iz2zeisjfk2rw89yhp3g19z shtest]# sh example02.sh  a b c
a B c indicates that all parameter 3 of this program
indicates the number of parameters for this program
2755 Represents the process ID of the program
2757 the PID 2755 that executes the previous background directive
indicates the process ID of the program
1.3 Read Command

Function: Read command to be readable for parameters
Syntax: read a b C

#!/bin/bash

echo "input three parameters:"
read the second third
echo "the" the "the" the "the" the "the" the "the" the " 7/>echo "The second parameter is $second"
echo "the third parameter is $third"
1.4 Expr Command

Function: The arithmetic operation of the shell variable
Expr command: Arithmetic operations on integer variables
Syntax: expr expression, for example

Expr 3 + 5  #算术运算符中要有空格
var1=10
var2=6
expr $var 1-3
expr $var 1 + $var 2
expr $var 1/2
expr $v AR1 \* $var 2 expr

' expr 5 + ' \* ' expr 2 + 3 ' expr
' expr 5 + '/' Expr 2 + 3 '

example04.sh

#!/bin/sh

a=10
b=20
c=30


value1= ' expr $A + $B + $C '
echo ' value1: $value 1 "

value2= ' expr $ C/$B '

echo ' value2: $value 2 "

value3= ' expr $A \*  $B '
echo" Value3: $value 3 "
1.5 variable test statement

Test integer:

Test int1-eq int2
test int1-ge int2
test int1-gt int2
test int1-le int2
test int1-lt int2
te St Int1-ne Int2

File test:

test-d file #测试是否为目录
test-f file #测试是否为文件
test-r file #测试文件是否可读
test-w file #测试文件是否可写
test-x file #测试文件是否可执行
test-e file #测试文件是否存在
test-s file #测试大小是否为空
1.6 If Judgment statement

Process Control Statements

If test[]
then
   statement
Fi

# indicates that two commands are written on one line and do not affect each other

Example:

#!/bin/bash
echo "input a file name:"
read file_name

if [-D $file _name]
then
    echo $file _name is a Dir "
elif [f $file _name]; then
    echo" $file _name is a file "
elif [-C $file _name-o-b $file _name]; then< C12/>echo "$file _name is D device file"
else
    echo "$file _name are an unknow file"
fi
1.7 Case Process Control
#!/bin/bash
echo "*************************"
echo "Please select your operation:"
echo "1 Copy"
echo "2 Delete"
echo "3 Backup"
echo "Q Quit"
read op case
$op in 
    1)
    echo "Your selection is Copy "
    ;;
    2)
    echo "Your selection is Delete"
    ;;
    3)
    echo "Your selection is Backup"
    ;;
    Q)
    echo "Quit"
    ;;
    *)
    echo "Invalid selection"
Esac
1.8 Use of double parentheses
#!/bin/sh

var1=1 while

(var1<100) does
    echo "$var 1"
    ((var1=var1*2))
done
1.9 Loop Statement nesting
#!/bin/sh
echo "Please input line number and shape:"
read N S

i=0
n= $N for

(I=1; i<=n; i++) does
   j=1
   for ((j=1; j<= $i; j +))
      do echo-n "$S" #-n acts as a newline.
   Done
  

Print Inverted triangle

Please input line number and shape: 
5 &
&
&&
&&&
&& &&
&&&&&
1.10 Break and Continue
#!/bin/bash

# Break Continue
# break out of the Loop
# continue jump out of the loop and do the next cycle

############################### ##############
# #
Print a menu function, after the error, you can re-enter the
# only input q, you can exit the menu
#
################################# ############ while

true does
echo "
***************************************"
echo "please select Your operation: "
echo" 1 Copy "
echo" 2 Delete "
echo" 3 Backup "
echo" 4 Quit "
echo" ************ "
Read op case

$op in
    C)
    echo" Your selecttion is Copy "
    ;;
    D)
    echo "Your selecttion is Delete"
    ;;
    B)
    echo "Your selecttion is Backup"
    ;;
    Q)
    echo "Your selecttion is Quit"
    break
    ;;
    *)
    echo "Invalide selection,please try Again"
    continue
    ;;
Esac done
1.11 Shift Command
#!/bin/bash echo "####################################" echo "#"
echo "
#做一个加法计算器, find all parameters of the and #"
echo "# #                  "
echo "####################################"

echo "please input your nums:"
if [$#-le 0 ]
   then
   echo "err!:not enough Parameters"
   exit 124
fi

sum=0 while

[$#-gt 0]
do
  sum= ' expr $sum + '
  shift done

echo ' Sum: $sum '
Second, the Shell function 2.1 Function Definitions

Grammar:

Function name ()
{
  shell command sequence
}

Or

function function name ()
{
  shell command sequence
}

Note: The function call does not require a band ()
The variables in the function are global variables, and there are no local variables.
You can pass arguments when calling a function, using 1</in a function

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.