Hi bash fans,
There is such a thing as too terse. Can somebody explain what this cryptic snippet means? I get the "substitute part; it's the "error,exit" part that has me confounded.
+------------------+--------------------+---------------+--------------+ | | parameter | parameter | parameter | | | Set and Not Null | Set But Null | Unset | +------------------+--------------------+---------------+--------------+ |${parameter:?word}|substitute parameter|error,exit |error,exit | |${parameter?word} |substitute parameter|substitute null|error,exit | +------------------+--------------------+---------------+--------------+
TIA, Mike Wright
On 11/4/18 10:00 PM, Mike Wright wrote:
There is such a thing as too terse. Can somebody explain what this cryptic snippet means? I get the "substitute part; it's the "error,exit" part that has me confounded.
My assumption is that it will print an error and abort the script.
On Mon, Nov 5, 2018 at 7:51 AM Mike Wright nobody@nospam.hostisimo.com wrote:
There is such a thing as too terse. Can somebody explain what this cryptic snippet means? I get the "substitute part; it's the "error,exit" part that has me confounded.
+------------------+--------------------+---------------+--------------+ | | parameter | parameter | parameter | | | Set and Not Null | Set But Null | Unset | +------------------+--------------------+---------------+--------------+ |${parameter:?word}|substitute parameter|error,exit |error,exit | |${parameter?word} |substitute parameter|substitute null|error,exit | +------------------+--------------------+---------------+--------------+
In the case of ":?word" and "unset", for example, it'll print "word", and exit the script if it's running within a script.
th macbook12 ~ $ bash --version GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin18.0.0) ...
th macbook12 ~ $ echo ${PS1:?fedora} \u \h \w $
th macbook12 ~ $ echo ${PS5:?fedora} -bash: PS5: fedora
th macbook12 ~ $ vi mike.sh
th macbook12 ~ $ chmod +x mike.sh
th macbook12 ~ $ cat mike.sh #!/bin/sh one=ONE three=THREE echo "print one" echo ${one:?fedora} echo echo "print two" echo ${PS5:?fedora} echo echo "print three" echo ${three}
th macbook12 ~ $ ./mike.sh print one ONE
print two ./mike.sh: line 8: PS5: fedora
On 11/5/18 12:54 AM, Tom H wrote:
On Mon, Nov 5, 2018 at 7:51 AM Mike Wright nobody@nospam.hostisimo.com wrote:
There is such a thing as too terse. Can somebody explain what this cryptic snippet means? | | parameter | parameter | parameter | | | Set and Not Null | Set But Null | Unset | +------------------+--------------------+---------------+--------------+ |${parameter?word} |substitute parameter|substitute null|error,exit | +------------------+--------------------+---------------+--------------+
In the case of ":?word" and "unset", for example, it'll print "word", and exit the script if it's running within a script.
</snip>
Thanks for the extensive reply Tom.
I've been poking at these for a bit now. The are powerful. It helped me to rearrange the chart:
+--------------------+-----------+-------------+-------------+ | parameter is --> | set | null,empty | not set | +--------------------+-----------+-------------+-------------+ | ${parameter-word} | parameter | null | word | | ${parameter=word} | parameter | null | assign word | | ${parameter?word} | parameter | null | error, exit | | ${parameter+word} | word | word | null | +--------------------+-----------+-------------+-------------+ | parameter: is --> | set | null,empty,not set | +--------------------+-----------+-------------+-------------+ | ${parameter:-word} | parameter | word | | ${parameter:=word} | parameter | assign word | | ${parameter:?word} | parameter | error, exit | | ${parameter:+word} | word | null | +--------------------+-----------+---------------------------+
and then try to come up with uses.
Some time back I made a simple egg timer and had to test for a "how long" parameter and used if []; then.
Using posix parameter expansion turned it into this:
# if $1 wait that long # else wait this long seconds=300
sleep ${1:-$seconds} mplayer ~/Music/otherSounds/Chicken-clucking.mp3
On that note, the timer just went off. Time to get the cornbread out of the oven :D