Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   Bash script problem (http://www.linux-archive.org/debian-user/658248-bash-script-problem.html)

Soare Catalin 04-21-2012 08:07 AM

Bash script problem
 
Hello fellow Linux supporters!


I apologise if this specific thread is off topic to this mailing list.


I've been having problems with a backup script and am not sure how to make this work.

So far, Mr. Google hasn't helped me much -- maybe my search terms have been as dumb as I'm feeling right now.


The script will take files or dirs as parameters and will back them up in a presefined location, using tar. Problems arise when it will encounter files or directories which contain spaces in their names.

Would anyone be kind enough to tell me where I went wrong with my approach?


Script is below:




#!/bin/bash


#Init stuff DATETIME=`date +%Y_%m_%d.%H_%M` BK_LOCATION="/mnt/work/backup/"

BK_LIST=""


#Test parameters. If dirs and files do exist, add them to a list # that will be used as parameters for the tar command.

for PARAM in "$@";

do

if [ -d "$PARAM" ];

then #is it an existing directory?

BK_LIST="$BK_LIST ${PARAM}"

else

if [ -f "$PARAM" ]; then #is it an existing file?

BK_LIST="$BK_LIST ${PARAM}"

fi

fi

done


if [ ! -n "$BK_LIST" ];

then

exit 0

else #this else branch exists for debugging purposes

echo "You have chosen to backup: $BK_LIST"

fi


BK_FULLPATH="${BK_LOCATION} BACKUP_${DATETIME}.tar.bz2"


tar -cjf $BK_FULLPATH $BK_LIST




--Regards,

Sent from my Brick (TM)

emmanuel segura 04-21-2012 09:35 AM

Bash script problem
 
BK_FULLPATH="${BK_LOCATION}/BACKUP_${DATETIME}.tar.bz2"

tar -cjf "$BK_FULLPATH" "$BK_LIST"


Il giorno 21 aprile 2012 10:07, Soare Catalin <lolinux.soare@gmail.com> ha scritto:


Hello fellow Linux supporters!


I apologise if this specific thread is off topic to this mailing list.


I've been having problems with a backup script and am not sure how to make this work.

So far, Mr. Google hasn't helped me much -- maybe my search terms have been as dumb as I'm feeling right now.


The script will take files or dirs as parameters and will back them up in a presefined location, using tar. Problems arise when it will encounter files or directories which contain spaces in their names.

Would anyone be kind enough to tell me where I went wrong with my approach?


Script is below:




#!/bin/bash


#Init stuff DATETIME=`date +%Y_%m_%d.%H_%M` BK_LOCATION="/mnt/work/backup/"

BK_LIST=""


#Test parameters. If dirs and files do exist, add them to a list # that will be used as parameters for the tar command.

for PARAM in "$@";

do

if [ -d "$PARAM" ];

then #is it an existing directory?

BK_LIST="$BK_LIST ${PARAM}"

else

if [ -f "$PARAM" ]; then #is it an existing file?

BK_LIST="$BK_LIST ${PARAM}"

fi

fi

done


if [ ! -n "$BK_LIST" ];

then

exit 0

else #this else branch exists for debugging purposes

echo "You have chosen to backup: $BK_LIST"

fi


BK_FULLPATH="${BK_LOCATION} BACKUP_${DATETIME}.tar.bz2"


tar -cjf $BK_FULLPATH $BK_LIST




--Regards,

Sent from my Brick (TM)



--
esta es mi vida e me la vivo hasta que dios quiera

emmanuel segura 04-21-2012 09:37 AM

Bash script problem
 
Scusa ho svagliato


BK_FULLPATH="${BK_LOCATION}BACKUP_${DATETIME}.tar. bz2"

tar -cjf "$BK_FULLPATH" "$BK_LIST"

Il giorno 21 aprile 2012 10:07, Soare Catalin <lolinux.soare@gmail.com> ha scritto:


Hello fellow Linux supporters!


I apologise if this specific thread is off topic to this mailing list.


I've been having problems with a backup script and am not sure how to make this work.

So far, Mr. Google hasn't helped me much -- maybe my search terms have been as dumb as I'm feeling right now.


The script will take files or dirs as parameters and will back them up in a presefined location, using tar. Problems arise when it will encounter files or directories which contain spaces in their names.

Would anyone be kind enough to tell me where I went wrong with my approach?


Script is below:




#!/bin/bash


#Init stuff DATETIME=`date +%Y_%m_%d.%H_%M` BK_LOCATION="/mnt/work/backup/"

BK_LIST=""


#Test parameters. If dirs and files do exist, add them to a list # that will be used as parameters for the tar command.

for PARAM in "$@";

do

if [ -d "$PARAM" ];

then #is it an existing directory?

BK_LIST="$BK_LIST ${PARAM}"

else

if [ -f "$PARAM" ]; then #is it an existing file?

BK_LIST="$BK_LIST ${PARAM}"

fi

fi

done


if [ ! -n "$BK_LIST" ];

then

exit 0

else #this else branch exists for debugging purposes

echo "You have chosen to backup: $BK_LIST"

fi


BK_FULLPATH="${BK_LOCATION} BACKUP_${DATETIME}.tar.bz2"


tar -cjf $BK_FULLPATH $BK_LIST




--Regards,

Sent from my Brick (TM)



--
esta es mi vida e me la vivo hasta que dios quiera

Cam Hutchison 04-22-2012 07:34 AM

Bash script problem
 
Soare Catalin <lolinux.soare@gmail.com> writes:

>The script will take files or dirs as parameters and will back them up in a
>presefined location, using tar. Problems arise when it will encounter files
>or directories which contain spaces in their names.

>then #is it an existing directory?
>BK_LIST="$BK_LIST ${PARAM}"

here...

>else
>if [ -f "$PARAM" ]; then #is it an existing file?
>BK_LIST="$BK_LIST ${PARAM}"

.... and here.

As you build up BK_LIST, you lose the ability to tell which spaces are
the ones you added, or were already in $PARAM. You end up treating all
spaces as word separators.

To fix this, you want to make BK_LIST an array:

BK_LIST=()
# or declare -a BK_LIST, but I prefer the former

Append to the array with +=

BK_LIST+="${PARAM}"

Expand the array, preserving spaces:

tar -cjf $BK_FULLPATH "${BK_LIST[@]}"



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 52a8.4f93b476.2139c@xionine.xdna.net">http://lists.debian.org/52a8.4f93b476.2139c@xionine.xdna.net

Dom 04-22-2012 07:25 PM

Bash script problem
 
On 22/04/12 08:34, Cam Hutchison wrote:

Soare Catalin<lolinux.soare@gmail.com> writes:


The script will take files or dirs as parameters and will back them up in a
presefined location, using tar. Problems arise when it will encounter files
or directories which contain spaces in their names.



then #is it an existing directory?
BK_LIST="$BK_LIST ${PARAM}"


here...


else
if [ -f "$PARAM" ]; then #is it an existing file?
BK_LIST="$BK_LIST ${PARAM}"


.... and here.

As you build up BK_LIST, you lose the ability to tell which spaces are
the ones you added, or were already in $PARAM. You end up treating all
spaces as word separators.

To fix this, you want to make BK_LIST an array:

BK_LIST=()
# or declare -a BK_LIST, but I prefer the former

Append to the array with +=

BK_LIST+="${PARAM}"

Expand the array, preserving spaces:

tar -cjf $BK_FULLPATH "${BK_LIST[@]}"




Or just quote the entries like

BK_LIST="$BK_LIST "${PARAM}""

(apologies if thi mail goes through twice, I got a bounce on my earlier
attempt)


--
Dom


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4F945B2C.3090307@rpdom.net">http://lists.debian.org/4F945B2C.3090307@rpdom.net

Soare Catalin 04-22-2012 09:02 PM

Bash script problem
 
On Sun, Apr 22, 2012 at 10:25 PM, Dom <toyer@rpdom.net> wrote:

On 22/04/12 08:34, Cam Hutchison wrote:


Soare Catalin<lolinux.soare@gmail.com> *writes:




The script will take files or dirs as parameters and will back them up in a

presefined location, using tar. Problems arise when it will encounter files

or directories which contain spaces in their names.





then #is it an existing directory?

BK_LIST="$BK_LIST ${PARAM}"




here...




else

if [ -f "$PARAM" ]; then #is it an existing file?

BK_LIST="$BK_LIST ${PARAM}"




.... and here.



As you build up BK_LIST, you lose the ability to tell which spaces are

the ones you added, or were already in $PARAM. You end up treating all

spaces as word separators.



To fix this, you want to make BK_LIST an array:



BK_LIST=()

# or declare -a BK_LIST, but I prefer the former



Append to the array with +=



* BK_LIST+="${PARAM}"



Expand the array, preserving spaces:



* tar -cjf $BK_FULLPATH "${BK_LIST[@]}"








Or just quote the entries like



BK_LIST="$BK_LIST "${PARAM}""



(apologies if thi mail goes through twice, I got a bounce on my earlier attempt)



--

Dom





--

To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


Archive: http://lists.debian.org/4F945B2C.3090307@rpdom.net




Thank you everyone for replying, but unfortunately, nothing seems to work for the moment, although all the answers appear to make sense.
First, the array solution appears to work, but when tar gets all the parameters, they become a long string without spaces :), obviously "cannot stat: No such file or directory".


I then tried to add a space char at the BK_LIST increment line, but that didn't work. Enclosing them in quotes also failed.


Also tried Dom's suggestion (thank you Dom!) but then, tar won't be able to stat any file! (and this I actually can't explain to myself why)* :)


So again, thank you very much for your responses, but the only thing in my mind for the moment is that I really have to go back to the abs-guide (or perhaps find another one?) -- bottom line is that there's so many things I've forgot/not used for anything else than to follow a tutorial...

--
Regards,
Catalin Soare

Dom 04-23-2012 03:28 AM

Bash script problem
 
On 22/04/12 22:02, Soare Catalin wrote:

On Sun, Apr 22, 2012 at 10:25 PM, Dom<toyer@rpdom.net> wrote:


On 22/04/12 08:34, Cam Hutchison wrote:


Soare Catalin<lolinux.soare@gmail.**com<lolinux.soare@gm ail.com>>
writes:

The script will take files or dirs as parameters and will back them up

in a
presefined location, using tar. Problems arise when it will encounter
files
or directories which contain spaces in their names.





So again, thank you very much for your responses, but the only thing in my
mind for the moment is that I really have to go back to the abs-guide (or
perhaps find another one?) -- bottom line is that there's so many things
I've forgot/not used for anything else than to follow a tutorial...



Have you considered writing the file/directory names out to a temporary
file and using that for tar input?


The list file should be initialised first with

>${TMPDIR}/BK_LISTFILE

Change the BK_LIST="$BK_LIST ${PARAM}" lines to:

echo "${PARAM}" >> ${TMPDIR}/BK_LISTFILE

and the tar line to:

tar -cjf $BK_FULLPATH -T ${TMPDIR}/BK_LISTFILE

Finally tidy up with:

rm ${TMPDIR}/BK_LISTFILE

No need to worry about any quoting or special characters in names this way.

(The tempfile command is very useful for creating temporary files for
this sort of thing)

--
Dom


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4F94CC6D.1010407@rpdom.net">http://lists.debian.org/4F94CC6D.1010407@rpdom.net

Iuri Guilherme dos Santos Martins 04-23-2012 08:40 PM

Bash script problem
 
When dealing with paths in bash i usually employ two things:

One is declaring arrays like this:

FILES_LIST=( )

And everytime I want to append to the array I go like this:

FILES_LIST=( ${FILES_LIST[@]} ${NEW_FILE} )

Obviously I will have problems if the paths or files have spaces in
their names, so in debian I would have to change the standart file
separator character. This is an environment variable so I need it back
the way it was after my script run so when I change it I make a variable
to backup and I ensure that even if my scripts crash the old file
separator variable will return as it was.


So it's like this:

IFS_DEFAULT=${IFS} #this is the environment variable
IFS_NEW=$(echo -en "
") #this is a variable that makes the spaces on
filenames be treated the way I want them to



Everytime I need to change the variable (because on the same script I
need to fall back to space separators) I just put the IFS=${IFS_NEW} (no
need to export in this case) and right after the command I fallback with
IFS=${IFS_DEFAULT}.



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4F95BE36.9070604@gmail.com">http://lists.debian.org/4F95BE36.9070604@gmail.com

"Dan B." 04-24-2012 01:01 AM

Bash script problem
 
Iuri Guilherme dos Santos Martins wrote:

When dealing with paths in bash i usually employ two things:

One is declaring arrays like this:

FILES_LIST=( )

And everytime I want to append to the array I go like this:

FILES_LIST=( ${FILES_LIST[@]} ${NEW_FILE} )

Obviously I will have problems if the paths or files have spaces in
their names, ...


Does this work?

FILES_LIST=( "${FILES_LIST[@]}" "${NEW_FILE}" )

(Read the part of the bash manual page that describes how ...[*]
and ...[@] act when inside double-quoted strings.)


Daniel


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4F95FB6B.8020403@kempt.net">http://lists.debian.org/4F95FB6B.8020403@kempt.net

Cam Hutchison 04-24-2012 01:18 AM

Bash script problem
 
Soare Catalin <lolinux.soare@gmail.com> writes:

>Thank you everyone for replying, but unfortunately, nothing seems to work
>for the moment, although all the answers appear to make sense.
>First, the array solution appears to work, but when tar gets all the
>parameters, they become a long string without spaces :), obviously "cannot
>stat: No such file or directory".

Please post the code you have ended up with. The array method I posted
will work, but you may be using it incorrectly. From the bash man page:

If the word is double-quoted, ${name[*]} expands to a single word
with the value of each array member separated by the first character
of the IFS special variable, and ${name[@]} expands each element of
name to a separate word.

That is, the literal "${name[@]}" expands to each element of the array
"name" as a separate word. Not one long word as you have experienced.

I've used this method plenty of times in the past, so I know it does work.
What version of bash are you using? (bash --version)


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 7c06.4f95ff7c.e79c6@xionine.xdna.net">http://lists.debian.org/7c06.4f95ff7c.e79c6@xionine.xdna.net


All times are GMT. The time now is 04:44 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.