zoukankan      html  css  js  c++  java
  • shell 变量

    本文参考:http://www.runoob.com/linux/linux-shell-variable.html

    echo $readonly yun='hello world' YUN='hell hadoop' yun_='iii' yun99='edrr' echo $yun$YUN$yun_"happy${yun99}new year" readonly s='readonly' echo $s s='readonly2' echo $s
    s='readonly2'只读变量运行的显示结果:
    hello worldhell hadoopiiihappyedrrnew year
    readonly
    ./variableshell.sh: line 9: s: readonly variable
    readonly

    只会在出错的一行显示错误

    删除变量:

    yun99='edrr'
    yun='hello world'
    YUN='hell hadoop'
    yun_='iii'
    yun99='edrr'
    
    readonly s='readonly'
    echo $s
    s='readonly2'
    echo $s
    
    unset yun
    echo $yun
    unset s
    echo $s
    
    
    
    结果:
    readonly
    ./variableshell.sh: line 8: s: readonly variable
    readonly
    
    ./variableshell.sh: line 13: unset: s: cannot unset: readonly variable
    readonly
    yun99="edrr"
    yun="hello world"
    YUN="hell hadoop"
    yun_="iii"
    yun99="edrr"
    
    readonly s="readonly"
    echo $s
    s="readonly2"
    echo $s
    
    unset yun
    echo $yun
    unset s
    echo $s
    
    
    
    结果:
    readonly
    ./variableshell.sh: line 8: s: readonly variable
    readonly
    
    ./variableshell.sh: line 13: unset: s: cannot unset: readonly variable
    readonly

    总结:只读变量不可更改,不可删除;一般变量删除后输出是一行空白

    变量命名规则:

    注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则:

    • 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。
    • 中间不能有空格,可以使用下划线(_)。
    • 不能使用标点符号。
    • 不能使用bash里的关键字(可用help命令查看保留关键字)

    变量类型:

    运行shell时,会同时存在三种变量:

    • 1) 局部变量 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。
    • 2) 环境变量 所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。
    • 3) shell变量 shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行

    shell字符串:

      获取字符串长度:

    str="www.baidu.com"
    echo ${#str} 

    单引号

    str='this is a string'

    单引号字符串的限制:

    • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
    • 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。

    双引号

    your_name='qinjx'
    str="Hello, I know your are "$your_name"! 
    "

    双引号的优点:

    • 双引号里可以有变量
    • 双引号里可以出现转义字符

    拼接字符串

    your_name="qinjx"
    greeting="hello, "$your_name" !"
    greeting_1="hello, ${your_name} !"
    echo $greeting $greeting_1

    获取字符串长度

    string="abcd"
    echo ${#string} #输出 4

    提取子字符串

    以下实例从字符串第 2 个字符开始截取 4 个字符:

    string="runoob is a great site"
    echo ${string:1:4} # 输出 unoo

    查找子字符串

    查找字符 "is" 的位置:

    string="runoob is a great company"
    echo `expr index "$string" is`  # 输出 8

    注意: 以上脚本中 "`" 是反引号,而不是单引号 "'";如何输出反引号,英文输入法状态下,esc键下面的地方,(~`)这个按钮就可以打出

    数组变量:

    array_name=(111 222 333 444 5550)
    echo ${array_name[2]}
    echo ${array_name[@]}
    length=${#array_name[@]}
    echo $length
    
    输出:
    333
    111 222 333 444 5550
    5

    Shell 注释

    以"#"开头的行就是注释,会被解释器忽略。

    sh里没有多行注释,只能每一行加一个#号。只能像这样:

    #--------------------------------------------
  • 相关阅读:
    csu 1141 节能
    WA:ZOJ 1025 Wooden Sticks
    UVa 531 Compromise
    UVa 437 The Tower of Babylon
    UVa 10285 Longest Run on a Snowboard
    在asp.net使用web.config身份验证
    掌握 Ajax,第 6 部分: 建立基于 DOM 的 Web 应用程序
    面向对象编程思想
    一步一步学Remoting之五:异步操作
    Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
  • 原文地址:https://www.cnblogs.com/ljangle/p/9066752.html
Copyright © 2011-2022 走看看