<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            linux shell編程對變量的賦值

            時間:2024-09-05 05:39:02 Linux認證 我要投稿
            • 相關推薦

            linux shell編程對變量的賦值

              shell對變量的賦值,所有的變量都是由字符串組成,不需要對變量名預先聲明,而且有很多關鍵字供編程者使用。下面列舉例子詳細說明。 在終端下建立文件 #vi s2.sh

              #!/bin/sh

              #set a variable a

              a="hello world"

              #print a

              echo "A is :"

              echo $a

              在這個程序當中,可以看到shell編程與C編程的區別,沒有那么嚴格的語法,在每句不需要加“;”,

              而且在shell中引用變量的時候前面加“$”。

              1 當變量名與其他文字混淆的時候,需要加“{}”

              #!/bin/sh

              # set a variable num

              num=2

              echo "the variable num is $numnd"

              運行程序后,答案并不是 the variable num is 2nd,而是什么都沒有輸出。因為bash會尋找numnd變量,但是并沒有給這個變量賦值。所以將程序改為:

              echo "the variable num is {$num}nd"

              2 默認變量

              $#:傳入腳本的命令行參數個數

              $*:所有命令行的參數值,而且各個參數值之間有空格表示

              $0:命令本身,就是要執行的shell文件名

              $1:第一個命令行參數

              $2:第二個命令行參數

              $3

              $4

              example:

              #!/bin/sh

              echo "number of vars:"$#

              echo "number of vars:"$0

              echo "number of vars:"$*

              echo "number of vars:"$1

              echo "number of vars:"$2

              echo "number of vars:"$3

              運行./file 11 22 33

              輸出結果:

              3

              file

              11 22 33

              11

              22

              33

              3局部變量

              在變量首次賦值是加關鍵字local可以聲明為局部變量。

              #!/bin/sh

              hello="aa"

              echo $hello

              function fucn1{

              local hello="bb"

              echo $hello

              }

              func1

              echo $hello

            【 linux shell編程對變量的賦值】相關文章:

            Linux shell常用命令匯總10-26

            Linux系統shell工具打印輸出08-16

            Linux Shell文本處理工具10-08

            2024年Linux認證考試要點:shell程序控制結構語句09-20

            PHP的變量的介紹11-06

            php調用shell的方法技巧07-15

            關于Shell腳本調試技術09-30

            Linux認證概念10-29

            Linux認證優勢06-10

            Linux認證考點06-25

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看