0

我想要的是用户使用zenity从任何地方选择一个文件,脚本将检测文件扩展名,例如(.tar.gz)或(.zip),并相应地对它们执行操作。这是一个例子。

#! /bin/bash

FILE=$(zenity --file-selection --title="Select a file to check")
echo "File: $FILE"

if [ "$FILE" = "*.zip" ] 
then
    echo "File that is a .zip found"
    FILENAMENOEXT="${FILE%.*}"
    echo "Filename with extention $FILENAMENOEXT"
    #Perform xx action to $FILE if it is a zip

elif [ "$FILE" = "*.tar.gz" ]
then
echo "File is a .tar.gz found"
FILENAMENOEXT="${FILE%.tar.*}"
echo "Filename with extention $FILENAMENOEXT"
#Perform xx action to $FILE if it is a t.tar.gz

else
    echo "File is neither .zip nor .tar.gz"
fi

echo "test $FILENAMENOEXT"
4

1 回答 1

2

这几乎是正确的。

您需要使用[[做模式匹配和引号禁用模式匹配。

所以不是[ "$FILE" = "*.zip" ]你想要[[ "$FILE" = *".zip" ]]的,而不是[ "$FILE" = "*.tar.gz" ]你想要的[[ "$FILE" = *".tar.gz" ]]

您也可以使用case语句代替if/ elif

case "$FILE" in
*.zip)
    echo "File that is a .zip found"
    ;;
*.tar.gz)
    echo "File is a .tar.gz found"
    ;;
*)
    echo "File is neither .zip nor .tar.gz"
    ;;
esac
于 2015-07-21T14:17:07.177 回答