zoukankan      html  css  js  c++  java
  • LINUX SHELL 笔记 02: 变量初识

    https://www.shellscript.sh/variables1.html

    变量是一个可操作(读、写)的内存块的名字。

    尝试-1

    创建一个变量:

    root@iZwz:~/labs# sh mytest.sh 
    Hello.
    root@iZwz:~/labs# cat mytest.sh 
    #!/bin/bash
    MY_MESSAGE="Hello."
    echo $MY_MESSAGE

     赋值号两边不应该有空格!~

    尝试-2

     从标准输入读取一行:

    root@iZwz:~/labs# sh mytest.sh 
    WHAT IS YOUR NAME?
    xkfx
    Hello xkfx - HOPE YOU WELL.
    root@iZwz:~/labs# cat mytest.sh 
    #!/bin/bash
    echo WHAT IS YOUR NAME?
    read MY_NAME
    echo "Hello $MY_NAME - HOPE YOU ARE WELL."

    2、关于变量

    变量里面到底是什么?

    一切都是“字符串”。

    如果你认为 sum 里面是数字就大错特错了。

    不论是整数、实数或是一切你喜欢的类型,

    都将作为字符串存储。

    只是 let 让解释器把变量当成 ...

    (向后引用 :P)

    变量的作用域

    export. (dot) 是两个很常用的命令,弄清楚这两个命令到底让计算机做了什么,就能很容易理解“变量的作用域”了 ~

    Ⅰ、关于 export:

    export命令用于将shell变量输出为环境变量,或者将shell函数输出为环境变量。 一个变量创建时,它不会自动地为在它之后创建的shell进程所知。而命令export可以向后面的shell传递变量的值。当一个shell脚本调用并执 行时,它不会自动得到原为脚本(调用者)里定义的变量的访问权,除非这些变量已经被显式地设置为可用。export命令可以用于传递一个或多个变量的值到任何后继脚本。
    
    来自: http://man.linuxde.net/export

     mytest.sh 的代码在下面下面!

    root@iZwz:~/labs# export MSG="AAAAAAAA"
    root@iZwz:~/labs# sh mytest.sh 
    MSG=AAAAAAAA
    MSG=VALUE-2

    Ⅱ、一旦一个脚本执行完毕,它所创建的“环境”、“变量”都将被破坏,我们可以通过 . 来引用脚本,而不是创建一个新的 shell 来运行它

    root@iZwz:~/labs# cat mytest.sh 
    #!/bin/bash
    echo "MSG=$MSG"
    MSG="VALUE-2"
    echo "MSG=$MSG"
    
    root@iZwz:~/labs# MSG="VALUE-1"
    root@iZwz:~/labs# sh mytest.sh # RUN THROUGH sh COMMAND, WHICH CREATE A NEW SHELL TO RUN IT.
    MSG=
    MSG=VALUE-2
    root@iZwz:~/labs# . mytest.sh # RUN THROUGH . COMMAND, WHICH USES CURRENT SHELL TO RUN IT.
    MSG=VALUE-1
    MSG=VALUE-2

    3、何时使用 ${} ? 

    #!/bin/bash
    echo "What is your name?"
    read USER_NAME
    echo "Hello $USER_NAME"
    echo "I will create you a file called ${USER_NAME}_file"
    touch "${USER_NAME}_file"
    root@iZwz:~/labs# sh mytest.sh 
    What is your name?
    xkfx
    Hello xkfx
    I will create you a file called xkfx_file
    root@iZwz:~/labs# ls
    mytest.sh  xkfx_file
  • 相关阅读:
    17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
    17.1.2 Replication Formats
    Setting the Master Configuration on the Slave
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment
    17.1.1.8 Setting Up Replication with Existing Data
    17.1.1.7 Setting Up Replication with New Master and Slaves
    17.1.1.6 Creating a Data Snapshot Using Raw Data Files
    列出display的值,并说明它们的作用
    CSS设置DIV居中
    CSS选择符有哪些?哪些属性可以继承?优先级算法如何计算?
  • 原文地址:https://www.cnblogs.com/xkxf/p/7634280.html
Copyright © 2011-2022 走看看