获取n个最新的参数shell脚本



所以我有一些脚本

files="${!#}"
if [[ -n "$files" ]]
then
do some instructions
else
do some instructions
fi

我真的需要知道如何从第7个自变量或n个自变量中得到n个自变量。类似这样的东西:

var1=arg1 arg2 arg3 .... arg7 argN argN+1 ...

在这样的变量中捕获它们:

var1= argN argN+1 ....

您可以使用bash数组,然后对其进行切片:

正如@GordonDavison所指出的,你可以直接切片$@

#!/bin/bash
files=( "${@:7}" )
# show content of the array
declare -p files

使用shift删除第一个N-1参数,然后$@将包含其余参数。

shift 6
if [ $# -ne 0 ]
then
files=("$@")
# do something with $files
else
# do something else
fi

最新更新