Click to See Complete Forum and Search --> : Script challenge time.
slapNUT
11-05-2002, 08:01 PM
I was writitng a script to do some backups and create the restore script on the fly.
I wanted the script to make the below command appear in the restore script
"verbatim." The problem is the final two single quotes on the sed expression did not
appear in the file.
This is what I wanted to be put in the file:
DISKS=`grep ^DISKS=[0-9]* /fd0/restore.dat|sed 's/^.*=//'`
#!/bin/sh
echo 'echo DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed 's/^.*=//'`' >/testfile
The above script only produces this, notice the sed expression:
DISKS=`grep ^DISKS=[0-9]* /fd0/restore.dat|sed s/^.*=//`
So it took me two hours to make it work, I'm sorry to say.
A cookie to whomever figures it out first. I'll probably not look here again till
tomorrow.
BTW I originally did it in ksh but I'll let you use bash if you like.
bwkaz
11-05-2002, 08:53 PM
Escape your single quotes:
#!/bin/sh
echo 'echo DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed \'s/^.*=//\'`' >/testfile
;)
slapNUT
11-06-2002, 12:19 AM
Sorry that doesn't work in bash or ksh. I spent most of my 2 hours trying to find a way to escape the quotes.
$ cat test1
#!/bin/sh
echo 'echo DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed \'s/^.*=//\'`' >testfile
$ ./test1
./test1: line 2: unexpected EOF while looking for matching ``'
./test1: line 4: syntax error: unexpected end of file
NOTE: I removed the leading / on the testfile, that was a typo as there is no need to be writing testfiles in your root directory :D
<edit1> Weird... the Esc backslashes don't show up on my browser but they are here when I edit. I'll try putting code brackets around it to see if it helps.
<edit2> That didn't help. This is weird cause the Esc slashes are here when I edit. So just imagine that there are Esc slashes before each of the single quote's on the sed expression.
hop-frog
11-06-2002, 02:21 AM
echo \D\I\S\K\S\=\`\g\r\e\p\ \^\D\I\S\K\S\=\[\0\-\9\]\*\ \/\f\d\0\/\r\e\s\t\o\r\e\.\d\a\t\|\s\e\d\ \'\s\/\^\.\*\=\/\/\'\`
this work?
Rüpel
11-06-2002, 03:23 AM
ouch! that looks weird! :D
smokybobo
11-06-2002, 03:30 AM
This works
#!/bin/sh
echo "echo DISKS=\`grep ^DISKS=[0-9]* /fd0/restore|sed 's/^.*=//'\`" > testfile
How about:
echo 'echo DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed s/\^\.\*=//`' >/testfile
Or why bother with the external echo anyways:
DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed 's/^.*=//'`
echo "$DISKS" >testfile
Or even:
echo `grep ^DISKS=[0-9]* /fd0/restore|sed 's/^.*=//'` >testfile.
Perhaps:
grep ^DISKS=[0-9]* /fd0/restore|sed *s/^.*=//' >testfile
And xargs might also help somewhere. I haven't tested any just thinking here. So if they fail, i'm not to blame :D.
slapNUT
11-06-2002, 10:39 AM
hop-frog
echo \D\I\S\K\S\=\`\g\r\e\p\ \^\D\I\S\K\S\=\[\0\-\9\]\*\ \/\f\d\0\/\r\e\s\t\o\r\e\.\d\a\t\|\s\e\d\ '\s\/\^\.\*\=\/\/'\`
That gives me this:
DISKS=`grep ^DISKS=[0-9]* /fd0/restore.dat|sed \s\/\^\.\*\=\/\/`
smokybobo
echo "echo DISKS=\`grep ^DISKS=[0-9]* /fd0/restore|sed 's/^.*=//'\`" > testfile
Thats much better than what I had. You put it in double quotes and esc the backtics. I tried escaping everything I could and never did come up with that.
echo 'DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed '"'"s/^.*=//"'"'`' > testfile
I finally just started at the left side and quoted everything untill I made it work. :D
Actually the echo wasn't supposed to be part of it but that was another early morning typo on my part.
Like I promised here's your cookie. If you look real close you'll figure out how to login to rom-world.com to download games :cool:
www.rom-world.com FALSE FALSE 1037580938 usernameandpass22 accessanddownload
Here's a hint: sed 's/and/:/g'
slapNUT
11-06-2002, 10:52 AM
Hena
The first one gives me this:
echo DISKS=`grep ^DISKS=[0-9]* /fd0/restore|sed s/\^\.\*=//`
The second one gives me this error:
grep: /fd0/restore: No such file or directory
The third one is what I tried first which leaves off the two single quotes on the sed expression.
And the fourth one gives me this:
./t1: line 2: unexpected EOF while looking for matching `''
./t1: line 3: syntax error: unexpected end of file
But go ahead and take a cookie too. :p