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

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

$
0
0
So, question @hortimech.

You suggested:

Code:

[ -d "$dir" ] || echo "Not a directory"
and not:

Code:

if ! [ -d "$dir" ]; thenecho "Not a directory"fi
I get it. But if you are doing more than one command in a statement how do I accomplish this?

Code:

myfunc(){if ! [ -d "$dir" ]; thenecho "Not a directory"mkdir -p "$dir"echo "Made a directory"#do many tasks herereturn 1fi# if not a directory, why are you here?}
Or would you do a hundred semicolons making your code unreadable?

Code:

[ -d "$dir" ] || echo "Not a directory"; mkdir -p "$dir"; echo "made a directory"; return 1; etc.

To me the latter is far less readable. Which is better for you? Or is there another option?

In my code, if applied, all if statements would have more than one command of something to do.
I was discussing a single option, where if something doesn't exist, then print a message, but your function is interesting, though why use a function at all ? If it is a 'one-off' then just do it in line, like this:

Code:

[ -d "$dir" ] || \{ echo "Not a directory"  mkdir -p "$dir" || { echo "Could not create '$dir'"; exit 1; }  echo "Made a directory"  #do many tasks here}

Statistics: Posted by hortimech — Wed Feb 04, 2026 7:38 am



Viewing all articles
Browse latest Browse all 8082

Trending Articles