1833934 Members
1696 Online
110063 Solutions
New Discussion

puzzle

 
SOLVED
Go to solution
U.SivaKumar_2
Honored Contributor

puzzle

Hi,
I have three files a.txt b.txt c.txt and a shell script run.sh .
The file c.txt is empty. My requirement - When
I run run.sh once the cotents of file a.txt should be in c.txt. And If run run.sh second time the contents of b.txt should be in c.txt.
The order of file content of c.txt should be in round robin (a.txt->c.txt,b.txt->c.txt,a.txt->c.txt.....so on) every time I execute the run.sh.

So what can be the shell script for run.sh ?.


regards,
U.SivaKumar
Innovations are made when conventions are broken
12 REPLIES 12
John Palmer
Honored Contributor
Solution

Re: puzzle

Something like this...

#!/usr/bin/sh

if diff b.txt c.txt 2>/dev/null
then cp a.txt c.txt
else cp b.txt c.txt
fi

exit

If b.txt is the same as c.txt then a.txt->c.txt otherwise b.txt->c.txt.

Regards,
John
U.SivaKumar_2
Honored Contributor

Re: puzzle

Hi,
Well done Sir.
What happens if my a.txt and b.txt are of same size ?.

regards,
U.SivaKumar
Innovations are made when conventions are broken
Donald Kok
Respected Contributor

Re: puzzle

Hi,
I would maintain a checkfile which only holds the last copied file. Then in the script you can check on this content.

Another option is to change a bit on the c.txt file. Like the sticky bit or the x-bit. And in the script check on this bit.

Good luck
Donald
My systems are 100% Murphy Compliant. Guaranteed!!!
Dietmar Konermann
Honored Contributor

Re: puzzle

Hi!

You need to store the laste state somewhere... or you could use diff to get the last cp state (would be inefficient with large files!).

Idea: hold a state file.

#!/usr/bin/sh

if [[ "$(cat nextfile 2>/dev/null)" = b.txt ]]; then
cp b.txt c.txt
echo a.txt >nextfile
else
cp a.txt c.txt
echo b.txt >nextfile
fi
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Simon Abbott
Frequent Advisor

Re: puzzle

Hello,

For your script to know which file to write from, it will have to know which file was written from last time. I would set this in a variable or a file. If the script needs to work for differnent users or sessions it would have to be in a file.

Therefore I'd do somthing like this:

if [ ! -f last_written_from ] ; then
echo b > last_written_from
fi

if [ "$(cat last_written_from)" = "b" ] ; then
cp a.txt c.txt
echo a > last_written_from
else
cp b.txt c.txt
echo b > last_written_from
fi
I'm still working on that one
Carlos Fernandez Riera
Honored Contributor

Re: puzzle


if [ -s c.txt ]; then
echo "CHANGING"
file=$(ls -1rt a.txt b.txt|head -1)
cat $file >c.txt
touch $file
else
cat a.txt >c.txt
touch a.txt
fi
sleep 1 #
unsupported
H.Merijn Brand (procura
Honored Contributor

Re: puzzle

1. cmp is faster than diff
2. use MD5 or sum + timestamp to identify which file is in c.txt
3. use tar or cpio instead of cp to maintan access and modification times, so you can use these to check
4. use a stampfile in which you state what the next file will be (or what the last file was)
5. use symlinks :)
Enjoy, Have FUN! H.Merijn
Trond Haugen
Honored Contributor

Re: puzzle

King Johns answer is short and beautiful.
diff compares the contents of the files.

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Jordan Bean
Honored Contributor

Re: puzzle

If it isn't necessary to copy the contents of the files, why not alternate links?

#!/usr/bin/sh
if [ -e a.txt -a -e b.txt ]
then
if [ a.txt -ef c.txt ]
then
ln -f b.txt c.txt
else
ln -f a.txt c.txt
fi
fi
Yogeeraj_1
Honored Contributor

Re: puzzle

hi,


if diff b.txt c.txt 2>/dev/null
then cp a.txt c.txt
else cp b.txt c.txt
fi


this is beautiful scripting indeed ;)

Thanks King John.

Best Regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
John Palmer
Honored Contributor

Re: puzzle

Actually, on further reflection, using cmp -s is more efficient. I sort of assumed from the original post that files called ?.txt weren't likely to be huge.

if cmp -s b.txt c.txt
then cp a.txt c.txt
else cp b.txt c.txt
fi

Linking the files may be OK but if there is any intention to alter the contents of c.txt then the linked original file gets changed as well.

Regards,
John
H.Merijn Brand (procura
Honored Contributor

Re: puzzle

Don't check content at all :)

l1:/u/usr/merijn 126 > perl -we '$a=(readlink($c="c.txt"))=~/^a/?"b":"a";unlink$c;symlink"$a.txt",$c' ; ll *.txt
1363 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:48 a.txt
1420 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:49 b.txt
1423 lrwxrwxrwx 1 merijn softwr 5 Aug 14 11:10 c.txt -> a.txt
l1:/u/usr/merijn 127 > perl -we '$a=(readlink($c="c.txt"))=~/^a/?"b":"a";unlink$c;symlink"$a.txt",$c' ; ll *.txt
1363 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:48 a.txt
1420 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:49 b.txt
1423 lrwxrwxrwx 1 merijn softwr 5 Aug 14 11:10 c.txt -> b.txt
l1:/u/usr/merijn 128 > perl -we '$a=(readlink($c="c.txt"))=~/^a/?"b":"a";unlink$c;symlink"$a.txt",$c' ; ll *.txt
1363 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:48 a.txt
1420 -rw-rw-rw- 1 merijn softwr 64 Aug 14 10:49 b.txt
1423 lrwxrwxrwx 1 merijn softwr 5 Aug 14 11:10 c.txt -> a.txt
l1:/u/usr/merijn 129 >
Enjoy, Have FUN! H.Merijn