在最后一个参数之前提取的参数与QUOT; $ @" [英] Extract parameters before last parameter in "$@"
问题描述
我试图创建一个bash脚本,将在命令行给出的最后一个参数提取到在其他地方使用的变量。这里是我工作的脚本:
I'm trying to create a Bash script that will extract the last parameter given from the command line into a variable to be used elsewhere. Here's the script I'm working on:
#!/bin/bash
# compact - archive and compact file/folder(s)
eval LAST=\$$#
FILES="$@"
NAME=$LAST
# Usage - display usage if no parameters are given
if [[ -z $NAME ]]; then
echo "compact <file> <folder>... <compressed-name>.tar.gz"
exit
fi
# Check if an archive name has been given
if [[ -f $NAME ]]; then
echo "File exists or you forgot to enter a filename. Exiting."
exit
fi
tar -czvpf "$NAME".tar.gz $FILES
由于第一个参数可以是任意数量的,我必须找到一个方法来提取的最后一个参数(例如紧凑file.a file.b file.d文件-A-B-d.tar.gz)。因为它是现在存档名称将包含在文件紧凑。有没有办法做到这一点?
Since the first parameters could be of any number, I have to find a way to extract the last parameter, (e.g. compact file.a file.b file.d files-a-b-d.tar.gz). As it is now the archive name will be included in the files to compact. Is there a way to do this?
推荐答案
要从数组中删除最后一个项目,你可以使用这样的:
To remove the last item from the array you could use something like this:
#!/bin/bash
length=$(($#-1))
array=${@:1:$length}
echo $array
这篇关于在最后一个参数之前提取的参数与QUOT; $ @&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!