天天看點

linux shell 擷取最後一個參數,bash – 在“$@”中的最後一個參數之前提取參數

我試圖建立一個Bash腳本,它将從指令行提取的最後一個參數提取到一個變量,以供其他地方使用。這裡是我正在處理的腳本:

#!/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 ... .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

由于第一個參數可以是任何數字,我必須找到一種方法來提取最後一個參數(例如compact file.a file.b file.d files-a-b-d.tar.gz)。由于現在的存檔名稱将包含在要壓縮的檔案中。有沒有辦法做到這一點?