我试图搜索如何在Bash函数中传递参数,但出现的总是如何从命令行传递参数。
我想在脚本中传递参数。我试着:
myBackupFunction("..", "...", "xx")
function myBackupFunction($directory, $options, $rootPassword) {
...
}
但是语法不正确。如何将参数传递给函数?
我试图搜索如何在Bash函数中传递参数,但出现的总是如何从命令行传递参数。
我想在脚本中传递参数。我试着:
myBackupFunction("..", "...", "xx")
function myBackupFunction($directory, $options, $rootPassword) {
...
}
但是语法不正确。如何将参数传递给函数?
当前回答
另一种向Bash传递命名参数的方法…是通过引用传递的。Bash 4.0支持这一点
#!/bin/bash
function myBackupFunction(){ # directory options destination filename
local directory="$1" options="$2" destination="$3" filename="$4";
echo "tar cz ${!options} ${!directory} | ssh root@backupserver \"cat > /mnt/${!destination}/${!filename}.tgz\"";
}
declare -A backup=([directory]=".." [options]="..." [destination]="backups" [filename]="backup" );
myBackupFunction backup[directory] backup[options] backup[destination] backup[filename];
Bash 4.3的另一种语法是使用nameref。
尽管nameref无缝地取消了引用,因此更加方便,但一些较旧的受支持发行版仍然发布了较旧的版本,因此我暂时不推荐使用它。
其他回答
一个简单的例子,将在执行脚本或调用函数时在脚本内部清除。
#!/bin/bash
echo "parameterized function example"
function print_param_value(){
value1="${1}" # $1 represent first argument
value2="${2}" # $2 represent second argument
echo "param 1 is ${value1}" # As string
echo "param 2 is ${value2}"
sum=$(($value1+$value2)) # Process them as number
echo "The sum of two value is ${sum}"
}
print_param_value "6" "4" # Space-separated value
# You can also pass parameters during executing the script
print_param_value "$1" "$2" # Parameter $1 and $2 during execution
# Suppose our script name is "param_example".
# Call it like this:
#
# ./param_example 5 5
#
# Now the parameters will be $1=5 and $2=5
去掉括号和逗号:
myBackupFunction ".." "..." "xx"
函数应该是这样的:
function myBackupFunction() {
# Here $1 is the first parameter, $2 the second, etc.
}
声明函数有两种典型的方法。我更喜欢第二种方法。
function function_name {
command...
}
or
function_name () {
command...
}
调用带参数的函数:
function_name "$arg1" "$arg2"
该函数根据参数的位置(而不是名称)引用传递的参数,即$1,$2,依此类推。$0是脚本本身的名称。
例子:
function_name () {
echo "Parameter #1 is $1"
}
此外,还需要在函数声明之后调用它。
#!/usr/bin/env sh
foo 1 # this will fail because foo has not been declared yet.
foo() {
echo "Parameter #1 is $1"
}
foo 2 # this will work.
输出:
./myScript.sh: line 2: foo: command not found
Parameter #1 is 2
参考:高级bash脚本编写指南。
另一种向Bash传递命名参数的方法…是通过引用传递的。Bash 4.0支持这一点
#!/bin/bash
function myBackupFunction(){ # directory options destination filename
local directory="$1" options="$2" destination="$3" filename="$4";
echo "tar cz ${!options} ${!directory} | ssh root@backupserver \"cat > /mnt/${!destination}/${!filename}.tgz\"";
}
declare -A backup=([directory]=".." [options]="..." [destination]="backups" [filename]="backup" );
myBackupFunction backup[directory] backup[options] backup[destination] backup[filename];
Bash 4.3的另一种语法是使用nameref。
尽管nameref无缝地取消了引用,因此更加方便,但一些较旧的受支持发行版仍然发布了较旧的版本,因此我暂时不推荐使用它。
它从用户那里获取两个数字,将它们提供给名为add的函数(在代码的最后一行),add将对它们求和并输出。
#!/bin/bash
read -p "Enter the first value: " x
read -p "Enter the second value: " y
add(){
arg1=$1 # arg1 gets to be the first assigned argument (note there are no spaces)
arg2=$2 # arg2 gets to be the second assigned argument (note there are no spaces)
echo $(($arg1 + $arg2))
}
add x y # Feeding the arguments