我正在尝试创建一个命令,该命令允许用户输入名称作为他们想要创建的压缩文件(将成为 tar.gz 文件)的第一个参数,并将文件和目录名称作为第二个参数。
到目前为止,我有这个脚本
name_of_archive=$1
directory_or_file_paths=$2
if [ $# -eq 0 ]
then
echo "Usage: $0 [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
第一个是目录,第二个是文件。如果我尝试单独压缩两者,它可以工作,但如果我尝试一次压缩多个文件或目录或混合,则它不起作用。我希望它能够做到这一点。