LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   CentOS (https://www.linuxquestions.org/questions/centos-111/)
-   -   shell script - archive tar script (https://www.linuxquestions.org/questions/centos-111/shell-script-archive-tar-script-4175595392/)

robertkwild 12-14-2016 03:21 AM

shell script - archive tar script
 
hi all,

made a script and thought i would show you guys about it,


Code:

#!/bin/bash

        echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
        read method

        case $method in
                1)
                        echo "please specify full path to tar file"
                        read -e tar

                        base=$(basename "$tar")

                        echo "please enter ID number ie ID1234"
                        read id

                        echo "please specify where you want the tar file to be stored"
                        read -e dest

                        if ! mv "$tar" "$id"_"$base" ; then
                                echo "something went wrong with the mv command, please do manually"
                                exit
                        fi

                        if ! rsync -avh "$id"_"$base" "$dest" ; then
                                echo "something went wrong with the rsync command, please do manually"
                                exit
                        fi

                        if ! rm -f "$id"_"$base" ; then
                                echo "something went wrong with the rm -f command, please do manually"
                                exit
                        fi
                               
                        ;;

                2)
                        echo "please specify full path to directory you want to be made into a tar"
                        read -e dir

                        cd $dir
                        cd ..

                        base=$(basename "$dir")

                        echo "please enter ID number ie ID1234"
                        read id

                        echo "please specify where you want the tar file to be stored"
                        read -e dest

                        if ! tar -cf "$id"_"$base".tar "$base" ; then
                                echo "something went wrong with the tar, please do manually"
                                exit
                        fi

                        if ! rsync -avh "$id"_"$base".tar "$dest" ; then
                                echo "something went wrong with the rsync, please do manually"
                                exit
                        fi

                        if ! rm -f "$id"_"$base".tar ; then
                                echo "something went wrong with the rm -f command, please do manually"
                                exit
                        fi

                        if ! rm -rf "$dir" ; then
                                echo "something went wrong with the rm -rf command, please do manually"
                                exit
                        fi

                        ;;

                *)
                        echo "invalid selection, please re-run the script"
                        exit

                        ;;

        esac


pan64 12-14-2016 07:32 AM

in general:
usually we send error messages to stderr instead of stdout
usually the exit code is 0 when everything is ok and non-zero in case of any error.


All times are GMT -5. The time now is 06:03 AM.