Shell script: 命令行參數(shù)

$0: 代表第一個參數(shù),通常是腳本本身的名字
$1: 代表第二個參數(shù)
...
以此類推

例如:

#!/bin/bash

echo "\$0="$0
echo "\$1="$1
echo "\$2="$2

運行結(jié)果:

$ ./a.sh a b
$0=./a.sh
$1=a
$2=b

$@表示從第一到第n個參數(shù),例如:

#!/usr/bin/bash

for arg in $@
do
    echo $arg
done

運行結(jié)果:

$ ./a.sh a b c
a
b
c

還有,當入?yún)⒉淮嬖诘臅r候,可以有缺省值,例如:

#!/usr/bin/bash
para1=${1:-xxx}
echo $para1

運行結(jié)果:

$ ./c.sh abc
abc
$ ./c.sh 
xxx

更多內(nèi)容,定義這里,感興趣的自己看吧:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

${parameter:-[word]}
Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
${parameter:=[word]}
Assign Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be assigned to parameter. In all cases, the final value of parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.
${parameter:?[word]}
Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall be substituted. An interactive shell need not exit.
${parameter:+[word]}
Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word (or an empty string if word is omitted) shall be substituted.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,654評論 19 139
  • 1.創(chuàng)建文件夾 !/bin/sh mkdir -m 777 "%%1" 2.創(chuàng)建文件 !/bin/sh touch...
    BigJeffWang閱讀 10,495評論 3 53
  • 一,shell題記 不懂shell的程序員不是好程序員,學習shell是為了自動化,使用自動化可以非常有效的提高工...
    架構(gòu)師小秘圈閱讀 8,793評論 3 70
  • 與其抱怨生活糟糕,為什么不低頭審視自己。人的內(nèi)在變了,外在才會跟著變化,因為新的你才配得上新的生活。絕大多數(shù)人都在...
    e655d1e64997閱讀 282評論 0 1
  • 咻! 天空之上,一道流光以一種驚人的速度掠過,隱約仿佛有著龍吟傳蕩開來,那是一道虛幻的龍影,龍影馱負著一道人影,自...
    混沌天書閱讀 318評論 0 0

友情鏈接更多精彩內(nèi)容