I first learned how to code in a C
language, and using
if ( code )
then
code
fi
makes it really hard for me to read my code (don't ask why).
Is there a substitute for then
?
Something like this:
if ( code ) {
code
}
fi
I first learned how to code in a C
language, and using
if ( code )
then
code
fi
makes it really hard for me to read my code (don't ask why).
Is there a substitute for then
?
Something like this:
if ( code ) {
code
}
fi
There's no substitute, but you can use both if you really need the braces:
if true ; then {
something
} fi
That uses an unnecessary explicitly-braced command group as the body of the then
block. Any commands in the braces will just be executed in the current shell when the then
runs, exactly as though the braces weren't there.
I wouldn't really recommend this style in general or for code that someone else will need to read, but it does give you the braces you want, and it'll do the trick if you're using an editor that requires braced blocks or something like that.
{ if { [[ a == aa ]];};then { echo yes;};else { echo no;}; fi;}
– basically, when you use bash
enough, the standard syntax becomes familiar enough, so that it isn't much of an issue.
You seem to be looking for the C shell (csh
, tcsh
, etc...). Be aware that experts recommend against it for programming.
I'm not sure whether or not the part that you consider to be unappealing is the fact that then
is on the following line? If so, consider using the following style:
if condition; then
stuff
fi
I have to admit that spelling statement names backwards like fi
for if
and esac
for case
(but not elihw
for while
!) is kind of quirky. But that's standard Bourne shell, the most portable shell syntax you can use.
then
unappealing, it's just that my editor will highlight corresponding {}
pairs, and that's useful for when I have some sort of error because then I can easily see all of my if
statements and see if something went wrong.
vi
's %
command which jumps to the matching brace/parenthesis/bracket. I sympathize. I miss vi
's %
command when programming in Python.
Maybe this is easier on the eyes:
[ condition ] && {
# statement 1
# statment 2
# ...
}
Other than that, just get used to it. Use the language's idioms.
The following is near to there. And weird.
: if; { code; } && {
code
}
I personally never liked the...
if true; then
more;fi
...form myself. I like it when the control words lead. It always seemed strange to me that the prevailing ...;then
form came about at all.
if this && that
then do these things too
else i will probably get upset
fi
It's almost entirely opposite to your request, but it's another way to do it. You can also tack any wanted redirects onto the fi
line as needed and they will apply to the whole block.
if(...) {
, i.e. leaving opening brace on the same line as if
in C-like languages.
if
command, even if on the next line.
;
delim in that case. It's why they do.
:
shell builtin like this, just so a line of code could be byte-wise closer to C syntax, should not be encouraged in any way.
Commented
Oct 31, 2015 at 14:50
Here is a way:
[ "$var" = "find" ] && echo "found"
is equivalent to:
if [ "$var" = "find" ]; then
echo "found"
fi
{...}
around the echo "found"
, and then you'll be heading in the right direction.
I always use:
if (code); then
code;
fi
Probably not exactly what you want but it's a bit more c like.