LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2009, 11:01 PM   #1
hateseven
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
Question 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!!
 
Old 02-24-2009, 01:57 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
echo ${dir//\//.}

This will replace all the slashes in $dir with periods. See here for more info.
 
Old 02-24-2009, 04:51 PM   #3
hateseven
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the reply David!

Quote:
Originally Posted by David the H. View Post

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!
 
Old 02-24-2009, 05:16 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 02-24-2009, 06:12 PM   #5
hateseven
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 02-24-2009, 07:44 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by hateseven View Post
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 ......
 
Old 02-24-2009, 09:34 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
removing file with special characters rsashok Linux - General 4 02-09-2010 04:08 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
bash replacing characters within a variable baks Programming 5 03-19-2007 12:33 AM
Python - Removing first x characters Mithrilhall Programming 3 01-16-2007 09:52 AM
Removing Characters when outputting to a script thedarkdestroyer Linux - Newbie 3 03-21-2006 03:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration