如何传递一个具有多个参数的变量来同时运行?

How to pass a variable which has multiple arguments to function all at once?

我正在尝试创建一个命令,该命令允许用户输入名称作为他们要创建的压缩文件(将成为 tar.gz 文件)的第一个参数以及文件和目录名称第二个参数。

到目前为止我有这个脚本

name_of_archive=
directory_or_file_paths=

if [ $# -eq 0 ]
then
        echo "Usage: [=10=] [name of archive you wish to create] [path of directories or files you wish to compress]"
        echo "You must enter atleast one file or directory name"
        exit 1
else
        if [ -e "$directory_or_file_paths" ] || [ -d "$directory_or_file_paths" ]
        then
                tar -czvf "$name_of_archive".tar.gz "$directory_or_file_paths"
                echo "The filenames or directories you specified have been compressed into an archive called $name_of_archive.tar.gz"
        else
                echo "Some or all of the file or directory names you have given do not exist"
        fi
        exit
fi

这是我使用命令时得到的结果:

compression2.bash compression1 ./test ./listwaste
./test/
./test/test2/
./test/test2/2
./test/1
The filenames or directories you specified have been compressed into an archive called compression1.tar.gz

第一个是目录,第二个是文件。如果我尝试单独压缩两者,它会起作用,但如果我尝试一次压缩多个文件或目录或混合,则不起作用。我希望它能够做到这一点。

那是因为您只查看第二个参数并将其放入您的 directory_or_file_paths 变量中。每当 Linux 在您的命令中找到 space 时,它会将其视为另一个参数,因此您甚至不会查看这些其他文件或文件夹。你需要做的是,如果参数的数量不为 0,并且你有第一个作为你的 name_of_archive,那么你将需要遍历所有剩余的参数并构造一个包含所有参数的字符串, 由 spaces 分隔,这是您将作为参数提供给 tar 命令的内容。

我认为您想在第一次输入存档的变量名称后使用 shift。然后您可以传递整个列表,而不是只传递一个存档参数。

name_of_archive=
shift
directory_or_file_paths=("$@")
...

https://ss64.com/bash/shift.html

将文件名存储在字符串中不是一个好主意。将它们存储在数组中是一种更好的方法:

#!/usr/bin/env bash

[[ $# -lt 2 ]] && exit 1

name=; shift
files=("$@")

#exclude all files/directories that are not readable
for index in "${!files[@]}"; do
   [[ -r ${files[index]} ]] || unset "files[index]"
done

[[ ${#files[@]} -eq 0 ]] && exit 1    

if tar -czvf "${name:-def_$$}.tar.gz" "${files[@]}"; then
   echo "Ok"
else
   echo "Error"
   exit 1
fi

shift; files=("$@") 丢弃第一个参数(名称)并将其余参数(文件名)保存到数组中。


您还可以使用更直接的方法为 tar 构建文件名数组:

name=; shift

for file; do
   [[ -r $file ]] && files+=("$file")
done