Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Text File Entries 2

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

On a Solaris 8 system using a Bourne shell script I need to create and populate a small text file. I know I could do the below but there are too many lines (50-ish) for this to be manageable.

Code:
echo "Line 1" >  myfile.txt
echo "Line 2" >> myfile.txt

I also thought about doing a HERE type variable using vi but I am not sure how to pass an Escape to vi dynamically. This would be great if I could do this. :)
Code:
vi myfile.txt << VI_SESSION
i
Line 1
Line 2
[COLOR=blue]<< Escape and save here??? >>[/color]
VI_SESSION


Any suggestions?

Thanks,

Michael42
 
Hi Michael,

you are trying much too complicated routes.
Just use cat, don't worry about escapes:
Code:
cat > myfile.txt << SESSION
Line 1
Line 2
SESSION
regards
 
Thanks very much guys. I was making this way to hard. :)

-Michael42
 
One more related item please.

In using the cat method how can I ensure any text that uses a dollar sign ($) is not interpreted as a variable within my cat session?

Example issue:
Code:
cat > myfile.txt << SESSION
Line 1
Line 2
Line 3 MYVAR1=abc
Line 4 MYVAR2=$MYVAR1/dirx
SESSION

I want the line to stay "MYVAR2=$MYVAR1/dirx" and not replace the value of $MYVAR2 with the variable value of MYVAR1.

Thanks again guys,

Michael42
 
Why not simply this ?
echo 'Line 1
Line 2
Line 3 MYVAR1=abc
Line 4 MYVAR2=$MYVAR1/dirx' > myfile.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
feherke,

Brilliant. Thanks sir.


PHV,

>> Why not simply this ?

Usually this would be fine. In my case it would be too tedious to maintain. The "cat" method for this instance is PERFECT.

Thanks again guys,

Michael42

 
How is ONE echo (writing directly to the target) is more tedious to maintain than a cat << (forcing the shell to create a temporary file) ?
 
Michael42, you don't want to answer my question ?
 
PHV,

When I started UNIX scripting I used echo "my text" >> myfile.txt for everything. I thought it was better than sliced bread. When I discovered HERE (also called SESSION) variables it changed my coding style. To each his own. :) BTW, I am not trying to convince you of anything. You have helped me MANY times.

I just like this style:

SQLPLUS Example 1
Code:
$ORACLE_HOME/bin/sqlplus << SQLPLUS_SESSION
set pagesize 5000
set linesize 120
SELECT * FROM mytable ORDER BY location;
exit
SQLPLUS_SESSION

FTP Example 1
Code:
ftp hostx << FTP_SESSION
bin
hash
cd /dir1/dir3
put myfile
bye
FTP_SESSION

To me the above is easier to maintain than the below as I just change what is in between the SESSION variables. These are small "session scripts". Imagine big yucky ones.:

SQLPLUS Example 2
Code:
set pagesize 5000 > /tmp/myscript.sql
set linesize 120 >> /tmp/myscript.sql
SELECT * FROM mytable ORDER BY location; >> /tmp/myscript.sql
exit >> /tmp/myscript.sql

$ORACLE_HOME/bin/sqlplus joe/test@/tmp/myscript.sql

FTP Example 2 (I know you can use native ftp scripts - this is just an example\alternative)
Code:
bin > myxfer.ftp
hash >> myxfer.ftp
cd /dir1/dir3 >> myxfer.ftp
put myfile >> myxfer.ftp
bye >> myxfer.ftp

ftp hostx @ myxfer.ftp

Happy Coding,

Michael42
 
I don't think you read PHV's suggestion.

He asked the maintenance difference between:
Code:
cat > file << EOF
[COLOR=red]Line of text 1
Line of text 2
Line of text 3
Line of text 4
Line of text 5[/color]
EOF
and
Code:
echo '\
[COLOR=red]Line of text 1
Line of text 2
Line of text 3
Line of text 4
Line of text 5[/color]' > file

The answer is that there is no maintenance difference, but there may be a performance difference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top