I am not an expert of shell scripting, but I can do some simple things. Now, I read a script written by others and I need some help from the experts of this forum.
Please help me to understand what is going on in this cycle:
In particular, I do not understand this synax: [[...]] && \ export... ||,
where the || operand is used after the export command. It rise by me the question if the export command return any Boolean value?
if [[ ... ]] ; then
export...
else
echo "Please ...
fi
the \ is used for cosmetic reasons : when a line is too long, you can break it by typing <enter> but in order to "desactivate" the <enter> you have to protect it first by a \
so the line
[[...]] && export... || cmd...
can be put one several line to make it readable when commands are too long:
if [ $COMPILER = "sun" ]; then
if [[ -f /sunstudio12/bin/sunf90 ]]; then
export IMDI_COMPILER=/sunstudio12/bin/sunf90
else
echo "Please specify location!"
exit
fi
if [[ -z "$SZIP_ROOT" ]]; then
SZIP_ROOT=/szip-2.1
fi
elif [ $COMPILER = "intel" ]; then
[[ xxx ]] is test [ xxx ], see man test or test in man ksh.
I call this "procedure as logic". Run the expresions and commands connected by && in order and keep going as long as commnads return true). You see this in PERL and C/C++ occasionally. Every command returns a logical value, the state of $?, 0 = true. I prefer, for each step, "if ( !next_command );then print error on next command and exit". It keeps the indentedness under control, separates the steps and reads easily. I call it "Cut them off at the pass!" (prevent complexity by doing simple, important things first, and when they are out of the way, the rest is simpler).
Sorry, but this is absolute tosh. A Conditional Expression is not the same as a "test" and they have their own syntax and rules. Admittedly there is some small overlap in the syntax for certain conditions (such as "-f").
Every single command run on a UNIX/Linux/POSIX system will return an exit status. If that exit status is 0, the shell treats that as a boolean true. Any other exit status is treated as a boolean false.
Regards,
Alister
---------- Post updated at 02:21 PM ---------- Previous update was at 02:14 PM ----------
Actually, export always returns a 0 exit status, so it will never fail. Scrutinizer's code should work just as the original.
That said, I'll leave my post as is since that's a very unusual corner case. In just about every other situation, a command can return a non-zero exit status and the constructs would not be logically equivalent.
Your quite right Alister, however I suspect my interpretation is what the author of the script intended to write. Otherwise there is no logic to the error message and the exit statement. This illustrates how easily these logical ANDs and ORs get misinterpreted. That is precisely why I dislike them especially when there is a x && y || z concoction..
--edit--
Actually I think I was correct after all:
If the test fails then the statement after the && does not get executed since the left side is false. And then the part after the || gets excecuted since its left side was false and also the export will never fail, so that will not trigger the right side of the or.
OK, only now I read your added remark to your post. Probably all of us are right then
Your interpretation is indeed correct, but its correctness hinges on export always returning 0. I mentioned that not to nitpick, but as a heads up for newbies.
Regards,
Alister
---------- Post updated at 02:48 PM ---------- Previous update was at 02:45 PM ----------
Hmmm. POSIX says export returns 0 and doesn't mention any possible error conditions. However, some shells' export builtin can return a non-zero status. Perhaps it's better to be safe than sorry and not treat export's return as a given.
But still then I am back to my original point and that it is unlikely what the author intended, especially given the error message, so we better correct the author. BTW, do you ever check return codes of export statements in your scripts?