Unix Shell - a script to create files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > a script to create files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author a script to create files
Gary Wessle

2006-11-26, 7:18 pm

Hi

I find my self doing the following task many times and I just thought
of automating it by writing a shell script which does it for me.

the task.

ask the user for a file name "f_name".
create 2 files, the first named f_name.h and the second f_name.cpp

in the first file place the following text
****************
#ifndef F_NAME_H
#efine F_NAME_H



#endif
****************

in the second file place

****************
#include "f_name.h"
****************


but if any of the files already exists, report which one exists and
ask for a different name. or "q" to quit with out doing anything.

Icarus Sparry

2006-11-26, 7:18 pm

On Mon, 27 Nov 2006 06:47:10 +1100, Gary Wessle wrote:

> Hi
>
> I find my self doing the following task many times and I just thought
> of automating it by writing a shell script which does it for me.
>
> the task.
>
> ask the user for a file name "f_name".
> create 2 files, the first named f_name.h and the second f_name.cpp
>
> in the first file place the following text
> ****************
> #ifndef F_NAME_H
> #efine F_NAME_H
>
>
>
> #endif
> ****************
>
> in the second file place
>
> ****************
> #include "f_name.h"
> ****************
>
>
> but if any of the files already exists, report which one exists and
> ask for a different name. or "q" to quit with out doing anything.


There are some parts of the problem you have left unspecified. For
instance do you want to be able to create the file in a different
directory?

The "q" to quit is not very unixy, EOF would be more normal.

Anyway, here goes, untested.

#!/bin/sh
while echo "Enter filename, or q to quit" ; read fn
do
if [ q = "${fn}" ] ; then exit 1 ; fi
basefn=${fn##*/}
ucbasefn=$(echo "$basefn" | tr '[a-z]' '[A-Z]')_H
if [ -e "${fn}.h" ]
then
echo "${fn}.h already exists"
elif [ -e "${fn}.cpp" ]
then
echo "${fn}.cpp already exists"
else
{
echo "#ifndef ${ucbasefn}"
echo "#define ${ucbasefn}"
echo
echo "#endif // ${ucbasefn}"
} > "${fn}.h"
echo "#include \"${basefn}.h\"" > ${fn}.cpp
break
fi
done
Chris F.A. Johnson

2006-11-26, 7:18 pm

On 2006-11-26, Gary Wessle wrote:
> Hi
>
> I find my self doing the following task many times and I just thought
> of automating it by writing a shell script which does it for me.
>
> the task.
>
> ask the user for a file name "f_name".
> create 2 files, the first named f_name.h and the second f_name.cpp
>
> in the first file place the following text
> ****************
> #ifndef F_NAME_H
> #efine F_NAME_H
>
>
>
> #endif
> ****************
>
> in the second file place
>
> ****************
> #include "f_name.h"
> ****************
>
>
> but if any of the files already exists, report which one exists and
> ask for a different name. or "q" to quit with out doing anything.


while :
do
printf "Enter file name: "
read fname
if [ -e "$fname.cpp" ]
then
printf "%s exists\n" "$fname.h" >&2
continue
elif [ -e "$fname.h" ]
then
printf "%s exists\n" "$fname.h" >&2
continue
else
printf "#ifndef %s_H\n#define %s_H\n\n\n#endif\n" \
"$fname" "$fname" > "$fname.cpp"
printf "#include %s.h\n" "$fname" > "$fname.h"
break
fi
done

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Icarus Sparry

2006-11-27, 7:22 am

On Sun, 26 Nov 2006 17:53:20 -0500, Chris F.A. Johnson wrote:

> On 2006-11-26, Gary Wessle wrote:
>
> while :
> do
> printf "Enter file name: "
> read fname
> if [ -e "$fname.cpp" ]
> then
> printf "%s exists\n" "$fname.h" >&2


Wrong filename in message

> continue


Useless use of continue

> elif [ -e "$fname.h" ]
> then
> printf "%s exists\n" "$fname.h" >&2
> continue


Useless use of continue.

> else
> printf "#ifndef %s_H\n#define %s_H\n\n\n#endif\n" \
> "$fname" "$fname" > "$fname.cpp"


Fails to uppercase the filename in the guard condition,
Outputs to the wrong file.
Does not handle the filename in a different directory.

> printf "#include %s.h\n" "$fname" > "$fname.h"


Outputs to the wrong file.
Fails to put quotation marks around the filename.

> break
> fi
> done
>


Script fails to quit if given filename of "q"
Script loops forever if input is redirected from /dev/null

I am sure you can do better Chris. Perhaps fix any problems in my solution
which was posted nearly an hour earlier, which has similar logic?
Kaz Kylheku

2006-11-27, 7:22 am


Gary Wessle wrote:
> Hi
>
> I find my self doing the following task many times and I just thought
> of automating it by writing a shell script which does it for me.
>
> the task.
>
> ask the user for a file name "f_name".
> create 2 files, the first named f_name.h and the second f_name.cpp
>
> in the first file place the following text
> ****************
> #ifndef F_NAME_H
> #efine F_NAME_H


You might want some additional characters here. A large project can
easily end up with two different headers which have the same name (but
are obviously in different directories). The two could end up included
in the same translation unit, if not directly than by way of other
includes. In the script below, I add the PID of the shell, but you can
hack something better.

Take the name on the command line; asking users questions is a bogus
approach that is poorly scriptable.

#!/bin/sh
#
# VC++ application wizard, ported to Unix
#

PATH=/bin:/usr/bin
export LC_COLLATE=C # so [A-Z] matches what we want

if [ $# != 1 ] ; then
echo "usage: $0 <filename>"
exit 1
fi

BASENAME=$1

#
# The name serves as a C and C++ preprocessor symbol, and with .h added
to it,
# as an include file name. This means that it has to meet certain
lexical
# conventions.
#

if ! echo $BASENAME | grep -q -E '^[_A-Za-z][A-Za-z0-9]+$' ; then
echo "$BASENAME isn't a valid C or C++ identifier"
exit 1
fi

case $BASENAME in
_* )
echo "Warning: symbols beginning with underscores are reserved by "
echo "ANSI C and C++. But you're the boss around here!"
;;
esac

#
# Existence checks
#

CLASH=$(ls -1 $BASENAME.{cpp,c,h} 2> /dev/null)

if [ "$CLASH" ] ; then
echo "cannot generate files because the following exist:"
echo $CLASH
exit 1
fi

#
# Generate files.
#

BASENAME_UC=$(echo $BASENAME | tr '[a-z]' '[A-Z]')

cat > $BASENAME.h <<!
#ifndef ${BASENAME_UC}_H_$$
#define ${BASENAME_UC}_H_$$

#endif
!

cat > $BASENAME.cpp <<!
#include "${BASENAME}.h"
!

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com