Source string Read only

(itstool) path: callout/screen
246/2460
Context English State
#!/bin/sh

. /etc/rc.subr

name="dummy"
start_cmd="${name}_start"
stop_cmd=":"
kiss_cmd="${name}_kiss"
extra_commands="kiss"

dummy_start()
{
if [ $# -gt 0 ]; then<co xml:id="rcng-args-start"/>
echo "Greeting message: $*"
else
echo "Nothing started."
fi
}

dummy_kiss()
{
echo -n "A ghost gives you a kiss"
if [ $# -gt 0 ]; then<co xml:id="rcng-args-kiss"/>
echo -n " and whispers: $*"
fi
case "$*" in
*[.!?])
echo
;;
*)
echo .
;;
esac
}

load_rc_config $name
run_rc_command "$@"<co xml:id="rcng-args-all"/>
What essential changes can we notice in the script?
All arguments you type after <option>start</option> can end up as positional parameters to the respective method. We can use them in any way according to our task, skills, and fancy. In the current example, we just pass all of them to <citerefentry><refentrytitle>echo</refentrytitle><manvolnum>1</manvolnum></citerefentry> as one string in the next line — note <envar>$*</envar> within the double quotes. Here is how the script can be invoked now:
<prompt>#</prompt> <userinput>/etc/rc.d/dummy start</userinput>
Nothing started.
<prompt>#</prompt> <userinput>/etc/rc.d/dummy start Hello world!</userinput>
Greeting message: Hello world!
The same applies to any method our script provides, not only to a standard one. We have added a custom method named <option>kiss</option>, and it can take advantage of the extra arguments not less than <option>start</option> does. E.g.:
<prompt>#</prompt> <userinput>/etc/rc.d/dummy kiss</userinput>
A ghost gives you a kiss.
<prompt>#</prompt> <userinput>/etc/rc.d/dummy kiss Once I was Etaoin Shrdlu...</userinput>
A ghost gives you a kiss and whispers: Once I was Etaoin Shrdlu...
If we want just to pass all extra arguments to any method, we can merely substitute <literal>"$@"</literal> for <literal>"$1"</literal> in the last line of our script, where we invoke <function>run_rc_command</function>.
An <citerefentry><refentrytitle>sh</refentrytitle><manvolnum>1</manvolnum></citerefentry> programmer ought to understand the subtle difference between <envar>$*</envar> and <envar>$@</envar> as the ways to designate all positional parameters. For its in-depth discussion, refer to a good handbook on <citerefentry><refentrytitle>sh</refentrytitle><manvolnum>1</manvolnum></citerefentry> scripting. <emphasis>Do not</emphasis> use the expressions until you fully understand them because their misuse will result in buggy and insecure scripts.
Currently <function>run_rc_command</function> may have a bug that prevents it from keeping the original boundaries between arguments. That is, arguments with embedded whitespace may not be processed correctly. The bug stems from <envar>$*</envar> misuse.
Further reading
<anchor xml:id="lukem"/><link xlink:href="http://www.mewburn.net/luke/papers/rc.d.pdf">The original article by Luke Mewburn</link> offers a general overview of <filename>rc.d</filename> and detailed rationale for its design decisions. It provides insight on the whole <filename>rc.d</filename> framework and its place in a modern BSD operating system.

Loading…

No matching activity found.

Browse all component changes

Things to check

Long untranslated

The string was not translated for a long time

Reset

Ellipsis

The string uses three dots (...) instead of an ellipsis character (…)

Reset

Glossary

English English
No related strings found in the glossary.

Source information

Source string comment
(itstool) path: callout/screen
Labels
No labels currently set.
Flags
no-wrap, read-only
Source string location
article.translate.xml:1313
Source string age
9 months ago
Translation file
, string 149