Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7995

C/C++ • Re: Little BASH file For Adding New Source File to Multi-File Project

$
0
0
A few suggestions.
In the beginning you have a filename passed to the script. You test it with -z, which instead you should use -r (see `man test`). That'll show if the file exists and is readable.
In your functions you have arguments that aren't tested. Were the correct arguments and correct amount of arguments passed?

Code:

myfunc(){if [ $# -ne 3 ]; thenecho "not enough arguments"fi}
Do the values of those arguments have valid contents?

Code:

myfunc(){for x in "$@"; doif [ -z "$x" ];thenecho "No contents"fidone}
And there are further tests that can be done, based on the script specs. RegEx can be used too.

And you assume that the directory in which you are writing files does in fact exist and is writable.

Perhaps a check before writing?

Code:

if ! [ -d "$dir" ]; thenecho "Not a directory"fiif ! [ -w "$dir" ]; thenecho "Directory not writable.fi
I learned a long time ago: Assume Nothing.

You could also use touch to initialize a file. Or /dev/null.

Statistics: Posted by valkyrie44 — Tue Feb 03, 2026 1:08 am



Viewing all articles
Browse latest Browse all 7995

Trending Articles