Shell Script Basics

Source: Internet
Author: User
Tags define local logical operators

Reprinted from Http://www.cnblogs.com/liyuhui-Z/p/7792108.htmlShell Introduction

As a command language, the shell interprets and executes user-entered commands interactively, and as a programming language, it defines variables and parameters and provides many control structures in high-level languages, including loops and branches.

Often used as a batch command to improve productivity!

Entry

Here is a shell of a simple starter program, we use the Linux above vi test.sh and then put the following code in, run sh test.sh can

#!/bin/bash    echo "Hello World !"

Above this program is output HelloWorld, which #!/bin/bash specifies the compiler, must be in the first line of the script, echo output information, here for the output Hello World

How to run this program, the first step into the directory where the script, such as Cd/root/shelltest, run the command sh test.sh so that you can enter.

Variables in the program define global variables

Global variables persist, do not disappear because of switching the shell terminal or log out, global variables need to be set in/root/.bush_profile,/ETC/BASHRC,/etc/profile,/ETC/PROFILE.D files, formatted asexport name=value

After the definition is complete in/etc/profile, the configuration file needs to be reloadedsource /etc/profile

Where the shell script is set in/ETC/PROFILE.D, the script can be executed when the Shelle client is started, and this folder is stored in scripts.

Create the script content as follows

This script is executed when the client is opened, and the corresponding content is output.

Define local variables, and local variables will expire when exiting the shell client

The difference between single quotation marks and double quotes

Single quotes: Output AS-is

Double quotes: If there are variables, the variables are output

No quotation marks: Output variable

Passed parameters

You can pass parameters to the script when the script is called, using the $ $ ... in the script. Accept parameters that are passed and need to be enclosed in curly braces when the argument exceeds 9 ${10}

Special Invocation Parameters:

    $0:返回Shell文件名称    $$:当前脚本的PID    $#:传递的所有参数 [email protected]:程序中的所有参数,这是将参数传递给其他程序的最佳方式。 $! :执行的上一个命令的PID $?:上一次指令的返回值 $_:此命令前面的最后一个参数 $* :获取所有的参数

Accessing variables between different scripts

With a sh command-initiated script, the properties inside the script end will disappear, and other scripts cannot access its internal properties.

When source you start a script with a command, its properties are not invalidated when the script ends, and other scripts can access it.

Cancel a variable

Use unset to cancel a variable, which includes environment variables and local variables

Array

Multiple values can be stored in an array. The Bash Shell supports only one-dimensional arrays (which do not support multidimensional arrays), does not need to define the array size when initializing (similar to PHP), and the array subscript starts at 0.

Defining arrays

    array=(value1 value2 value3)        array_name[0]=value0

Reading an array

${array_name[index]}

Get all the elements in an array

${my_array[@]}

Gets the length of the array

${#my_array[*]}

Process Control if

If no statement is executed in the Else statement, it cannot be written

If conditionthen command1 command2 ... CommandN fi------------------if conditionthen command1 command2 ... commandnElse commandfi------------if Condition1then command1elif condition2 Then  command2else commandnfi           
For
for var in item1 item2 ... itemNdo    command1    command2 ... commandNdone
While
while conditiondo    commanddone
Until

Similar to the Do-while cycle, at the Last Judgment condition, at least once

until conditiondo    commanddone
Case
case 值 in模式1)    command1    command2    ...    commandN ;;模式2) command1 command2 ... commandN ;;esac
Function
demoFun(){   // 定义      echo "这是我的第一个 shell 函数!"}echo "-----函数开始执行-----"demoFun // 调用,如果需要传递参数直接在后面跟就好 demoFun 1 2 3echo "-----函数执行完毕-----"
Operator
    • Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.

    • expr 2 + 2To use the method, you need to be aware of 1. There is a space between the expression and the operator 2. Need to use ' ' cause

Relational operators

How to use:

if [ $a -eq $b ]then echo "$a -eq $b : a 等于 b"else echo "$a -eq $b: a 不等于 b"fi
Boolean operator

How to use

if [ $a -lt 100 -a $b -gt 15 ]then echo "$a 小于 100 且 $b 大于 15 : 返回 true"else echo "$a 小于 100 且 $b 大于 15 : 返回 false"fi
logical operators

String operators

File Test Operators

Shell Script 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.