Here is the context: I'm making a small Linux command for me to automatically create the base code when I create a C program.
The name of my command is ctouch
: it's like the touch
command but after creating the file(s) it also creates the basic structure of a C program:
#include <stdio.h>
#include <stdlib.h>
int main(){
return 0:
}
Appending these lines works. Here is my code:
#! /bin/bash
for arg in "$@"
do
[ ${arg: -2} == ".c" ] && touch "$arg" || touch "$arg.c"
echo '#include <stdio.h>' > "$arg"
echo '#include <stdlib.h>' >> "$arg"
echo $'\nint main90{\n\n\t' >> "$arg"
echo $'\n\treturn 0;\n\n}' >> "$arg"
done
As you see, it can create as much files as touch
can take arguments. The conditional loop (the line after the do
) is useful in case I forgot to specify the .c
extension: but the problem is that both if
and else
are executed when the argument does not end in .c
But it's ok when I don't "forget" to specify that it is a C file.
How can I fix that?