Skip to main content
added 12 characters in body
Source Link
knc1
  • 5
  • 2

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair - this chunk is hard coded for the last pair.
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then decrement the indices (as in third eval statement) 

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then decrement the indices (as in third eval statement) 

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair - this chunk is hard coded for the last pair.
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then decrement the indices (as in third eval statement) 

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test
added 12 characters in body
Source Link
knc1
  • 5
  • 2

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then shiftdecrement twicethe untilindices all(as pairsin arethird handledeval statement) 

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then shift twice until all pairs are handled

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then decrement the indices (as in third eval statement) 

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test
Source Link
knc1
  • 5
  • 2

If a function or an application has more than zero arguments, it always has a last argument.

If you want to read option flag and value pairs, as in: $ ./t.sh -o output -i input -l last

And you want to accept a variable number of option/value pairs,

And do not want a huge "if .. then .. else .. fi" tree,

Then after checking for an argument count of non-zero and even,

Write a while loop with these four eval statements as the body, followed by a case statement using the two values determined in each pass through the loop.

The tricky part of the scripting is demonstrated here:

#!/bin/sh    

# For each pair
eval TMP="'$'$#"
eval "PICK=$TMP"
eval TMP="'$'$(($#-1))"
eval "OPT=$TMP"

# process as required - usually a case statement on $OPT
echo "$OPT \n $PICK"

# Then shift twice until all pairs are handled

:<< EoF_test
$ ./t.sh -o output -i input -l last
-l 
last
$ ./t.sh -o output -l last
-l 
last
$ ./t.sh  -l last
-l 
last
EoF_test