Operating System - HP-UX
1752782 Members
6228 Online
108789 Solutions
New Discussion юеВ

Using echo to create a script

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Using echo to create a script

Hello all,

I want to be able to create a script on the fly from another script by echoing lines into a file, but am running into difficulty, as it isn't working right. What am I doing wrong?

[CODE]
echo "for i in `grep $FRAME /root_home/powermt.sort.fil |awk '{print $7}'`" > pvtimout_set.sh
echo "do" >> pvtimout_set.sh
echo "echo ${i} ----------" >> pvtimout_set.sh
echo "echo pvchange -t 90 /dev/dsk/${i}" >> pvtimout_set.sh
echo "pvchange -t 90 /dev/dsk/${i}" >> pvtimout_set.sh
echo >> pvtimout_set.sh
echo "echo pvdisplay /dev/dsk/${i} |grep Timeout" >> pvtimout_set.sh
echo "pvdisplay /dev/dsk/${i} |grep Timeout" >> pvtimout_set.sh
echo >> pvtimout_set.sh
echo "done 2> /root_home/scripts/pvtimout.out 1>&2" >> pvtimout_set.sh
[/CODE]

Thanks for any help in advance!
11 REPLIES 11
Mel Burslan
Honored Contributor

Re: Using echo to create a script

any special character you want to echo into this script is getting interpreted by shell and resulting value is getting placed into the pvtimeout_set.sh

those characters, including but not limited to, $,>,<,{,},'," any other I might not have caught, will need to be escaped, by preceeding them with a backslash character.

Hope this helps
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Using echo to create a script

Hi Patrick:

IF you add a 'set -x' to this code you will see that evaluation of statements like "`grep $FRAME...`" is occuring and hence your script is hanging.

A much easier, clearer way to do this is with a HERE-DOC like this:

# cat .mymaker
#!/usr/bin/sh
cat <<-EOF! > pvtimeout.sh
for i in \`grep $FRAME /root_home/powermt.sort.fil |awk '{print $7}'\`
do
echo ${i} ----------
pvchange -t 90 /dev/dsk/${i}
pvchange -t 90 /dev/dsk/${i}
echo pvdisplay /dev/dsk/${i} |grep Timeout
pvdisplay /dev/dsk/${i} |grep Timeout
done
EOF!

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor
Solution

Re: Using echo to create a script

Hi (again) Patrick:

Ooops, I neglected to escape (backslash) things like "$" :

# cat ./mymaker
#!/usr/bin/sh
cat <<-EOF! > pvtimeout.sh
for i in \`grep \$FRAME /root_home/powermt.sort.fil |awk '{print \$7}'\`
do
echo \${i} ----------
pvchange -t 90 /dev/dsk/\${i}
pvchange -t 90 /dev/dsk/\${i}
echo pvdisplay /dev/dsk/\${i} |grep Timeout
pvdisplay /dev/dsk/\${i} |grep Timeout
done
EOF!

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Using echo to create a script

>JRF: I neglected to escape (backslash) things like "$":

If you are using "\" before EVERY "$", you can stop that by quoting the word:
cat <<-\EOF!
James R. Ferguson
Acclaimed Contributor

Re: Using echo to create a script

Hi (again):

> Dennis: If you are using "\" before EVERY "$", you can stop that by quoting the word:
cat <<-\EOF!

This is a bit different from Perl and I didn't really _read_ the manpage sentence that says, "If any character of word is quoted...". You make that clear --- thanks for the pointer!

/* NO Points please for this note */

Regards!

...JRF...

Arturo Galbiati
Esteemed Contributor

Re: Using echo to create a script

Hi Patrick,
you can use
echo
{
# begin script
your code
# end script
}>your script

all code within {} will be redirect to your script.

HTH,
Art
Patrick Ware_1
Super Advisor

Re: Using echo to create a script

Here is what I ended up using:

cat <<-EOF! > pvtimeout_set.sh
for i in \`grep ${FRAME} /tmp/powermt.sort.out |awk '{print \$7}'\`
do
echo \${i} ----------
echo pvchange -t ${TIMEOUT} /dev/dsk/\${i}
pvchange -t ${TIMEOUT} /dev/dsk/\${i}
echo pvdisplay /dev/dsk/\${i} |grep Timeout
pvdisplay /dev/dsk/\${i} |grep Timeout
echo
done
EOF!



Arturo,

That will pick up the echo statements I put into the script as well?
OldSchool
Honored Contributor

Re: Using echo to create a script

Dennis, thx for the quoting tip.

I wonder how many times I've overlooked that sentence?

no points pls
Dennis Handly
Acclaimed Contributor

Re: Using echo to create a script

>Here is what I ended up using:

You forgot a "\" for ${FRAME}. Note your echo piped to grep.

I would suggest you redo it as:
cat <<-\EOF!
for i in $(grep ${FRAME} /tmp/powermt.sort.out | awk '{print $7}'); do
echo "${i} ----------"
echo "pvchange -t ${TIMEOUT} /dev/dsk/${i}"
pvchange -t ${TIMEOUT} /dev/dsk/${i}
echo "pvdisplay /dev/dsk/${i} | grep Timeout"
pvdisplay /dev/dsk/${i} | grep Timeout
echo
done
EOF!

>That will pick up the echo statements I put into the script as well?

See my improvements.