# Using echo to create a script

**URL:** <https://community.unix.com/t/using-echo-to-create-a-script/243142>\
**Category:** Shell Programming and Scripting\
**Created:** [June 23, 2009, 12:53pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142 "2009-06-23T12:53:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LinuxRacr](https://community.unix.com/letter_avatar/linuxracr/32/5_5575768a8748004e209b776fc1b2916d.png) [@LinuxRacr](https://community.unix.com/u/LinuxRacr)\
**Post date:** [June 23, 2009, 12:53pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/1 "2009-06-23T12:53:11Z")

</div>

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?

```nohighlight
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

```

Thanks for any help in advance!

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [June 23, 2009, 2:29pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/2 "2009-06-23T14:29:01Z")

</div>

> [@linuxracr](#):
>
> ...  
> What am I doing wrong?  
> ...

Escape those accent graves with a backslash character each.

tyler\_durden

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [June 23, 2009, 2:35pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/3 "2009-06-23T14:35:23Z")

</div>

The problem is caused by not escaping characters which will be interpreted by the shell (e.g. dollar and backtick ... and maybe more) where you want them to appear in the new script. It might be easier to write the script in the normal manner and call it with a parameter.

If you persist with this approach, test thoroughly before executing the new script.

The lines starting ... echo "echo ... need attention to stop the pipe becoming active in the new script because the echoed string will not be in quotes.

---

<div class="post-metadata">

**Author:** ![LinuxRacr](https://community.unix.com/letter_avatar/linuxracr/32/5_5575768a8748004e209b776fc1b2916d.png) [@LinuxRacr](https://community.unix.com/u/LinuxRacr)\
**Post date:** [June 23, 2009, 4:43pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/4 "2009-06-23T16:43:32Z")

</div>

Thanks! Here is what I did, based on some help I received:

```nohighlight
cat <<-EOF! > pvtimeout_set.sh
for i in \`grep ${FRAME} /tmp/powermt.sort.out |awk '{print \$7}'\`
do
echo \${i} ----------
echo 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
echo
done
EOF!

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [June 23, 2009, 5:01pm UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/5 "2009-06-23T17:01:23Z")

</div>

FYI:

```nohighlight
     << [-]word
           The shell input is read up to a line that is the same
           as word, or to an EOF. No parameter substitution, com-
           mand substitution, or file name generation is per-
           formed on word. The resulting document, called a
           here-document, becomes the standard input. If any
           character of word is quoted, no interpretation is
           placed upon the characters of the document. Otherwise,
           parameter and command substitution occur, \NEWLINE is
           ignored, and \ must be used to quote the characters \,
           $, `, and the first character of word. If - is
           appended to <<, then all leading tabs are stripped
           from word and from the document.

```

---

<div class="post-metadata">

**Author:** ![LinuxRacr](https://community.unix.com/letter_avatar/linuxracr/32/5_5575768a8748004e209b776fc1b2916d.png) [@LinuxRacr](https://community.unix.com/u/LinuxRacr)\
**Post date:** [June 29, 2009, 9:56am UTC](https://community.unix.com/t/using-echo-to-create-a-script/243142/6 "2009-06-29T09:56:17Z")

</div>

Thanks.
