LinuxQuestions.org
Help answer threads with 0 replies.
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 03-22-2011, 05:26 PM   #1
elerox
LQ Newbie
 
Registered: Mar 2011
Posts: 4

Rep: Reputation: 0
shell - substition of variables


hello, i have following code :
Code:
TMPDIR=`mktemp -d`
mkdir $TMPDIR/old/
mkdir $TMPDIR/new/
tar -xf $1 -C $TMPDIR/old/
tar -xf $2 -C $TMPDIR/new/
tmpdirnew="$TMPDIR/new/"
tmpdirold="$TMPDIR/old/"
old_archive=`find $tmpdirold | sed -e 's/old/substitute/'  `
new_archive=`find $tmpdirnew | sed -e 's/new/substitute/'  `

for old_path in $old_archive ; do
   for new_path in $new_archive ; do
	if [ $old_path = $new_path ] ; then  old=`echo $old_path | sed -e 's/substitute/old/g'` ; new=`echo $new_path | sed -e 's/substitute/new/g'` ; old_new_diff=`cmp  $old $new`
		if [ old_new_diff != "" ] ; then echo $new_path
		fi
	fi
done
done
this script should extract two archives into /tmp/tmp.XXXX/old and /tmp/tmp.XXXX/new, and compare equal files. If they don't match, the script will print its adress, but it just print all files in the archive. Any ideas?
 
Old 03-22-2011, 07:59 PM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
Check the output of cmp. Maybe it is not returning the empty string as you supposed when files are equal.

An alternative script could use diff instead:
Code:
TMPDIR=`mktemp -d`
mkdir $TMPDIR/old/
mkdir $TMPDIR/new/
tar -xf $1 -C $TMPDIR/old/
tar -xf $2 -C $TMPDIR/new/
tmpdirnew="$TMPDIR/new/"
tmpdirold="$TMPDIR/old/"
diff -qr $tmpdirold $tmpdirnew
which would recursively compare the two directories and output only whether files differ.
 
Old 03-23-2011, 03:21 AM   #3
elerox
LQ Newbie
 
Registered: Mar 2011
Posts: 4

Original Poster
Rep: Reputation: 0
ok, i found a typo, there should be
if [ $old_new_diff != "" ] ; then echo $new_path
instead of
if [ old_new_diff != "" ] ; then echo $new_path

but here's another problem ... how to cut of the first part of adress? lets say that output of echo $new_path is
/tmp/tmp.OmujWm1931/new/hello/dir
and i want just /hello/dir

I tried
echo $new_path | sed "s/$tmpdirnew//"
but it says that : expression #1, char 8: unknown option to `s'

any ideas?

//edit, ok, i've done this too using regexes, but i'm still curious, why the sed above didn't work

Last edited by elerox; 03-23-2011 at 04:24 AM.
 
Old 03-23-2011, 05:04 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The $tmpdirnew will contain slahes, interfering with the intended ones. You can try:
Code:
echo $new_path | sed "s|$tmpdirnew||"
Your nested loop will contain many unmatched cases. What about using only one loop, and changing the paths inside from old to new. I mean:
Code:
for in OLDFILE /tmp/xyz/old/a /tmp/xyz/old/a; do
    NEWFILE=${OLDFILE/old/new}
    echo $NEWFILE
 done
 
  


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
shell variables thehollow89 Linux - General 20 03-22-2010 03:25 AM
help with shell variables/commands grndfl Linux - Newbie 2 07-25-2008 05:06 PM
Shell variables Milosevic Linux - Newbie 2 12-10-2005 10:32 PM
Sub shell environment variables tnine9 Programming 1 06-08-2004 01:27 PM
Shell Variables. ducka Programming 5 09-03-2003 05:38 PM

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

All times are GMT -5. The time now is 01:30 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