Re: OT: bash parameter substitutions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/5/18 12:54 AM, Tom H wrote:
On Mon, Nov 5, 2018 at 7:51 AM Mike Wright <nobody@xxxxxxxxxxxxxxxxxxxx> 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

_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux