Register
It is currently Thu Dec 14, 2017 6:27 pm

cant remove directory on bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Mar 24, 2017 11:51 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
hi all,

this is my script and as you can see in the screenshot i attach its not deleting the directory in the source folder

Code:
#!/bin/bash

cd /vol/cha-work/_ARCHIVE/to_be_archived/audio/robert_test
temp=/mnt/robert_test/temp
dest=/vol/cha-archive/audio

   echo "is this archive for an audio tar press (t) or an audio directory press (d)"
   read option

   case $option in

      d)

         echo "please specify full path to directory you want to be made into a tar"
         read -e 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

         cd "$dir"

         base=$(basename "$dir")

            echo -e "create "$base" into "$base".tar\n"
                                echo -e "move "$base".tar to "$dest"\n"
#                               echo -e "remove "$base".tar from "$dir"\n"
            echo -e "remove "$base" from "$dir"\n"

                                        echo "is this information correct, press (y) or press (n)"
                                        read correct

                                        case $correct in

                                                y)
                     echo "the script will now continue";;

                                                n)
                     echo "please re-run the script inputting correct details"
                                                        exit;;

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

                                        esac            

         date >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo "" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
#         echo -e ""$id"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e ""$dir"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e "how many files = `find . -type f | wc -l`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e "size of directory = `du -sh`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         ls -R >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         
         cd ..

         if ! cp -R "$base" "$temp" ; then
            echo "something went wrong rsync command, please do manually"
            exit
         fi

         cd "$temp"

         if ! tar -cf "$dest"/"$base".tar "$base" ; then
            echo "something went wrong creating the tar, please do manually"
            exit
         fi

         if ! rm -rf "$temp"/"$base" ; then
            echo "something went wrong with the rm -rf 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


where it says please enter the directory i have entered -

testdirtobearchived

and as you can see it creates the tar file fine and stores it in the destination folder but it doesnt remove the directory in robert_test

why is this?

many thanks

rob


You do not have the required permissions to view the files attached to this post.


Top
 Profile  
 PostPosted: Sat Mar 25, 2017 5:44 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
It can't delete a directory, if you give non existing pathes.

If you cd into temp, it's very unlikely that you find therein another temp dir.

Insert echo debug lines to see what's going on.


Top
 Profile  
 PostPosted: Tue Mar 28, 2017 8:22 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
sorry its been so long in replying back but sorted it

Code:
#!/bin/bash

source=/vol/cha-work/_ARCHIVE/to_be_archived/audio/robert_test
temp=/mnt/robert_test/temp
dest=/vol/cha-archive/audio

   echo "is this archive for an audio tar press (t) or an audio directory press (d)"
   read option

   case $option in

      t)
         cd "$source"

         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

#            echo -e "rename "$base" to "$id"_"$base"\n"
                             echo -e "move "$base" to "$dest"\n"
                           echo -e ""$base" will be removed from "$tar"\n"

               echo "is this information correct, press (yes) or press (no)"
               read correct
   
               case $correct in

                  yes)
                     echo "the script will now continue";;

                  no)
                     echo "please re-run the script inputting correct details"
                     exit;;

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

               esac

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

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

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

      d)
         cd "$source"

         echo "please specify full path to directory you want to be made into a tar"
         read -e 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

         cd "$dir"

         base=$(basename "$dir")

            echo -e "COPY "$base" to "$temp"\n"
                                echo -e "CREATE "$base".tar in "$temp"\n"
            echo -e "COPY "$base".tar to "$dest"\n"
            echo -e "REMOVE "$base".tar from "$temp" \n"
                                echo -e "REMOVE "$base" from "$temp"\n"
            echo -e "REMOVE "$base" from "$source"/"$dir"\n"

                                        echo "is this information correct, press (yes) or press (no)"
                                        read correct

                                        case $correct in

                                                yes)
                     echo "the script will now continue";;

                                                no)
                     echo "please re-run the script inputting correct details"
                                                        exit;;

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

                                        esac            

         date >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo "" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
#         echo -e ""$id"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e ""$dir"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e "how many files = `find . -type f | wc -l`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         echo -e "size of directory = `du -sh`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         ls -R >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
         
         cd ..

         if ! cp -R "$base" "$temp" ; then
            echo "something went wrong with the recursive copy command, please do manually"
            exit
         fi

         cd "$temp"

         chmod -R 777 "$temp"

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

         if ! cp "$base".tar "$dest" ; then
            echo "something went wrong copying the tar, please do manually"
            exit
         fi

         if ! rm -f "$temp"/"$base".tar ; then
            echo "something went wrong removing the tar, please do manually"
            exit
         fi


         if ! rm -rf "$temp"/"$base" ; then
            echo "something went wrong removing from temp, please do manually"
            exit
         fi

         cd "$source"/"$dir"
         cd ..

         if ! rm -rf "$base" ; then
            echo "something went wrong removing from source, please do manually"
            exit
         fi;;

      *)

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

   esac


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP