Linux--shell Script Basics (variable types, variable operations, definitions, operations and logical relationships)

Source: Internet
Author: User

Shell Script Basics

1.变量什么是变量#a=1# echo $a1变量:可以变化的量

1.2 Variable name considerations

变量名不能以数字开头不能与系统中已有的环境变量重名,尽量不要全部使用大写,尽量不要使用“_”下划线开头最好做到见名知意不能使用程序中的保留字,列如if,for等

1.3 Variable types

    • Character type
    • Numeric type
      Integral type
      Floating point Type
    • Boolean condition Yes and no
#$echo $a     引用abc撤销 unset +(变量名)a

1.4 Variable operation

* 设置变量* 引用变量* 撤销变量

The difference between single and double quotation marks

#echo $a1#echo ‘$a‘‘$a‘#echo "$a"1 #echo {$a}b1b                                 

Defined

转义符  \ 还原#!/bin/bash   程序执行脚本

1.5bash Variable Type

    * 环境变量    * 本地变量   (局部变量)    * 位置变量    * 特殊变量(bash内置的,用来保存某些特殊数据的变量,也称系统变量)

1.5.1 Local Variables

VAR_NAME=VALUE   //本地变量,作用域为当前shell进程,对当前shell外的其他shell进程,v包括当前shell,子shell进程均无效local VAR_NAME=VALUE  //局部变量,作用域为当前代码段,常用于函数

1.5.2 Environment variables

export VAR_NAME=VALUE    //作用域为当前shell进程及其子进程`

1.5.3 position Variable

$1,$2,$3, ....         //用来引用脚本的参数   shift [num]         //位置变量使用完以后退出,后面的参数向前推进    shift 1    一次往前推,例:$1完成任务后变成$2执行    

1.5.4 Special variables

$#   //是传给脚本的参数个数$0   //是脚本本身的名字$!   //是shell最后运行的后台Process的PID[email protected]   //是传给脚本的所有参数的列表$*   //是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个$$   //是脚本运行的当前进程ID号$?   //是显示上条命令的退出状态,0表示没有错误,其他表示有错误

1.5.5 Bash built-in environment variables

PATHSHELLUIDHISTSIZEHOMEPWDHISTFILEPS1

1.5.6 read-only variable (constant)

readonly  VAR_NAME=VALUE    //不能修改值,不能销毁,只能等shell进程终止时随之消亡

2. Script Basics
2.1 What is a script?

按实际需要,结合命令流程控制机制实现的源程序。说白点就是命令的堆砌。`

2.2 Program return value
Program execution has two types of return values in the future:

* 程序执行的结果* 程序状态返回代码(0-255)       0:正确执行       1-255:错误执行,1,2,127系统预留,有特殊意义

2.3 Script Test
How does bash test a script for errors? How to troubleshoot after an error?

bash -n scriptname  //检`查脚本是否有语法错误bash -x scriptname  //单步执行,检查脚本错在哪

2.4 Writing script considerations

* 禁止将未成功执行过的代码直接写入脚本* 脚本中的命令一定要用绝对路径

2.5 Shell arithmetic operations

A=3B=6let C=$A+$B     //let 算术运算表达式C=$[$A+$B]      //$[算术运算表达式]C=(($A+$B))     //$((算术运算表达式))C=`expr $A + $B`   //expr  算术运算表达式,表达式中各操作数及运算符之间要有空隔,而且要使用命令引用

2.6 Logical relationships between commands

逻辑与:&&    第一个条件为假时,第二个条件不用再判断,最终结果已经有    第一个条件为真时,第二个条件必须得判断逻辑或:||    前一个命令的结果为真时,第二个命令就不执行    前一个命令的结果为假时,第二个命令必须执行

Linux--shell Script Basics (variable types, variable operations, definitions, operations and logical relationships)

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.