Shell Script Basics----Variables

Source: Internet
Author: User

  • Definition of 1.1shell

    1. Stacking of Commands
    2. A file consisting of specific syntax and specific commands

    Execute script command

    bash   test.sh   不需要添加执行权限,使用解释器直接解释sh    tesh.sh./tesh.sh     相对路径,需要添加执行权限
  • 1.2shell Script Base---Variables

    Variable: A fixed string representing an invariant content
    Variable name considerations

    1.不能以数字开头(包含字母、数字、下划线)2.不能使用程序中的保留字(例如:if   for)3.不能跟系统中已有的环境变量重名(尽量不要全部使用大写,尽量不要用‘_’下划线开头)4.见名知义

    Variable type

        "字符型"     例如:a=abc    "数字型"       整型      浮点型,例如:a=1.1    "布尔型“(是否)   true   false

    Variable operation

    设置变量 引用变量

    $ variable Name
    ${variable Name}

    **bash特性**      "反斜杠    \   使反斜杠后面的一个变量变为字符串"     "单引号      ’   转义其中所有的变量为单纯的字符串"     "双引号    ""    保留其中的变量属性,不进行转义处理"     "反引号    ``     把其中放入命令执行后返回结果"撤销变量(unset  a  作用范围:仅在当前shell中有效)

    Bash Variable type

    Environment variables

    export     作用域为当前shell进程及其子进程

    Local variables (local variables)

    "Var_name=valuue"  本地变量,作用域为当前shell进程。对当前shell外的其他shell进程,包括当前shell的父shell,子shell进程均无效"Local  var_name=value"   局部变量,作用域为当前代码段,常用于函数

    Positional variables

    $1,$2    用于引用脚本的参数shirt      依此向前推进

    Special variables (bash built-in variables to hold certain special data, also known as system variables)

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

    Bash built-in environment variables

    "[[email protected] ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[[email protected] ~]# echo $SHELL/bin/bash[[email protected] ~]# echo $UID0[[email protected] ~]# echo $HISTSIZE1000[[email protected] ~]# echo $HOME/root[[email protected] ~]# echo $PWD/root[[email protected] ~]# echo $HISTFILE/root/.bash_history[[email protected] ~]# echo $PS1[\[email protected]\h \W]\$ "

    Read-only variables

       readonly   不能修改值,不能销毁,只能等shell进程

    Script Basics
    Definition: According to the actual demand, combined with the command flow control mechanism realization of the source program
    Program return value

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

    Script Test

    bash -n test.sh     检查脚本是否有语法错误bash  -x  test.sh    单步执行,检查脚本错在那里

    Writing script considerations

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

    Shell arithmetic operations

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

    Shell Script Basics----Variables

    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.