bash - 在 bash 中,如何设置变量来包含可变数量的命令行参数?

标签 bash shell command-line-arguments

我正在使用 bash shell。我正在编写一个脚本,我想捕获在参数 #5 之后(包括参数 #5)传递到我的脚本的可变数量的参数。到目前为止我有这个......

#!/bin/bash
…
declare -a attachments
attachments=( "$5" )

但是我不知道如何编写“附件”行来包含参数 #5 以及其后的任何参数。所以在下面的例子中

sh my_script.sh arg1 arg2 arg3 arg4 “my_file1.csv” “my_file2.csv”

我希望附件由“my_file1.csv”和“my_file2.csv”组成,而在此示例中......

sh my_script.sh arg1 arg2 arg3 arg4 “my_file1.csv” “my_file2.csv” “my_file3.csv”

我希望附件包含“my_file1.csv”、“my_file2.csv”和“my_file3.csv”。

最佳答案

srcdir=$1
destdir=$2
optflag=$3
barflag=$4
attachments=( "${@:5}" )

关于bash - 在 bash 中,如何设置变量来包含可变数量的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180159/

相关文章:

linux - 用于比较、选择和处理列的 awk

bash - 如何运行非阻塞 bash 命令并阻塞其输出?

windows - 使用 PowerShell 在计算机上查找特定的 .exe 文件

c - 使用 "*"作为函数 main() 的参数

linux - 逐行读取文本文件并检查该行是否包含特定字符串

c# - 将参数传递给应用程序

string - 如何在 Bash 中将一个字符串与多个正确值进行比较?

linux - 使用 SSH 的嵌套 grep

c++ - 命令行参数是一些字符串吗?

ruby - OptParse 中带有 2 个参数的选项