Linux-shell Programming Basics

Source: Internet
Author: User

1th Shell Basics 1.1 What is a shell

The shell is a command interpreter, which at the outermost of the operating system, is responsible for directly talking to the user, explaining the user's input to the operating system, and processing the output of a wide variety of operating systems, returning the output screen to the user.

1.1.1 Shell Dialog mode

Interactive way: From the keyboard input command, through the resolution of the/bin/bash, you can immediately get the shell response, a question and answer the way

Non-interactive: Shell script

1.2 Why use Shell programming

As a qualified OPS person must master one or more scripting languages, shell programming is an important tool to implement LINUX/UNIX system management and automation operations, can write tedious and repetitive commands to shell script execution, save time, effectively improve the efficiency of operations personnel , reducing unnecessary duplication of work time.

1.3 Shell in the system

Linux system default shell is bash

[[Email protected] ~] # cat/etc/shells/bin/sh/bin/bash/sbin/nologin/bin/Dash/bin/tcsh /bin/csh  
The 2nd Chapter Variable 2.1 What is a variable

X+y=20 x=10 y=? Unknown (variable)

An unknown amount of time that can be changed is called a variable

2.2 Classification of Variables 2.2.1 Common variables (local variables)

Used only in the current environment, you can temporarily change the normal variable to an environment variable using the Export command

[[Email protected] ~] # ip=10.0.0.201     Assigning values to variables (storing data in variables)   ~]#  echo $ip10.0.0.201
2.2.2 Environment variables (global variables)

1. Uppercase

2. System-defined

3. Can be used in most places

2.2.3 Special variables

The $ $ after number is a few of the shell scripts that represent the first few parameters

$# the number of parameters in the shell script

Determine the number of parameters for a script

$? Execution result of the previous command (return value)

0 performing the correct

Non-0 execution failure

$ A script file name

3rd. Writing specification for shell Scripts 3.1.1 Script storage location
To prevent the script from being put in place, it is stored uniformly in/server/scripts/

Create a Directory

[[Email protected] ~] # mkdir-p/server/scripts/  ~]#  cd/server/scripts/
3.1.2 Script Editor using vim3.1.3 selection interpreter

When executing a bash script, the kernel will be based on the "#!" After the interpreter to determine which program to use to interpret the contents of the script.

[[Email protected] ~] # vim/server/scripts/cal.sh     # !/bin/bash
3.1.4 File name Writing

The file name must be meaningful, convenient to see what the script is for, and end with. Sh

3.1.5 specifications and habits of writing scripts

1, placed in the unified directory, easy to find

2, writing scripts to use vim editing,

Bulk Delete

Bulk increase

1. Go to bulk Edit mode (visual block) ESC Ctrl + V

2. Select the range of bulk additions

3. Press SHIFT + I to modify

4. Press ESC to exit, and so on

3. The script file name ends with. sh

4. Be sure to specify the interpreter at the beginning of the script

5, the script in the # for comments, #后的内容不会执行, do not use the command, etc. can be commented out, the script try not to appear in Chinese comments in order to avoid the occurrence of different character sets garbled

6. Code Symbol Writing specification

1. The pairs of symbols must be written to add content, to avoid forgetting the omission of writing;

2. Process Control statement once written, then add content; (if condition; then content; fi)

3. Written code symbols are in English input method format

4. Make code easy to read by indenting

4th Chapter Shell Practice

Write a calculator script

4.1 Numerical calculation method
[[Email protected] ~] # awk  ' begin{print' 0.5
The variables in the 4.1.1 command line are placed in awk using the

Awk-v variable = assignment variable name defined by itself

[[Email protected] ~] # awk-vnum1=10  -vnum2=20 ' begin{print num1/num2} '    Here are the variables num1 and num20.5
4.1.2 Writing script calculates the subtraction of 10 and 20
[[email protected]]#Vim cal.sh#!/bin/bashN1=10N2=20awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1+num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1-num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1*num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1/num2}'

Execute script

[[email protected]/server/scripts] # sh cal.sh30-102000.5
4.1.3 defining special variables to be evaluated by means of command-line pass parameters
[[email protected]/server/scripts]#Vim cal.sh#!/bin/bashN1=$1The  first parameter of n2=$2The  second parameter , awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1+num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1-num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1*num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1/num2}'

Execute script

[[email protected]/server/scripts] # SH cal.sh      The following two arguments are required to execute successfully, preceded by a space  30-102000.5
4.2 Through the Read interactive

Read interactive assigning variable content

READ-P "hint message:" Variable name

Read-p "prompt message:" P

After execution, the input information is stored in the variable.

[[email protected]/server/scripts] # read-p "input num1:" N1  /server/scripts]#  echo $n 1Zeq
4.2.1 N1 n2 Assignment via the Read command
[[email protected]/server/scripts]#Vim cal.read.sh#!/bin/bashRead-P"input num1,num2:"N1 n2 a command to directly define two variables awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1+num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1-num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1*num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1/num2}'

Execute script

[[email protected]/server/scripts] # SH cal.read.sh Input num1,num2:          Two randomly enter a number, the middle of a space 30-102000.5

4.3 Article expression if4.3.1 determine if a directory, file exists

[-d/data]

[-f/data]

Note the format: [Space-f/data space] [] The two ends must have a space, the middle write content

[[email protected]/server/scripts] # [-d/data]  /server/scripts]#  echo $?                 $? Special variable, 0 for correct execution, not 0 execution failure   /server/scripts]#  [-d/data123]/server/scripts]#  echo $? 1
4.3.2 ratio Size
-eq         equal   ==-ne  equal         !=-gt     great than        >-ge     great equal       >=- Lt     less  than        <-le     less  equal       <=

4.3.3 Example of size ratio
[[email protected]/server/scripts]#[10-eq][[Email protected]/server/scripts]#echo $? 0[[email protected]/server/scripts]#[10-GT][[Email protected]/server/scripts]#echo $?1[[Email protected]/server/scripts]#[10-ge][[Email protected]/server/scripts]#echo $?0
4.3.4 If single branch format script

Format: if [conditional];then

Command

Fi

1, judge the number of parameters of the script, if not 2 prompt please enter two numbers

[[email protected]/server/scripts] # Cat pan.sh # !/bin/bash if [$#  -ne 2];then    "input two Numbers" fi    

2. Add a condition to the calculator (parameter pass)

Determine the number of arguments to the script,

If it's not 2,

Hint Please enter two numbers

[[email protected]/server/scripts]#Cat cal.sh#!/bin/bashN1=$1N2=$2if[ $#-ne 2];thenEcho"USAGE: $ NUM1 num2"$ $  file name exit  Exit exit meaning fi awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1+num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1-num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1*num2}'awk-vnum1= $n 1-vnum2= $n 2'Begin{print num1/num2}'

3. Execute script

[[email protected]/server/scripts] # sh  cal.sh/server/scripts] # sh  cal.sh14010024006
4.3.5 if dual-branch format script

Format: if [conditional];then

Command

Else

Command

Fi

1, comp.sh input two numbers:

1. If N1 is greater than N2

Output N1 > N2

2. If N1 is less than N2

Output N1 <= N2

[[email protected]/server/scripts] # cat  comp.sh#!/bin/bash  N1=$1n2if [$n 1-GT $n 2];then    " $n 1 > $n 2 " Else  "$n 1 <= $n 2" fi    

2. Execute script

[[email protected]/server/scripts] # sh comp.shten <=/server/scripts]#  sh comp.shten <= Ver/scripts]#  sh comp.sh> 10

Linux-shell Programming Basics

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.