圧縮/圧縮解除するファイルを選択するためのBashスクリプト

圧縮/圧縮解除するファイルを選択するためのBashスクリプト

Bashスクリプトを使用してファイルを圧縮および解凍しようとしましたが、この機能が初めてで問題が発生しました。スクリプトが現在何をしているのかを説明し、次に私が望むものを説明します。

  • ユーザーが「圧縮」を選択すると、すべてのファイルを表示するファイルダイアログボックスが表示されます。ユーザーはファイル(たとえば/home/ubuntu/file.txt)を選択します。ファイルが圧縮され /home/ubuntuています。file.zip/home/ubuntu/file.txt

  • ユーザーが「抽出」を選択すると、すべてのファイルを表示するファイルダイアログボックスが表示されます。ユーザーはファイルを選択します(たとえば/home/ubuntu/file.zip、ファイル
    を解凍します)。

今スクリプトで次のことをしたいと思います。

  1. ファイルを圧縮した後、圧縮されたファイルのディレクトリが同じになるようにします。例えば/home/ubuntu/filename.txt。 or以外の内部にのみ含めたいです。/home/ubuntu/filename.zipfilename.txt/home/ubuntu/filename.txthome/ubuntu/filename.txt
  2. 解凍時にファイルダイアログボックスに*.zipファイルのみを表示して、ユーザーが圧縮されていないファイルを選択できないようにしたいと思います。
  3. 解凍時にファイルがどこに保存されているかを知りたいです。

これは私のコードです。

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

別の方法は、ファイルの選択にシェルを使用することです。オプション(complete、、compgen)がある場合は、プログラム可能な完成でこれを行うことができます。

おすすめ記事