Register
It is currently Wed Dec 13, 2017 12:52 pm

changing script to work with freeBSD


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon May 22, 2017 8:07 am   

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

i have made my machine a freeBSD machine instead of a centos machine and my script doesnt work anymore and i was wondering if anyone can help me into why, i thought the commands are the same, here it is -

Code:
#!/bin/bash

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

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

   
      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 ""$source"/"$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


many thanks,

rob


Top
 Profile  
 PostPosted: Mon May 22, 2017 8:03 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
hi,

what's the point of having a case/esac when a simple if/else/fi would have done the job ?

btw, the first case is missing.

"not working" doesn't describe anything. what are the symptoms ?


Top
 Profile  
 PostPosted: Tue May 23, 2017 12:57 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
i had to change

Code:
#!/bin/bash



to

Code:
#!/bin/sh



now im getting stuck on this command

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



but when i press tab it should auto complete a directory it no longer auto completes and i dont know why?

the reason why i put a case in there was because i have an option t but i have deleted that out as i dont really use the option t anymore but i have still kept it


Top
 Profile  
 PostPosted: Tue May 23, 2017 2:24 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
don't expect /bin/sh to behave like bash.
on linux /bin/sh is more and more a link to dash; I don't know freeBSD, but it seems to use a more POSIX shell than bash.

-e is not POSIX.

can you use another shell (ksh, zsh..) that would have implemented this feature ?


Top
 Profile  
 PostPosted: Tue May 23, 2017 2:22 pm   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
good news, i did the following

pkg install bash

then to run the script

bash /path/to/script/script.name.sh

so appending the words bash infront of the script works


Top
 Profile  
 PostPosted: Tue Jun 06, 2017 1:52 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 273
Executing it in bash is definitely the simplest solution :)

One of the things that this forum, and other places like it, has taught me is to try to develop everything in plan old 'sh' for maximum portability! I do still get lazy though, especially for work, where I'm reasonably certain that my scripts will only ever be executed in the same environment...


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 16 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