Move under www to ease rsync
This commit is contained in:
91
www/eps/hpr2756/hpr2756_bash20_ex1.sh
Executable file
91
www/eps/hpr2756/hpr2756_bash20_ex1.sh
Executable file
@@ -0,0 +1,91 @@
|
||||
#!/bin/bash
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Example 1 for Bash Tips show 20: deleting individual array elements
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#
|
||||
# Seed the random number generator with a nanosecond number
|
||||
#
|
||||
RANDOM=$(date +%N)
|
||||
|
||||
echo "Indexed array"
|
||||
echo "-------------"
|
||||
|
||||
#
|
||||
# Create indexed array and populate with ad ae ... cf
|
||||
#
|
||||
declare -a iarr
|
||||
mapfile -t iarr < <(printf '%s\n' {a..c}{d..f})
|
||||
|
||||
#
|
||||
# Report element count and show the structure
|
||||
#
|
||||
echo "Length: ${#iarr[*]}"
|
||||
declare -p iarr
|
||||
|
||||
#
|
||||
# Unset a random element
|
||||
#
|
||||
ind=$((RANDOM % ${#iarr[*]}))
|
||||
echo "Element $ind to be removed, contents: ${iarr[$ind]}"
|
||||
unset "iarr[$ind]"
|
||||
|
||||
#
|
||||
# Report on the result of the element removal
|
||||
#
|
||||
echo "Length: ${#iarr[*]}"
|
||||
declare -p iarr
|
||||
|
||||
echo
|
||||
echo "Associative array"
|
||||
echo "-----------------"
|
||||
|
||||
#
|
||||
# Create associative array. Populate with the indices from the indexed array
|
||||
# using the array contents as the subscripts.
|
||||
#
|
||||
declare -A aarr
|
||||
for (( i = 0; i <= ${#iarr[*]}; i++ )); do
|
||||
# If there's a "hole" in iarr don't create an element
|
||||
[[ -v iarr[$i] ]] && aarr[${iarr[$i]}]=$i
|
||||
done
|
||||
|
||||
#
|
||||
# Report element count and keys
|
||||
#
|
||||
echo "Length: ${#aarr[*]}"
|
||||
echo "Keys: ${!aarr[*]}"
|
||||
|
||||
#
|
||||
# Use a loop to report array contents in sorted order
|
||||
#
|
||||
for key in $(echo "${iarr[@]}" | sort); do
|
||||
echo "aarr[$key]=${aarr[$key]}"
|
||||
done
|
||||
|
||||
#
|
||||
# Make another contiguous indexed array of the associative array's keys. We
|
||||
# don't care about their order
|
||||
#
|
||||
declare -a keys
|
||||
mapfile -t keys < <(printf '%s\n' ${!aarr[*]})
|
||||
|
||||
#
|
||||
# Unset a random element. The indexed array 'keys' contains the keys
|
||||
# of the associative array so we use the selected one as a subscript. We use
|
||||
# this array because it doesn't have any "holes". If we'd used 'iarr' we might
|
||||
# have hit the "hole" we created earlier!
|
||||
#
|
||||
k=$((RANDOM % ${#keys[*]}))
|
||||
echo "Element '${keys[$k]}' to be removed, contents: ${aarr[${keys[$k]}]}"
|
||||
unset "aarr[${keys[$k]}]"
|
||||
|
||||
#
|
||||
# Report final element count and keys
|
||||
#
|
||||
echo "Length: ${#aarr[*]}"
|
||||
echo "Keys: ${!aarr[*]}"
|
||||
declare -p aarr
|
||||
|
||||
exit
|
||||
26
www/eps/hpr2756/hpr2756_bash20_ex2.sh
Executable file
26
www/eps/hpr2756/hpr2756_bash20_ex2.sh
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Example 2 for Bash Tips show 20: simple use of positional parameters
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#
|
||||
# This script needs 2 arguments
|
||||
#
|
||||
if [[ $# -ne 2 ]]; then
|
||||
echo "Usage: $0 word count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
word=$1
|
||||
count=$2
|
||||
|
||||
#
|
||||
# Repeat the 'word' 'count' times on the same line
|
||||
#
|
||||
for (( i = 1; i <= count; i++ )); do
|
||||
echo -n "$word"
|
||||
done
|
||||
echo
|
||||
|
||||
exit
|
||||
410
www/eps/hpr2756/hpr2756_full_shownotes.html
Executable file
410
www/eps/hpr2756/hpr2756_full_shownotes.html
Executable file
@@ -0,0 +1,410 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="pandoc">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
<meta name="author" content="Dave Morriss">
|
||||
<title>Bash Tips - 20 (HPR Show 2756)</title>
|
||||
<style type="text/css">code{white-space: pre;}</style>
|
||||
<!--[if lt IE 9]>
|
||||
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
||||
<![endif]-->
|
||||
<link rel="stylesheet" href="http://hackerpublicradio.org/css/hpr.css">
|
||||
</head>
|
||||
|
||||
<body id="home">
|
||||
<div id="container" class="shadow">
|
||||
<header>
|
||||
<h1 class="title">Bash Tips - 20 (HPR Show 2756)</h1>
|
||||
<h2 class="subtitle">Deleting arrays; positional and special parameters in Bash</h2>
|
||||
<h2 class="author">Dave Morriss</h2>
|
||||
<hr/>
|
||||
</header>
|
||||
|
||||
<main id="maincontent">
|
||||
<article>
|
||||
<header>
|
||||
<h1>Table of Contents</h1>
|
||||
<nav id="TOC">
|
||||
<ul>
|
||||
<li><a href="#tidying-loose-ends-some-collateral-bash-tips">Tidying loose ends <small><small>(Some collateral Bash tips)</small></small></a><ul>
|
||||
<li><a href="#deleting-arrays">Deleting arrays</a></li>
|
||||
<li><a href="#positional-and-special-parameters">Positional and Special parameters</a></li>
|
||||
<li><a href="#silly-titles">Silly titles</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#the-unset-command">The <code>unset</code> command</a><ul>
|
||||
<li><a href="#the--v-option">The <code>'-v'</code> option</a></li>
|
||||
<li><a href="#the--f-option">The <code>'-f'</code> option</a></li>
|
||||
<li><a href="#the--n-option">The <code>'-n'</code> option</a></li>
|
||||
<li><a href="#variables-marked-as-readonly">Variables marked as <code>readonly</code></a></li>
|
||||
<li><a href="#using-a-dollar-sign-in-front-of-the-variable-name">Using a dollar sign in front of the variable name</a></li>
|
||||
<li><a href="#arrays-and-array-elements">Arrays and array elements</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#positional-parameters">Positional Parameters</a><ul>
|
||||
<li><a href="#the-set-command">The <code>set</code> command</a></li>
|
||||
<li><a href="#the-shift-command">The <code>shift</code> command</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#special-parameters">Special Parameters</a></li>
|
||||
<li><a href="#examples">Examples</a><ul>
|
||||
<li><a href="#creating-a-bash-shell-with-arguments">Creating a Bash shell with arguments</a></li>
|
||||
<li><a href="#downloadable-script">Downloadable script</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#links">Links</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
<h2 id="tidying-loose-ends-some-collateral-bash-tips">Tidying loose ends <small><small>(Some collateral Bash tips)</small></small></h2>
|
||||
<h3 id="deleting-arrays">Deleting arrays</h3>
|
||||
<p>I forgot to cover one thing on my list when doing the last show: I forgot to explain how to delete arrays and array elements. I’ll cover that topic in this episode.</p>
|
||||
<h3 id="positional-and-special-parameters">Positional and Special parameters</h3>
|
||||
<p>I have also avoided talking much about the positional and special parameters in Bash: <code>'$1'</code>, <code>'$2'</code>, <code>'$#'</code> and the rest. I will cover (some of) these in this episode.</p>
|
||||
<h3 id="silly-titles">Silly titles</h3>
|
||||
<p>I stopped doing the weird episode titles by episode 14 because I thought the joke was getting tired. However, I think a few people missed them (and a certain HPR colleague was found <em>vandalising my new titles as they were being posted</em> ;-), so I have added them inside the notes on the older shows and am adding one here – as a homage to silliness.</p>
|
||||
<h2 id="the-unset-command">The <code>unset</code> command</h2>
|
||||
<p>This is a built-in command that originated from the Bourne shell. It removes variables, arrays, parts of arrays or functions.</p>
|
||||
<p>The command syntax is (from the GNU Bash manual):</p>
|
||||
<pre><code>unset [-fnv] [name]</code></pre>
|
||||
<p>The <code>unset</code> command removes each variable or function represented by <em>name</em>. This is just the name of the thing to be deleted and does not take a dollar sign (<code>'$'</code>). If the variable or function does not exist then this does not cause an error.</p>
|
||||
<h3 id="the--v-option">The <code>'-v'</code> option</h3>
|
||||
<p>If this option is given each <em>name</em> refers to a shell variable, which is removed.</p>
|
||||
<pre><code>$ fruit='rambutan'
|
||||
$ echo "fruit is $fruit"
|
||||
fruit is rambutan
|
||||
$ unset -v fruit
|
||||
$ echo "fruit is $fruit"
|
||||
fruit is</code></pre>
|
||||
<p>A variable <code>unset</code> in this way will have been completely removed. This is not the same as setting the variable to null:</p>
|
||||
<pre><code>$ fruit='mangosteen'
|
||||
$ if [[ -v fruit ]]; then echo "Exists"; else echo "Doesn't exist"; fi
|
||||
Exists
|
||||
$ fruit=
|
||||
$ if [[ -v fruit ]]; then echo "Exists"; else echo "Doesn't exist"; fi
|
||||
Exists
|
||||
$ unset -v fruit
|
||||
$ if [[ -v fruit ]]; then echo "Exists"; else echo "Doesn't exist"; fi
|
||||
Doesn't exist</code></pre>
|
||||
<p>Remember that the Bash conditional expression <code>'-v varname'</code> returns <em>true</em> if the shell variable <em>varname</em> is set (has been assigned a value). Being null simply means that the variable has a null value, but it still exists.</p>
|
||||
<h3 id="the--f-option">The <code>'-f'</code> option</h3>
|
||||
<p>If this option is given then each <em>name</em> refers to a shell function, which is removed. Although there’s not much more to say, we’ll look at this in a little more detail when we cover functions in a formal way in a later episode.</p>
|
||||
<p>Note that if no option is given to <code>'unset'</code> each <em>name</em> is first checked to see if it is a variable, and if it is it is removed. If not and <em>name</em> is a function then <u>it</u> is removed. This could be unfortunate if you have variables and functions with similar names and you mistype a variable name.</p>
|
||||
<p>The POSIX definition states that functions can only be removed if the <code>'-f'</code> option is given.</p>
|
||||
<h3 id="the--n-option">The <code>'-n'</code> option</h3>
|
||||
<p>This option is for removing variables with the <code>nameref</code> option set. We will look at such variables in a later show and will go into more detail about unsetting them then.</p>
|
||||
<h3 id="variables-marked-as-readonly">Variables marked as <code>readonly</code></h3>
|
||||
<p>These cannot be unset. We touched on this in episode 19 with the <code>'declare -r'</code> and <code>'readonly'</code> commands.</p>
|
||||
<h3 id="using-a-dollar-sign-in-front-of-the-variable-name">Using a dollar sign in front of the variable name</h3>
|
||||
<p>The issue of whether the dollar sign is used or not is important. Consider the following:</p>
|
||||
<pre><code>$ a='b'
|
||||
$ b='Contents of variable b'
|
||||
$ echo "a=$a b=$b"
|
||||
a=b b=Contents of variable b
|
||||
$ unset $a # <--- Don't do this!
|
||||
$ echo "a=$a b=$b"
|
||||
a=b b=</code></pre>
|
||||
<p>Here the variable <code>'b'</code> has been removed where (presumably) the intention was to remove variable <code>'a'</code>!</p>
|
||||
<h3 id="arrays-and-array-elements">Arrays and array elements</h3>
|
||||
<p>Entire arrays can be removed with one of the following:</p>
|
||||
<pre><code>unset array
|
||||
unset array[*]
|
||||
unset array[@]</code></pre>
|
||||
<p>Note again that the array name is <u>not</u> preceded by a dollar sign (<code>'$'</code>).</p>
|
||||
<p>Individual elements may be removed as follows:</p>
|
||||
<pre><code>unset array[subscript]</code></pre>
|
||||
<p>As expected, the <em>subscript</em> must be numeric (or an expression returning a number) for indexed arrays. For associative arrays the <em>subscript</em> is a string (or an expression returning a string). Care is needed to quote appropriately if the subscript string contains spaces.</p>
|
||||
<p>An index for an indexed array can be negative, as discussed in earlier shows, in which case the element in question is relative to the end of the array.</p>
|
||||
<p>Note that <code>ShellCheck</code>, the script checking tool, advises that when subscripted arrays are used with <code>unset</code> they be quoted to avoid problems with glob expansion. The examples in this episode do this.</p>
|
||||
<p>I have included a downloadable script <a href="hpr2756_bash20_ex1.sh">bash20_ex1.sh</a> which demonstrates array element deletion for both types of array:</p>
|
||||
<pre><code>#!/bin/bash
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Example 1 for Bash Tips show 20: deleting individual array elements
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#
|
||||
# Seed the random number generator with a nanosecond number
|
||||
#
|
||||
RANDOM=$(date +%N)
|
||||
|
||||
echo "Indexed array"
|
||||
echo "-------------"
|
||||
|
||||
#
|
||||
# Create indexed array and populate with ad ae ... cf
|
||||
#
|
||||
declare -a iarr
|
||||
mapfile -t iarr < <(printf '%s\n' {a..c}{d..f})
|
||||
|
||||
#
|
||||
# Report element count and show the structure
|
||||
#
|
||||
echo "Length: ${#iarr[*]}"
|
||||
declare -p iarr
|
||||
|
||||
#
|
||||
# Unset a random element
|
||||
#
|
||||
ind=$((RANDOM % ${#iarr[*]}))
|
||||
echo "Element $ind to be removed, contents: ${iarr[$ind]}"
|
||||
unset "iarr[$ind]"
|
||||
|
||||
#
|
||||
# Report on the result of the element removal
|
||||
#
|
||||
echo "Length: ${#iarr[*]}"
|
||||
declare -p iarr
|
||||
|
||||
echo
|
||||
echo "Associative array"
|
||||
echo "-----------------"
|
||||
|
||||
#
|
||||
# Create associative array. Populate with the indices from the indexed array
|
||||
# using the array contents as the subscripts.
|
||||
#
|
||||
declare -A aarr
|
||||
for (( i = 0; i <= ${#iarr[*]}; i++ )); do
|
||||
# If there's a "hole" in iarr don't create an element
|
||||
[[ -v iarr[$i] ]] && aarr[${iarr[$i]}]=$i
|
||||
done
|
||||
|
||||
#
|
||||
# Report element count and keys
|
||||
#
|
||||
echo "Length: ${#aarr[*]}"
|
||||
echo "Keys: ${!aarr[*]}"
|
||||
|
||||
#
|
||||
# Use a loop to report array contents in sorted order
|
||||
#
|
||||
for key in $(echo "${iarr[@]}" | sort); do
|
||||
echo "aarr[$key]=${aarr[$key]}"
|
||||
done
|
||||
|
||||
#
|
||||
# Make another contiguous indexed array of the associative array's keys. We
|
||||
# don't care about their order
|
||||
#
|
||||
declare -a keys
|
||||
mapfile -t keys < <(printf '%s\n' ${!aarr[*]})
|
||||
|
||||
#
|
||||
# Unset a random element. The indexed array 'keys' contains the keys
|
||||
# of the associative array so we use the selected one as a subscript. We use
|
||||
# this array because it doesn't have any "holes". If we'd used 'iarr' we might
|
||||
# have hit the "hole" we created earlier!
|
||||
#
|
||||
k=$((RANDOM % ${#keys[*]}))
|
||||
echo "Element '${keys[$k]}' to be removed, contents: ${aarr[${keys[$k]}]}"
|
||||
unset "aarr[${keys[$k]}]"
|
||||
|
||||
#
|
||||
# Report final element count and keys
|
||||
#
|
||||
echo "Length: ${#aarr[*]}"
|
||||
echo "Keys: ${!aarr[*]}"
|
||||
declare -p aarr
|
||||
|
||||
exit</code></pre>
|
||||
<p>Running the script generates the following output:</p>
|
||||
<pre><code>Indexed array
|
||||
-------------
|
||||
Length: 9
|
||||
declare -a iarr=([0]="ad" [1]="ae" [2]="af" [3]="bd" [4]="be" [5]="bf" [6]="cd" [7]="ce" [8]="cf")
|
||||
Element 5 to be removed, contents: bf
|
||||
Length: 8
|
||||
declare -a iarr=([0]="ad" [1]="ae" [2]="af" [3]="bd" [4]="be" [6]="cd" [7]="ce" [8]="cf")
|
||||
|
||||
Associative array
|
||||
-----------------
|
||||
Length: 8
|
||||
Keys: be bd af ad ae cd ce cf
|
||||
aarr[ad]=0
|
||||
aarr[ae]=1
|
||||
aarr[af]=2
|
||||
aarr[bd]=3
|
||||
aarr[be]=4
|
||||
aarr[cd]=6
|
||||
aarr[ce]=7
|
||||
aarr[cf]=8
|
||||
Element 'ae' to be removed, contents: 1
|
||||
Length: 7
|
||||
Keys: be bd af ad cd ce cf
|
||||
declare -A aarr=([be]="4" [bd]="3" [af]="2" [ad]="0" [cd]="6" [ce]="7" [cf]="8" )
|
||||
</code></pre>
|
||||
<p>I have included a lot of comments in the script to explain what it is doing.</p>
|
||||
<p>Things to note are:</p>
|
||||
<ul>
|
||||
<li><p>The indexed array <code>'iarr'</code> is filled with character pairs in order, and the indices generated are a contiguous sequence. Once an element is unset a “hole” is created in the sequence.</p></li>
|
||||
<li><p>The associative array <code>'aarr'</code> has a random element deleted. The element is selected by using the indexed array <code>'keys'</code> which is created from the keys themselves. We use this rather than <code>'iarr'</code> so that the random number can’t match the “hole” we created earlier.</p></li>
|
||||
</ul>
|
||||
<hr />
|
||||
<h2 id="positional-parameters">Positional Parameters</h2>
|
||||
<p>The <em>positional parameters</em> are “the shell’s command-line arguments” - to quote the GNU Bash Manual.</p>
|
||||
<p>We have seen these parameters in various contexts in other shows in the <a href="http://hackerpublicradio.org/series.php?id=42" title="Bash Scripting series">Bash Scripting</a> series. They are denoted by numbers 1 onwards, as in <code>'$1'</code>. It is possible to set these parameters at the time the shell is invoked, but it is more common to see them being used in scripts. When a shell script is invoked any positional parameters are temporarily replaced by the arguments to the script, and the same occurs when executing a shell function.</p>
|
||||
<p>The positional parameters are referred to as <code>'$N'</code> or <code>'${N}'</code> where <em>N</em> is a number starting from 1. The <code>'${N}'</code> <u>must</u> be used if <em>N</em> is 10 or above. These numbers denote the position of the argument of course. The positional parameters cannot be set in assignment statements, so <code>'1=42'</code> is illegal.</p>
|
||||
<h3 id="the-set-command">The <code>set</code> command</h3>
|
||||
<p>This command (which is <b>hugely</b> complex!) allows the positional parameters to be cleared or redefined (amongst many other capabilities).</p>
|
||||
<pre><code>set -- [argument...]</code></pre>
|
||||
<p>This clears the positional parameters and, if there are arguments provided, places them into the parameters.</p>
|
||||
<pre><code>set - [argument...]</code></pre>
|
||||
<p>If there are arguments then they replace the positional parameters. If no arguments are given then the positional parameters remain unchanged.</p>
|
||||
<h3 id="the-shift-command">The <code>shift</code> command</h3>
|
||||
<p>This is a Bourne shell builtin command. Its job is to shift the positional parameters to the left.</p>
|
||||
<pre><code>shift [n]</code></pre>
|
||||
<p>If <em>n</em> is omitted it is assumed to be 1. The positional parameters from <code>n+1</code> onwards are renamed to <code>$1</code> onwards. So, if the positional parameters are:</p>
|
||||
<pre><code>Haggis Neeps Tatties</code></pre>
|
||||
<p>The command <code>'shift 2'</code> will result in them being:</p>
|
||||
<pre><code>Tatties</code></pre>
|
||||
<p>It is an error to shift more places than there are parameters, and <em>n</em> cannot be negative. The <code>'shift'</code> command returns a non-zero status if there is an error, but the positional parameters are not changed.</p>
|
||||
<h2 id="special-parameters">Special Parameters</h2>
|
||||
<p>A number of these are related to the positional parameters, and we will concentrate on these at the moment. The GNU Bash manual contains more detail than is shown here. Look at the manual for the full information if needed.</p>
|
||||
<table>
|
||||
<colgroup>
|
||||
<col style="width: 30%" />
|
||||
<col style="width: 69%" />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr class="header">
|
||||
<th style="text-align: left;">Parameter</th>
|
||||
<th style="text-align: left;">Explanation</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="odd">
|
||||
<td style="text-align: left;"><b>*</b></td>
|
||||
<td style="text-align: left;"><code>($*)</code> Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. When in double quotes a string is formed containing the positional parameters separated by the <code>IFS</code> delimiter. This is similar to the expression <code>"${array[*]}"</code> which we have seen before.</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td style="text-align: left;"><b>@</b></td>
|
||||
<td style="text-align: left;"><code>($@)</code> Expands to the positional parameters, starting from one. If the expression is within double quotes the parameters form quoted words. This is similar to the expression <code>"${array[@]}"</code> which we have seen before.</td>
|
||||
</tr>
|
||||
<tr class="odd">
|
||||
<td style="text-align: left;"><b>#</b></td>
|
||||
<td style="text-align: left;"><code>($#)</code> Expands to the number of positional parameters in decimal.</td>
|
||||
</tr>
|
||||
<tr class="even">
|
||||
<td style="text-align: left;"><b>0</b></td>
|
||||
<td style="text-align: left;"><code>($0)</code> Expands to the name of the shell or shell script.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h2 id="examples">Examples</h2>
|
||||
<h3 id="creating-a-bash-shell-with-arguments">Creating a Bash shell with arguments</h3>
|
||||
<p>The following snippets show how Bash can be invoked with arguments and how they can be manipulated:</p>
|
||||
<pre><code>$ /bin/bash -s Hacker Public Radio
|
||||
$ echo $0
|
||||
/bin/bash
|
||||
$ echo $#
|
||||
3
|
||||
$ echo $@
|
||||
Hacker Public Radio</code></pre>
|
||||
<p>Here <code>/bin/bash</code> is invoked with the <code>'-s'</code> option which causes it to run interactively and allows arguments. Inside the shell <code>'$0'</code> contains the file used to invoke Bash. There are three positional parameters, and these are displayed</p>
|
||||
<pre><code>$ set - $@ is cool
|
||||
$ echo $#
|
||||
5
|
||||
$ echo $@
|
||||
Hacker Public Radio is cool</code></pre>
|
||||
<p>The <code>'set'</code> command is used to change the positional parameters to themselves (<code>'$@'</code>) with two more. These make the count 5, then the new parameters are displayed.</p>
|
||||
<pre><code>$ shift 2
|
||||
$ echo $@
|
||||
Radio is cool</code></pre>
|
||||
<p>This shows the use of the <code>'shift'</code> command to move the parameters two places to the left, thereby removing the first two of them.</p>
|
||||
<pre><code>$ cat /tmp/test
|
||||
#!/bin/bash
|
||||
|
||||
echo $#
|
||||
echo $@
|
||||
|
||||
exit
|
||||
$ /tmp/test rice millet wheat
|
||||
3
|
||||
rice millet wheat
|
||||
$ echo $@
|
||||
Radio is cool
|
||||
$ exit</code></pre>
|
||||
<p>A very simple script in <code>/tmp/test</code> is shown which displays the count of its arguments and the arguments themselves. It is invoked with three arguments which temporarily become the positional parameters of the script. The shell’s positional parameters are still intact afterwards though.</p>
|
||||
<p>The <code>'exit'</code> terminates the shell which was created at the start of these snippets.</p>
|
||||
<h3 id="downloadable-script">Downloadable script</h3>
|
||||
<p>Here is a very simple downloadable script <a href="hpr2756_bash20_ex2.sh">bash20_ex2.sh</a> which demonstrates the use of arguments:</p>
|
||||
<pre><code>#!/bin/bash
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# Example 2 for Bash Tips show 20: simple use of positional parameters
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#
|
||||
# This script needs 2 arguments
|
||||
#
|
||||
if [[ $# -ne 2 ]]; then
|
||||
echo "Usage: $0 word count"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
word=$1
|
||||
count=$2
|
||||
|
||||
#
|
||||
# Repeat the 'word' 'count' times on the same line
|
||||
#
|
||||
for (( i = 1; i <= count; i++ )); do
|
||||
echo -n "$word"
|
||||
done
|
||||
echo
|
||||
|
||||
exit</code></pre>
|
||||
<p>Running the script as <code>'./bash20_ex2.sh goodbye 3'</code> generates the following output:</p>
|
||||
<pre><code>goodbyegoodbyegoodbye
|
||||
</code></pre>
|
||||
<h2 id="links">Links</h2>
|
||||
<ul>
|
||||
<li><a href="https://www.gnu.org/software/bash/manual/bash.html">“<em>GNU BASH Reference Manual</em>”</a>
|
||||
<ul>
|
||||
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion">“3.4 Shell Parameters”</a></li>
|
||||
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Shell-Builtin-Commands">“4 Shell Builtin Commands”</a>
|
||||
<ul>
|
||||
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins">“4.1 Bourne Shell Builtins”</a></li>
|
||||
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins">“4.2 Bash Builtin Commands”</a></li>
|
||||
</ul></li>
|
||||
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin">“4.3.1 The Set Builtin”</a></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
<!--- -->
|
||||
<ul>
|
||||
<li><p>POSIX Shell Command Language: <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset"><em>unset</em></a></p></li>
|
||||
<li><p><a href="http://hackerpublicradio.org/series.php?id=42">HPR series: <em>Bash Scripting</em></a></p></li>
|
||||
<li>Previous episodes under the heading <em>Bash Tips</em>:
|
||||
<ol>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=1648">HPR episode 1648 “<em>Bash parameter manipulation</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=1843">HPR episode 1843 “<em>Some Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=1884">HPR episode 1884 “<em>Some more Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=1903">HPR episode 1903 “<em>Some further Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=1951">HPR episode 1951 “<em>Some additional Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2045">HPR episode 2045 “<em>Some other Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2278">HPR episode 2278 “<em>Some supplementary Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2293">HPR episode 2293 “<em>More supplementary Bash tips</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2639">HPR episode 2639 “<em>Some ancillary Bash tips - 9</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2649">HPR episode 2649 “<em>More ancillary Bash tips - 10</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2659">HPR episode 2659 “<em>Further ancillary Bash tips - 11</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2669">HPR episode 2669 “<em>Additional ancillary Bash tips - 12</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2679">HPR episode 2679 “<em>Extra ancillary Bash tips - 13</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2689">HPR episode 2689 “<em>Bash Tips - 14 (Some auxiliary Bash tips)</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2699">HPR episode 2699 “<em>Bash Tips - 15 (More auxiliary Bash tips)</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2709">HPR episode 2709 “<em>Bash Tips - 16 (Further auxiliary Bash tips)</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2719">HPR episode 2719 “<em>Bash Tips - 17 (Additional auxiliary Bash tips)</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2729">HPR episode 2729 “<em>Bash Tips - 18 (Extra auxiliary Bash tips)</em>”</a></li>
|
||||
<li><a href="http://hackerpublicradio.org/eps.php?id=2739">HPR episode 2739 “<em>Bash Tips - 19 (Supplemental auxiliary Bash tips)</em>”</a></li>
|
||||
</ol></li>
|
||||
</ul>
|
||||
<!--- -->
|
||||
<ul>
|
||||
<li>Resources:
|
||||
<ul>
|
||||
<li>Examples: <a href="hpr2756_bash20_ex1.sh">bash20_ex1.sh</a>, <a href="hpr2756_bash20_ex2.sh">bash20_ex2.sh</a></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
</article>
|
||||
</main>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user