{"id":36,"date":"2018-10-20T20:00:00","date_gmt":"2018-10-20T12:00:00","guid":{"rendered":""},"modified":"2018-11-11T16:10:58","modified_gmt":"2018-11-11T08:10:58","slug":"the-linux-command-line-positional-parameters","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=36","title":{"rendered":"The Linux Command Line&#8212;Positional Parameters"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">When we last left our script, it looked something like this:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/># sysinfo_page - A script to produce a system information HTML file<br \/><br \/>##### Constants<br \/><br \/>TITLE=\"System Information for $HOSTNAME\"<br \/>RIGHT_NOW=$(date +\"%x %r %Z\")<br \/>TIME_STAMP=\"Updated on $RIGHT_NOW by $USER\"<br \/><br \/>##### Functions<br \/><br \/>system_info()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System release info&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;p&gt;Function not yet implemented&lt;\/p&gt;\"<br \/><br \/>}   # end of system_info<br \/><br \/><br \/>show_uptime()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System uptime&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    uptime<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/><br \/>}   # end of show_uptime<br \/><br \/><br \/>drive_space()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Filesystem space&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    df<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/><br \/>}   # end of drive_space<br \/><br \/><br \/>home_space()<br \/>{<br \/>    # Only the superuser can get this information<br \/><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$(id -u)\" = \"0\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Home directory space by user&lt;\/h2&gt;\"<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Bytes Directory\"<br \/>        du -s \/home\/* | sort -nr<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><br \/>}   # end of home_space<br \/><br \/><br \/><br \/>##### Main<br \/><br \/>cat &lt;&lt;- _EOF_<br \/>  &lt;html&gt;<br \/>  &lt;head&gt;<br \/>      &lt;title&gt;$TITLE&lt;\/title&gt;<br \/>  &lt;\/head&gt;<br \/>  &lt;body&gt;<br \/>      &lt;h1&gt;$TITLE&lt;\/h1&gt;<br \/>      &lt;p&gt;$TIME_STAMP&lt;\/p&gt;<br \/>      $(system_info)<br \/>      $(show_uptime)<br \/>      $(drive_space)<br \/>      $(home_space)<br \/>  &lt;\/body&gt;<br \/>  &lt;\/html&gt;<br \/>_EOF_<br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">We have most things working, but there are several more features I want to add:<\/div>\n<ol style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li>I want to specify the name of the output file on the command line, as well as set a default output file name if no name is specified.<\/li>\n<li>I want to offer an interactive mode that will prompt for a file name and warn the user if the file exists and prompt the user to overwrite it.<\/li>\n<li>Naturally, we want to have a help option that will display a usage message.<\/li>\n<\/ol>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">All of these features involve using command line options and arguments. To handle options on the command line, we use a facility in the shell called&nbsp;<i>positional parameters<\/i>. Positional parameters are a series of special variables (<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$0<\/tt>&nbsp;through&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$9<\/tt>) that contain the contents of the command line.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Let&#8217;s imagine the following command line:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">some_program word1 word2 word3<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">If&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">some_program<\/tt>&nbsp;were a bash shell script, we could read each item on the command line because the positional parameters contain the following:<\/div>\n<ul style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li>$0 would contain &#8220;some_program&#8221;<\/li>\n<li>$1 would contain &#8220;word1&#8221;<\/li>\n<li>$2 would contain &#8220;word2&#8221;<\/li>\n<li>$3 would contain &#8220;word3&#8221;<\/li>\n<\/ul>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Here is a script you can use to try this out:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Positional Parameters\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> '$0 = ' $0<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> '$1 = ' $1<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> '$2 = ' $2<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> '$3 = ' $3<br \/><\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Detecting Command Line Arguments<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Often, you will want to check to see if you have arguments on which to act. There are a couple of ways to do this. First, you could simply check to see if&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$1<\/tt>contains anything like so:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$1\" != \"\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Positional parameter 1 contains something\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">else<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Positional parameter 1 is empty\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Second, the shell maintains a variable called&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$#<\/tt>&nbsp;that contains the number of items on the command line in addition to the name of the command (<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$0<\/tt>).<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> $# -gt 0 <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Your <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">command<\/tt> line contains $# arguments\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">else<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Your <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">command<\/tt> line contains no arguments\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Command Line Options<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">As we discussed before, many programs, particularly ones from&nbsp;<a href=\"http:\/\/www.gnu.org\/\" style=\"color: #002740;\">the GNU Project<\/a>, support both short and long command line options. For example, to display a help message for many of these programs, you may use either the &#8220;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">-h<\/tt>&#8221; option or the longer &#8220;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">--help<\/tt>&#8221; option. Long option names are typically preceded by a double dash. We will adopt this convention for our scripts.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Here is the code we will use to process our command line:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">interactive=<br \/>filename=~\/sysinfo_page.html<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$1\" != \"\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $1 in<br \/>        -f | --file )           <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt><br \/>                                filename=$1<br \/>                                ;;<br \/>        -i | --interactive )    interactive=1<br \/>                                ;;<br \/>        -h | --help )           usage<br \/>                                <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt><br \/>                                ;;<br \/>        * )                     usage<br \/>                                <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt> 1<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">This code is a little tricky, so bear with me as I attempt to explain it.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The first two lines are pretty easy. We set the variable&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">interactive<\/tt>&nbsp;to be empty. This will indicate that the interactive mode has not been requested. Then we set the variable&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;to contain a default file name. If nothing else is specified on the command line, this file name will be used.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">After these two variables are set, we have default settings, in case the user does not specify any options.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Next, we construct a&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt>&nbsp;loop that will cycle through all the items on the command line and process each one with&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt>. The&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt>&nbsp;will detect each possible option and process it accordingly.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Now the tricky part. How does that loop work? It relies on the magic of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt>.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt>&nbsp;is a shell builtin that operates on the positional parameters. Each time you invoke&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt>, it &#8220;shifts&#8221; all the positional parameters down by one.&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$2<\/tt>becomes&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$1<\/tt>,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$3<\/tt>&nbsp;becomes&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$2<\/tt>,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$4<\/tt>&nbsp;becomes&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">$3<\/tt>, and so on. Try this:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You start with $# positional parameters\"<br \/><br \/># Loop until all parameters are used up<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$1\" != \"\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Parameter 1 equals $1\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"You now have $# positional parameters\"<br \/><br \/>    # Shift all the parameters down by one<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt><br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Getting An Option&#8217;s Argument<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Our &#8220;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">-f<\/tt>&#8221; option requires a valid file name as an argument. We use&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt>&nbsp;again to get the next item from the command line and assign it to&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>. Later we will have to check the content of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;to make sure it is valid.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Integrating The Command Line Processor Into The Script<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">We will have to move a few things around and add a usage function to get this new routine integrated into our script. We&#8217;ll also add some test code to verify that the command line processor is working correctly. Our script now looks like this:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash<br \/><br \/># sysinfo_page - A script to produce a system information HTML file<br \/><br \/>##### Constants<br \/><br \/>TITLE=\"System Information <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">for<\/tt> $HOSTNAME\"<br \/>RIGHT_NOW=$(date +\"%x %r %Z\")<br \/>TIME_STAMP=\"Updated on $RIGHT_NOW by $USER\"<br \/><br \/>##### Functions<br \/><br \/>system_info()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System release info&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;p&gt;Function not yet implemented&lt;\/p&gt;\"<br \/><br \/>}   # end of system_info<br \/><br \/><br \/>show_uptime()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System uptime&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    uptime<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/><br \/>}   # end of show_uptime<br \/><br \/><br \/>drive_space()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Filesystem space&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    df<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/><br \/>}   # end of drive_space<br \/><br \/><br \/>home_space()<br \/>{<br \/>    # Only the superuser can get this information<br \/><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$(id -u)\" = \"0\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Home directory space by user&lt;\/h2&gt;\"<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Bytes Directory\"<br \/>        du -s \/home\/* | sort -nr<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><br \/>}   # end of home_space<br \/><br \/><br \/>write_page()<br \/>{<br \/>    cat &lt;&lt;- _EOF_<br \/>    &lt;html&gt;<br \/>        &lt;head&gt;<br \/>        &lt;title&gt;$TITLE&lt;\/title&gt;<br \/>        &lt;\/head&gt;<br \/>        &lt;body&gt;<br \/>        &lt;h1&gt;$TITLE&lt;\/h1&gt;<br \/>        &lt;p&gt;$TIME_STAMP&lt;\/p&gt;<br \/>        $(system_info)<br \/>        $(show_uptime)<br \/>        $(drive_space)<br \/>        $(home_space)<br \/>        &lt;\/body&gt;<br \/>    &lt;\/html&gt;<br \/>_EOF_<br \/><br \/>}<br \/><br \/>usage()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"usage: sysinfo_page [[[-f file ] [-i]] | [-h]]\"<br \/>}<br \/><br \/><br \/>##### Main<br \/><br \/>interactive=<br \/>filename=~\/sysinfo_page.html<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">while<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$1\" != \"\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">do<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">case<\/tt> $1 in<br \/>        -f | --file )           <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt><br \/>                                filename=$1<br \/>                                ;;<br \/>        -i | --interactive )    interactive=1<br \/>                                ;;<br \/>        -h | --help )           usage<br \/>                                <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt><br \/>                                ;;<br \/>        * )                     usage<br \/>                                <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt> 1<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">esac<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">shift<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">done<\/tt><br \/><br \/><br \/># Test code to verify command line processing<br \/><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$interactive\" = \"1\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"interactive is on\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">else<\/tt><br \/> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"interactive is off\"<br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"output file = $filename\"<br \/><br \/><br \/># Write page (comment out until testing is complete)<br \/><br \/># write_page &gt; $filename<br \/><\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">Adding Interactive Mode<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The interactive mode is implemented with the following code:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$interactive\" = \"1\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/><br \/>    response=<br \/><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter name of output file [$filename] &gt; \"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> response<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> -n \"$response\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>        filename=$response<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> -f $filename <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Output file exists. Overwrite? (y\/n) &gt; \"<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">read<\/tt> response<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">if<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">[<\/tt> \"$response\" != \"y\" <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">];<\/tt> <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">then<\/tt><br \/>            <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Exiting program.\"<br \/>            <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">exit<\/tt> 1<br \/>        <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fi<\/tt><br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">First, we check if the interactive mode is on, otherwise we don&#8217;t have anything to do. Next, we ask the user for the file name. Notice the way the prompt is worded:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> -n \"Enter name of output file [$filename] &gt; \"<br \/><\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">We display the current value of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;since, the way this routine is coded, if the user just presses the enter key, the default value of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;will be used. This is accomplished in the next two lines where the value of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">response<\/tt>&nbsp;is checked. If&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">response<\/tt>&nbsp;is not empty, then&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;is assigned the value of<tt style=\"font-family: courier, lucidatypewriter, monospace;\">response<\/tt>. Otherwise,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace;\">filename<\/tt>&nbsp;is left unchanged, preserving its default value.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">After we have the name of the output file, we check if it already exists. If it does, we prompt the user. If the user response is not &#8220;y,&#8221; we give up and exit, otherwise we can proceed.<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts When we l &#8230; <a title=\"The Linux Command Line&#8212;Positional Parameters\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=36\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Positional Parameters\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,6],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}