View Single Post
  #2   (View Single Post)  
Old 11th June 2021
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

Another approach to process the passed arguments is to use "shift". Using the above example:

$1 will contain "King Alfred the Great"
$2 holds the number "1"
$3 holds the number "2"

The special variable "$#" contains the number of arguments.
The situation after a shift:

$1 holds the number "1"
$2 holds the number "2"
and $# will be decreased by one to reflect the number of the remaining parameters.

A script to demonstrate that we can keep printing the first parameter in "$1" when we use shift:
Code:
#!/bin/sh
# J65nko - daemonforums.org

while [ $# -ne 0 ]; do
    nr=$((nr+1))
    echo  "Parameter $nr :\t$1"
    shift
done
Running with same arguments as in the previous example:
Code:
./paramshift.sh "King Alfred the Great" 1 2   
Parameter 1 :   King Alfred the Great
Parameter 2 :   1
Parameter 3 :   2
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote