Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
            echo    "Your   name    is  "$1
fi

If you execute mypgm1, as follows:


Click here to view code image
matthew@seymour:~$ bash mypgm1


you get the following output:


Click here to view code image
Name not provided


However, if you execute mypgm1 as follows:


Click here to view code image
matthew@seymour:~$ bash mypgm1 Sandra


you get this output:


Click here to view code image
Your name is Sandra


The shell program mypgm1 also illustrates another aspect of shell
programming: the built-in variables provided to the shell by the Linux kernel.
In mypgm1, the built-in variable $# provides the number of positional
parameters passed to the shell program. You learn more about working with
built-in variables in the next major section of this chapter.


Using Positional Parameters to Access and Retrieve


Variables from the Command Line


Using positional parameters in scripts can be helpful if you need to use
command lines with piped commands requiring complex arguments. Shell
programs containing positional parameters can be even more convenient if the
commands are infrequently used. For example, if you use your Ubuntu system
with an attached voice modem as an answering machine, you can write a
script to issue a command that retrieves and plays the voice messages. The
following lines convert a saved sound file (in .rmd or voice-phone format)
and pipe the result to your system’s audio device:


Click here to view code image
#!/bin/sh


play voice message in /var/spool/voice/incoming


rmdtopvf /var/spool/voice/incoming/$1 | pvfspeed -s 8000 | \
pvftobasic >/dev/audio

Free download pdf