gpt4 book ai didi

linux - 如何在接收参数的shell脚本中创建一个函数?

转载 作者:IT王子 更新时间:2023-10-29 00:08:35 30 4
gpt4 key购买 nike

我正在处理一个 shell 脚本,我有一些重复的代码行(比方说复制粘贴)。

我希望这些行在一个函数中。正确的语法是什么?

为了让这些函数接收参数,我应该做哪些更改?

这是一个例子。

我需要把这个:

amount=1
echo "The value is $amount"
amount=2
echo "The value is $amount"

像这样:

function display_value($amount) {
echo "The value is $amount"
}

amount=1
display_value($amount)
amount=2
display_value($amount)

这只是一个例子,但我认为已经足够清楚了。

提前致谢。

最佳答案

function display_value() {
echo "The value is $1"
}

amount=1
display_value $amount
amount=2
display_value $amount

关于linux - 如何在接收参数的shell脚本中创建一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655517/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com