Efficient Text File Writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Write a template main.c file via shell script to make it easier for yourself later.
    The issue here isn't writing the file though, I can do that with echo statements.
    What I'd like to see is how to do this in a less redundant way? Note down in the attempts at solution, the amount of echos and >> main$ext is completely unnecessary.
    So I'd like to see an alternative, more "efficient for the programmer", way of doing it.
    Is there any way to work with a block of text within the shell script?

Edit: I'd also like to avoid all one echo statement with a bunch of "\n" characters, that would ALSO be redundant in my opinion.

  1. Relevant commands, code, scripts, algorithms:
    ext=".c"
    proj="proj"
    the echo statement
    and >> the append redirection operator

  2. The attempts at a solution (include all code and scripts):

#! /bin/bash
#  main.c
ext=".c"
read proj
echo "//$auth" > main$ext
echo "//main.$ext" >> main$ext
echo "//$proj main file" >> main$ext
echo "" >> main$ext
echo "#include <iostream>" >> main$ext
echo "#include <cstdlib>" >> main$ext
echo "" >> main$ext
echo "#include \".h\"" >> main$ext
echo "" >> main$ext
echo "" >> main$ext

The finished product looks akin to

//$auth
//main.c
//$proj main file/implementation

#include <iostream>
#include <cstdlib>

#include ".h"
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of California Davis, Davis, CA, USA, Sean Davis, ECS40
    (This is extra work outside of class though so it really has nothing to do with school. I just like shell scripting lately)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Hi this could be done with a so-called "here document":

cat > file << EOF
//$auth
...
EOF

See man bash

Scrutinizer is correct, but as there are always several ways in Unix to do something, you could do it with the exec too, if you use ksh (note that this won't work in bash, because bash doesn't have the print command):

exec 3> /some/output/file   # open the file as I/O channel 3 and clear
                            # its contents if it already exists

print -u3 "some text"       # print some text to channel 3
print -u3 "something else"  # same
...

exec 3>&-                   # close I/O-channel 3

This way you won't have to take care of the first ">" and subsequent ">>" redirections (if you mistakenly use ">" it will clear the file) and you will not have to repeat the output files name over and over again.

You could even influence the content of your output file with some program logic in the script - something the here-document provides little support for. For instance, the following would not be possible with a here-doc:

exec 3> /some/output/file

print -u3 "first line"
print -u3 "second line"

if [ <some_criteria> ] ; then
     print -u3 "----Optional line------"
fi
print -u3 "third line"

exec 3>&-

In exchange for helping you I'd like to ask you to learn about I/O channels (file descriptors) and redirection in Unix and how to use these. You might want to read this thread as a starter.

I hope this helps.

bakunin

1 Like