![](/img/trans.png)
[英]shell script - how to use variables for a shell script without defining the variable in the script
[英]Defining a variable when running a shell script?
我正在嘗試編寫基本進入特定文件夾並執行某些操作的Shell腳本。 這里的問題是文件夾名稱是變量,即/path/to/variable
我當時在想,是否可以從命令行運行Shell腳本並在同一行中定義變量? 就像是:
./run.sh $id=456
當它運行時,它的路徑為/path/to/$id
,因此為/path/to/456
。
您可以按照以下樣式運行腳本:
./run.sh 465
比起您可以使用$1
(第一個參數), $2
(第二個參數)等等訪問腳本中參數的內容。 要更改目錄,請使用cd /path/to/$1
BTW: $#
返回參數計數。
另一種方法是使用參數,例如: http : //www.linux.com/archive/feature/118031因此,您可以像這樣運行腳本: ./run.sh -d 465
(其中-d代表目錄)
反過來。
id=456 ./run.sh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.