歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 《Linux命令行與shell腳本》筆記

《Linux命令行與shell腳本》筆記

日期:2017/3/3 12:05:43   编辑:SHELL編程

13.1 命令行參數

命令行參數:允許在運行腳本時向命令行添加數據值
[code]$ ./addem 10 30

13.1.1 讀取參數

bash shell會將一些稱為位置參數的特殊變量分配給命令行輸入的所有參數,者甚至包括shell執行的程序的名字 位置參數變量是標准的數字

$0:程序名

$1:第一個參數

2:第二個參數,以此類推,直到第9個參數2:第二個參數,以此類推,直到第9個參數9

[code]$cat test
#!/bin/bash

total=$[ $1 * $2 ]
echo The total value is $total.
$./test 2 5
The total value is 10.
shell參數可以是字符串,每個參數都是用空格分隔的,所以shell會將空格當成分隔兩個值的分隔符

參數值中包含空格,必須要用引號(單引號雙引號都可以)

[code]#cat test
#!/bin/bash

echo Hello $1, glad to meet you
$./test 'Rich Blum'
Hello Rich Blum, glad to meet you.
$
腳本需要多於9個命令行參數時,需要使用花括號({}),比如,${10}

13.1.2 讀取程序名

$0:獲取shell在命令行啟動的程序的名字 當傳給$0變量的真實字符串是整個腳本的路徑時,程序中就會使用整個路徑,而不僅僅是程序名

basename命令會只返回程序名而不包括路徑

[code]$cat test
#!/bin/bash
name=`basename $0`
echo The command entered is : $0
echo The command entered is : name
$./test
The command entered is : ./test
The command entered is : test
$/home/rich/test
The command entered is : /home/rich/test
The command entered is : test
$

13.1.3 測試參數

當腳本認為參數變量中有數據而實際上並沒有時,會得到一個錯誤

解決方法:在使用參數前檢查參數[ -n “$1” ]

13.2 特殊參數變量

13.2.1 參數計數

$#特殊變量:含有腳本運行時就有的命令行參數的個數,可以在腳本中任何地方使用,跟普通變量一樣 最後一個參數的表示形式是{!#}而不是{!#}而不是{$#}

當命令行上沒有任何參數時,#的值為0,在params變量中也為0,但#的值為0,在params變量中也為0,但{!#}變量返回命令行用到的腳本名

[code]$cat test
#!/bin/bash

if [ $# -ne 2 ]
then 
    echo Usage: test a b
else
    total=$[ $1 + $2 ]
    echo The total is $total
fi
$./test 
Usage: test a b
$./test 10
Usage: test a b
$./test 10 15
The total is 25
$./test 10 15 20
Usage: test a b

13.2.2 抓取所有的數據

∗和*和@變量提供了對所有參數的快速訪問,這兩個都能在單個變量中存儲所有的命令行參數

$*:會將命令行上提供的所有參數當做單個單詞保存,即當成一個參數

$@:將命令行上提供的所有參數當做同一字符串中的多個獨立的單詞,允許遍歷所有的值,將提供的每個參數分隔開來,通常用for命令完成

13.3 移動變量

shift命令:根據它們的相對位置來移動命令行參數 默認情況下會將每個參數變量減1,所以變量3的值會移動到3的值會移動到2,2的值會移動到2的值會移動到1,而變量$1的值會被刪除

shift命令可以提供一個參數n來執行多位移動,如shift 2:連續移動2位

當一個參數被移除後,它的值會被丟掉無法恢復

變量$0的值,也就是程序名不會改變

可以用shift命令遍歷命令行參數,尤其在不知道到底有多少個參數的時候

[code]$cat test
#!/bin/bash

count=1
while [ -n "$1" ]
do
    echo "Parameter #$count = $1"
    count=$[ $count + 1 ]
    shift
done
$
$./test rich barbara
parameter #1 = rich
Parameter #2 = barbara

13.4 處理選項

選項:跟在單破折線後面的單個字母,能改變命令的行為

13.4.1 查找選項

在命令行上,選項緊跟在腳本名之後,就跟命令行參數一樣處理簡單選項:在提取參數時,用case語句來判斷參數是否被格式化成了選項

[code]$cat test
#!/bin/bash

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Found the -a option";;
    -b) echo "Found the -b option";;
    *) echo "$1 is not an option";;
    esac
    shift
done
$
$./test -a -c
Found the -a option
-c is not an option
$
分離參數和選項

shell會用雙破折線來表明選項結束了,遇到雙破折線之後,腳本會安全地將剩下的命令行參數當做參數來處理,而不是選項

[code]$cat test
#!/bin/bash

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Found the -a option";;
    -b) echo "Found the -b option";;
    --) shift
            break;;
    *) echo "$1 is not an option";;
    esac
    shift
done

count=1
for param in $@
do
    echo "Parameter #$count: $param"
    count=$[ $count + 1 ]
done
$
$./test -a test1
Found the -a option
test1 is not an option
$./test -a -- test1
Found the -a option
Parameter #1: test1
處理帶值的選項

當命令行選項要求額外的參數時,腳本必須能檢測並能正確的處理

[code]$cat test
#!/bin/bash

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Found the -a option";;
    -b) param="$2"
          echo "Found the -b option, with parameter value $param"
          shift 2;;
    -c) echo "Found the -c option";;
    --) shift
            break;;
    *) echo "$1 is not an option";;
    esac
    shift
done

count=1
for param in "$@"
do
    echo "Parameter #$count: $param"
    count=$[ $count + 1 ]
done
$
$./test -a -b test1 -d
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
$
$./test -b test1 -a -d
Found the -b option, with parameter value test1
Found the -a option
-d is not an option
$
如果將多個選項放進一個參數中時,它就不工作了

[code]$ ./test -ac
-ac is not an option
$

13.4.2 使用getopt命令

getopt:識別命令行,從而在腳本中解析它們時更方便

命令格式:getopt options optstring parameters

optstring:定義了命令行有效的選項字母,還定義了哪些選項字母需要參數值

首先,在optstring中列出你要在腳本中用到的每個命令行選項字母

然後,在每個需要參數值的選項字母後加一個冒號

getopt命令會給予你定義的optstring解析提供的參數

[code]$getopt ab:cd -a -b test1 -cd test2 test3
-a -b test1 -c -d -- test2 test3
$
如果指定了一個不在optstring中的選項,默認情況下,getopt命令會產生一條錯誤信息

如果想忽略這條錯誤,在命令後加上-q選項

[code]$getopt ab:cd -a -b test1 -cde test2 test3
getopt: invail option --e
-a -b test1 -c -d -- test2 test3
$
$getopt -q ab:cd -a -b test1 -cde test2 test3
-a -b test1 -c -d -- test2 test3
$
在腳本中使用getopt

可以在腳本中使用getopt來格式化輸入給腳本的任何命令行選項

方法:

首先,用getopt命令生成的格式化後的版本來替換已有的命令行選項和參數,用set命令可以做到

set命令的選項之一是雙破折號,它會將命令行參數替換成set命令的命令行的值

然後,該方法將原始的腳本命令行參數傳遞給getopt命令

之後再將getopt命令的輸出傳給set命令,用getopt格式化後的命令行參數來替換原始的命令行參數

看起來如下:

[code]set -- `getopts -q ab:cd "$@"`
[code]$cat test
#!/bin/bash

set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Found the -a option";;
    -b) param="$2"
          echo "Found the -b option, with parameter value $param"
          shift ;;
    -c) echo "Found the -c option";;
    --) shift
            break;;
    *) echo "$1 is not an option";;
    esac
    shift
done

count=1
for param in "$@"
do
    echo "Parameter #$count: $param"
    count=$[ $count + 1 ]
done
$
$./test -a -b test1 -cd test2 test3
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2'
Parameter #1: 'test3'

Copyright © Linux教程網 All Rights Reserved