国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
Accessing Arguments with Positional Parameters
Handling Multiple or Optional Arguments
Using getopts for Command-Line Options
首頁 系統(tǒng)教程 Linux 如何將爭論傳遞給bash腳本?

如何將爭論傳遞給bash腳本?

Jun 18, 2025 am 12:23 AM
參數(shù)傳遞 bash腳本

在Bash腳本中傳遞參數(shù)可通過在腳本名後直接輸入?yún)?shù)實現(xiàn),這些參數(shù)在腳本中通過$1、$2等位置變量訪問。例如運行./greet.sh Alice時,$1表示Alice。若需處理多個參數(shù),可使用循環(huán)配合"$@"遍歷所有參數(shù)。此外,使用getopts函數(shù)可解析帶選項的參數(shù)(如-n或-a),提升腳本靈活性與易用性。

How to pass arguments to a bash script?

You can pass arguments to a bash script by simply typing them after the script name when you run it. The syntax looks like this:

 ./script.sh arg1 arg2 arg3

The arguments ( arg1 , arg2 , etc.) are then accessible inside the script using special variables like $1 , $2 , $3 , and so on.


Accessing Arguments with Positional Parameters

Inside your bash script, each argument is available via positional parameters:

  • $0 – the name of the script itself
  • $1 – the first argument
  • $2 – the second argument
  • ...and so on

For example, if your script is called greet.sh and contains this:

 echo "Hello $1"

Then running:

 ./greet.sh Alice

Will output:

 Hello Alice

This is the most basic and commonly used way to handle script arguments.

If you have more than 9 arguments, use ${10} , ${11} , etc., since just $10 would be interpreted as $1 followed by the number 0.


Handling Multiple or Optional Arguments

Sometimes you want to allow multiple or optional inputs. Bash makes this easy with built-in variables like:

  • $@ – all arguments as separate strings
  • $* – all arguments as one single string
  • $# – the number of arguments passed

A common use case is looping through all arguments:

 for name in "$@"
do
    echo "Hello $name"
done

Running:

 ./greet.sh Alice Bob Charlie

Would print:

 Hello Alice
Hello Bob
Hello Charlie

This approach gives you flexibility when handling variable numbers of inputs.

Also, you can check how many arguments were given using $# . For example:

 if [ $# -lt 2 ]; then
    echo "Need at least two arguments!"
    exit 1
fi

This ensures your script doesn't run with incomplete input.


Using getopts for Command-Line Options

If your script supports flags or options (like -n or -v ), getopts is the built-in tool for parsing them.

Here's a simple example that accepts -n for name and -a for age:

 while getopts n:a: option; do
    case $option in
        n) NAME=$OPTARG;;
        a) AGE=$OPTARG;;
    esac
done

echo "Name: $NAME, Age: $AGE"

Run it like this:

 ./script.sh -n Alice -a 30

It keeps things clean when dealing with scripts that accept configuration flags or settings from the command line.

Keep in mind that options with arguments need a colon after their letter in the getopts string (eg, n: means -n expects a value).


That's basically it — you can start with positional parameters for simple needs, and move to getopts when you want to support flags and options. It's not complicated, but getting the syntax right helps avoid bugs later.

以上是如何將爭論傳遞給bash腳本?的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1600
29
PHP教程
1502
276
優(yōu)化Golang函數(shù)參數(shù)傳遞效能的最佳實踐 優(yōu)化Golang函數(shù)參數(shù)傳遞效能的最佳實踐 Apr 13, 2024 am 11:15 AM

為了優(yōu)化Go函數(shù)參數(shù)傳遞性能,最佳實踐包括:使用值類型避免復(fù)制小型值類型;使用指針傳遞大型值類型(結(jié)構(gòu)體);使用值類型傳遞切片;使用接口傳遞多態(tài)類型。在實踐中,傳遞大型JSON字符串時,傳遞data參數(shù)指針可以顯著提高反序列化性能。

PHP 函數(shù)的參數(shù)傳遞方式是什麼? PHP 函數(shù)的參數(shù)傳遞方式是什麼? Apr 10, 2024 am 11:06 AM

PHP參數(shù)傳遞有兩種方式:傳值呼叫(參數(shù)作為值的副本傳遞,函數(shù)內(nèi)修改不影響原變數(shù))和引用傳遞(參數(shù)的位址傳遞,函數(shù)內(nèi)修改會影響原變數(shù)),在需要修改原變量的情況下使用引用傳遞,如購物車總價計算時需要引用傳遞才能正確計算。

如何傳遞參數(shù)到 PHP 函數(shù)? 如何傳遞參數(shù)到 PHP 函數(shù)? Apr 10, 2024 pm 05:21 PM

PHP函數(shù)可以透過參數(shù)傳遞值,分為按值傳遞和按引用傳遞:按值傳遞:函數(shù)內(nèi)部對參數(shù)修改不會影響原始值;按引用傳遞:函數(shù)內(nèi)部對參數(shù)修改會影響原始值。此外,還可以傳遞數(shù)組作為參數(shù),用於計算資料總和等操作。

C++ 函式參數(shù)詳解:不同指標(biāo)型別的傳參方式對比 C++ 函式參數(shù)詳解:不同指標(biāo)型別的傳參方式對比 Apr 27, 2024 am 09:27 AM

C++中指標(biāo)參數(shù)的傳參方式有三種:傳值、傳引用、傳送位址。傳值複製指針,不影響原始指針;傳引用允許函數(shù)修改原始指針;傳送位址允許函數(shù)修改指針指向的值。根據(jù)需要選擇合適的傳參方式。

C++ 函數(shù)參數(shù)詳解:並行程式設(shè)計中參數(shù)傳遞的效能最佳化 C++ 函數(shù)參數(shù)詳解:並行程式設(shè)計中參數(shù)傳遞的效能最佳化 Apr 27, 2024 pm 02:09 PM

多線程環(huán)境中,函數(shù)參數(shù)傳遞方式不同,性能差異顯著:按值傳遞:復(fù)制參數(shù)值,安全,但大型對象開銷大。按引用傳遞:傳遞引用,效率高,但函數(shù)修改會影響調(diào)用者。按常量引用傳遞:傳遞常量引用,安全,但限制函數(shù)對參數(shù)操作。按指針傳遞:傳遞指針,靈活,但指針管理復(fù)雜,可能出現(xiàn)懸垂指針或內(nèi)存泄漏。并行求和中,按引用傳遞效率優(yōu)于按值傳遞,按指針傳遞靈活度最高,但管理復(fù)雜。

Golang形參考指南:參數(shù)傳遞方式、傳值與傳址 Golang形參考指南:參數(shù)傳遞方式、傳值與傳址 Mar 02, 2024 pm 05:18 PM

Golang形參要求指南:參數(shù)傳遞方式、傳值與傳址在學(xué)習(xí)Golang程式語言過程中,了解參數(shù)傳遞的方式以及傳值和傳址的概念是非常重要的。本文將深入探討Golang中的形參要求,包括參數(shù)傳遞方式、傳值和傳址的區(qū)別,並提供具體的程式碼範(fàn)例幫助讀者更好地理解。一、參數(shù)傳遞方式在Golang中,函數(shù)的參數(shù)傳遞方式有兩種:傳值和傳址。傳值(傳遞副本):當(dāng)函數(shù)呼叫時,實際

Go語言中的參數(shù)傳遞方式探究 Go語言中的參數(shù)傳遞方式探究 Apr 03, 2024 pm 02:48 PM

在Go語言中,函數(shù)參數(shù)的傳遞方式主要有兩種:值傳遞:傳遞變數(shù)的副本,不會影響呼叫程式碼中的原始變數(shù)。指標(biāo)傳遞:傳遞變數(shù)的位址,允許函數(shù)直接修改呼叫程式碼中的原始變數(shù)。

C++函式的參數(shù)傳遞方式全面解析 C++函式的參數(shù)傳遞方式全面解析 Aug 21, 2023 pm 09:51 PM

C++是一門優(yōu)秀的程式語言,擁有強(qiáng)大的功能和靈活的語法。在C++程式設(shè)計中,我們常常需要傳遞參數(shù)給函數(shù),在傳遞參數(shù)時,參數(shù)傳遞方式就變成了一個非常重要的問題。本文將從C++函數(shù)的參數(shù)傳遞方式方面,全面解析參數(shù)傳遞方式的不同之處,包括傳值、傳引用和傳指標(biāo)等。一、傳值參數(shù)傳遞方式在C++中,當(dāng)我們將參數(shù)傳遞給函數(shù)時,傳值參數(shù)傳遞方式是最常見的方式。傳值參數(shù)傳遞方式是指

See all articles