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:So, question @hortimech.
You suggested:and not:Code:
[ -d "$dir" ] || echo "Not a directory"I get it. But if you are doing more than one command in a statement how do I accomplish this?Code:
if ! [ -d "$dir" ]; thenecho "Not a directory"fiOr would you do a hundred semicolons making your code unreadable?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?}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.
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