我正在尝试使用 bash 脚本压缩和解压缩文件,但我是新手,遇到了问题。我将解释该脚本当前的作用,然后解释我希望它做什么。
用户选择“压缩”:出现一个文件对话框,显示所有文件。用户选择一个文件(例如
/home/ubuntu/file.txt
)。该文件被压缩并保存/home/ubuntu
为file.zip
包含/home/ubuntu/file.txt
.用户选择“解压缩”:出现一个文件对话框,显示所有文件。用户选择一个文件(例如
/home/ubuntu/file.zip
。该文件被
解压缩。
现在我想从脚本中执行以下操作:
- 压缩文件后,我希望该压缩文件的目录相同。例如,对于
/home/ubuntu/filename.txt
,我希望将其保存在/home/ubuntu/
as中filename.zip
。我希望它只包含filename.txt
inside 而不是/home/ubuntu/filename.txt
orhome/ubuntu/filename.txt
。 - 解压缩时,我希望文件对话框仅显示
*.zip
文件,以便用户无法选择未压缩的文件。 - 解压时,我想知道文件保存在哪里。
这是我的代码:
#! /bin/bash
#This bash compresses and decompresses files
act=-1 #A variable used to transfer which action has been chosen
action1="Compress"
action2="Decompress" #action1 & 2 both used for certain echoes
function menu { #The menu
clear
local title="-------Menu-------"
local prompt="Please choose an action:"
local options=("$action1" "$action2" "Quit")
echo "$title"
PS3="$prompt" #Changes the default '#?' that the select command uses to $prompt.
select option in "${options[@]}";do
case $option in
${options[0]})
echo "You chose $option"
act=0 #Changes act to 0 , to be used later
break
;;
${options[1]})
echo "You chose $option"
act=1 #Changes act to 1 , to be used later
break;
;;
${options[2]})
echo "You chose $option"
exit
;;
*)
echo "Invalid option please choose between 1-3"
;;
esac
break
done
}
function ynPrompt { #a y/n prompt to go back to the main menu or exit
while true #helps to loop the y/n prompt in case of wrong input ex. a343
do
read -r -p "Do you want to go back to the menu? [y/N] " response
case $response in
[yY][eE][sS]|[yY]) #accepts yes or y and ignores casing ex. yEs is accepted.
continue 2 #continues the outer control loop
;;
[nN][oO]|[nN]) #accepts no or n and ignores casing ex. nO is accepted.
exit #exits the script
;;
*)
continue #shows the message again
;;
esac
break
done
}
function main { #Performs the selected action
if [ $act -eq 0 ]; then
if zip -r ${path}.zip ${path}
then echo Compression successful
echo $? #prints 0
else echo $?
fi
#echo "$action1"
#zip -r ${path}.zip ${path}
elif [ $act -eq 1 ]; then
if unzip ${path}
then echo Decompression successful
echo ${path}
echo $? #prints 0
else echo $?
fi
#echo "$action2"
#unzip ${path}
else
echo "$error"
fi
}
#~~~~~~~~~~~~ Script start ~~~~~~~~~~~~~~~~
while true #outer control loop
do
menu #call menu
cd /home
path=$(zenity --file-selection) #Stores the path of the file into path variable through zenity dialog
#path can only be .zip if i had --file filter *.zip
if [ -z $path ]; then #checks length of $path , returns true if length = 0
ynPrompt #call ynprompt
else
main #call main
fi
break
done
答案1
您可以使用它进行压缩:
compress_file ()
{
local dir file
test -f "$1" || return 2
dir="$(readlink -f "$1")"
file="${dir##*/}"
dir="${dir%/*}"
cd "$dir"
# check whether target file exists:
# test -f "$file".zip && : whatever
echo zip "$file".zip "$file"
}
compress_file /path/to/file
选择解压文件
我不熟悉zenity
。好像不能过滤文件。您可以通过创建一个临时目录、仅将*.zip
文件链接到其中并zenity
针对该目录运行来达到所需的效果。当然,如果用户选择不同的目录,那么他将看到所有文件。
zipfile_dialog ()
{
local file startdir="/home/ubuntu" tmpdirname=.zipscript.$$
cd "$startdir" || return 2
test -d "$tmpdirname" && { rm -r "$tmpdirname" || return 2; }
mkdir -p "$tmpdirname" || return 2
for file in *.zip; do
cd "$tmpdirname"
ln -s ../"$file"
cd ..
done
ls "$tmpdirname"
# call zenity here
rm -r "$tmpdirname"
}
zipfile_dialog
另一种方法是使用 shell 进行文件选择。如果您可以选择的话,可以通过可编程完成来完成(complete
,compgen
)。