LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   removing characters from variable? (https://www.linuxquestions.org/questions/programming-9/removing-characters-from-variable-707010/)

hateseven 02-23-2009 11:01 PM

removing characters from variable?
 
Hello,

I have the following script that I'm trying to use to backup specific directories on my box to my USB drive:

Code:

for dir in /etc /var /home /usr/local /opt
do if [ -d $dir ]
then find $dir | cpio -ovH crc -C65536 -O /mnt/USBDrive/backup.$dir.$(date +%m_%d_%Y_%k_%m_%S).cpio
fi
done

(props to 98_1LE for getting me started on this code from this thread (links to forums at unix.com)).

Resulting in several .cpio files (or one large one, I'm open to change here) with a filename of "backup.<directoryname>.<timestamp>.cpio .

Now obviously this doesn't work because the variable $dir has /'s in it, making getting a nice error from cpio:

Code:

cpio: /mnt/USBDrive/backup./etc.02_23_2009_22_02_08.cpio: No such file or directory
cpio: /mnt/USBDrive/backup./var.02_23_2009_22_02_08.cpio: No such file or directory
cpio: /mnt/USBDrive/backup./home.02_23_2009_22_02_08.cpio: No such file or directory
cpio: /mnt/USBDrive/backup./usr/local.02_23_2009_22_02_08.cpio: No such file or directory
cpio: /mnt/USBDrive/backup./opt.02_23_2009_22_02_08.cpio: No such file or directory

I was thinking there may be a way to put in some sort of command to remove the /'s from the variable in between the find and the cpio commands, leaving a readable filename.

Anyone have any ideas/suggestions to how I can get this working?

Please be gentle, Ive been using linux for about 2 years now and LOVE it, but I'm still green about a few things!! This site has helped me a TON in the past, and I'd never actually had a need to post anything myself, until now. :)

Thanks in advance!!

David the H. 02-24-2009 01:57 AM

echo ${dir//\//.}

This will replace all the slashes in $dir with periods. See here for more info.

hateseven 02-24-2009 04:51 PM

Thanks for the reply David!

Quote:

Originally Posted by David the H. (Post 3455318)

Code:

echo ${dir//\//.}

I tried just running a test, before adding in the time consuming find and cpio:

Code:

for dir in /etc /var /home /usr/local /opt
do if [ -d $dir ]
then echo ${dir//\//.}
fi
done

but the output is still just

/etc
/var
/home
/usr/local
/opt


Any further suggestions?


Thanks again!

David the H. 02-24-2009 05:16 PM

Hmm. It works perfectly for me. Are you not using bash perchance?

You could try something like this then:
Code:

for dir in /etc /var /home /usr/local /opt
do if [ -d $dir ]
then

dir2=$(echo $dir|sed "s|/|.|g)
echo $dir2

fi
done


hateseven 02-24-2009 06:12 PM

Thanks again!

Ah, that last one worked!
I'm using bash:

Code:

[/mnt/USBDrive]> echo $SHELL
/bin/bash

Here's the script I ended up using:

Code:

for dir in /etc /var /home /usr/local /opt
do if [ -d $dir ]
then
dir2=$(echo $dir|sed "s|/|.|g")
find $dir -mount -depth | cpio -ovH crc -C65536 -O /mnt/USBDrive/backup$dir2.$(date +%m_%d_%Y_%k_%m_%S).cpio
fi
done


I'm just getting into understanding awk and sed. Do you have any suggestions for some good online references that could help with comprehension of each?

Thank you so much! :)

--h87

ghostdog74 02-24-2009 07:44 PM

Quote:

Originally Posted by hateseven (Post 3456209)
Code:

for dir in /etc /var /home /usr/local /opt
do if [ -d $dir ]
then
dir2=$(echo $dir|sed "s|/|.|g")
find $dir -mount -depth | cpio -ovH crc -C65536 -O /mnt/USBDrive/backup$dir2.$(date +%m_%d_%Y_%k_%m_%S).cpio
fi
done


for loop is unnecessary
Code:

find /etc /var/ home /usr/local /opt -type d ......

David the H. 02-24-2009 09:34 PM

Good to see it's working. But I'm still puzzled as to why the parameter substitution didn't work for you. I copied your test script exactly and it did just what it was supposed to. Substitution is generally preferable if you can get it to work because you don't have to call an external command for it.

This page has detailed tutorials for sed and awk, among other useful subjects, though truthfully I haven't gotten around to going through them myself yet. The Advanced Bash Scripting Guide I linked to earlier also has some coverage of them.


All times are GMT -5. The time now is 02:25 PM.