获取 n 个最新参数 shell 脚本

get n latest arguments shell script

所以我有一些脚本

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 ....

使用 shift 删除前 N-1 个参数,然后 $@ 将包含剩余的参数。

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

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

正如@GordonDavisson 指出的那样,您可以直接切片 $@

#!/bin/bash

files=( "${@:7}" )

# show content of the array
declare -p files