Bash: Why is used eval and shift in a script that parses command line arguments?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something
or -s
some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long::
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument `$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument `$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"`$arg'" ; done
First of all what does the shift
program in the following line:
-a|--a-long) echo "Option a" ; shift ;;
Afterwards what is the purpose to use the eval
command in the following line:
eval set -- "$TEMP"
I tried to comment the line in script mentioned above and I got the following response:
$ ./getOptExample2.sh -a 10 -b 20 --a-long 40 -charem --c-long=echi
Param: -a
Option a
Param: 10
Internal error!
But if I uncomment it it runs like a charm:
Option a
Option b, argument `20'
Option a
Option c, argument `harem'
Option c, argument `echi'
Remaining arguments:
--> `10'
--> `40'
bash shell-script getopts eval
add a comment |Â
up vote
2
down vote
favorite
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something
or -s
some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long::
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument `$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument `$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"`$arg'" ; done
First of all what does the shift
program in the following line:
-a|--a-long) echo "Option a" ; shift ;;
Afterwards what is the purpose to use the eval
command in the following line:
eval set -- "$TEMP"
I tried to comment the line in script mentioned above and I got the following response:
$ ./getOptExample2.sh -a 10 -b 20 --a-long 40 -charem --c-long=echi
Param: -a
Option a
Param: 10
Internal error!
But if I uncomment it it runs like a charm:
Option a
Option b, argument `20'
Option a
Option c, argument `harem'
Option c, argument `echi'
Remaining arguments:
--> `10'
--> `40'
bash shell-script getopts eval
1
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something
or -s
some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long::
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument `$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument `$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"`$arg'" ; done
First of all what does the shift
program in the following line:
-a|--a-long) echo "Option a" ; shift ;;
Afterwards what is the purpose to use the eval
command in the following line:
eval set -- "$TEMP"
I tried to comment the line in script mentioned above and I got the following response:
$ ./getOptExample2.sh -a 10 -b 20 --a-long 40 -charem --c-long=echi
Param: -a
Option a
Param: 10
Internal error!
But if I uncomment it it runs like a charm:
Option a
Option b, argument `20'
Option a
Option c, argument `harem'
Option c, argument `echi'
Remaining arguments:
--> `10'
--> `40'
bash shell-script getopts eval
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something
or -s
some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long::
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument `$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument `$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"`$arg'" ; done
First of all what does the shift
program in the following line:
-a|--a-long) echo "Option a" ; shift ;;
Afterwards what is the purpose to use the eval
command in the following line:
eval set -- "$TEMP"
I tried to comment the line in script mentioned above and I got the following response:
$ ./getOptExample2.sh -a 10 -b 20 --a-long 40 -charem --c-long=echi
Param: -a
Option a
Param: 10
Internal error!
But if I uncomment it it runs like a charm:
Option a
Option b, argument `20'
Option a
Option c, argument `harem'
Option c, argument `echi'
Remaining arguments:
--> `10'
--> `40'
bash shell-script getopts eval
bash shell-script getopts eval
edited 35 mins ago
asked Aug 4 '17 at 7:12
Dimitrios Desyllas
16313
16313
1
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19
add a comment |Â
1
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19
1
1
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
One of the many things that getopt
does while parsing options is to rearrange the arguments, so that non-option arguments come last, and combined short options are split up. From man getopt
:
Output is generated for each element described in the previous section.
Output is done in the same order as the elements are specified in the
input, except for non-option parameters. Output can be done in
compatible (unquoted) mode, or in such way that whitespace and other
special characters within arguments and non-option parameters are
preserved (see QUOTING). When the output is processed in the shell
script, it will seem to be composed of distinct elements that can be
processed one by one (by using the shift command in most shell
languages).
[...]
Normally, no non-option parameters output is generated until all
options and their arguments have been generated. Then '--' is
generated as a single parameter, and after it the non-option parameters
in the order they were found, each as a separate parameter.
This effect is reflected in your code, where the option-handling loop assumes that all option arguments (including arguments to options) come first, and come separately, and are finally followed by non-option arguments.
So, TEMP
contains the rearranged, quoted, split-up options, and using eval set
makes them script arguments.
As for shift
, it does what it always does: remove the first argument, and shift all arguments (so that what was $2
will now be $1
). This eliminates the arguments that have been processed, so that, after this loop, only non-option arguments are left and you can conveniently use $@
without worrying about options.
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to-
. To do this, the first character in the short arguments list should be-
, as in-o -ab:c::
â bobpaul
Mar 20 at 17:51
add a comment |Â
up vote
1
down vote
The script works correctly when it gives you an error for -a 10
. The -a
option needs no parameter in this script. You should only use -a
.
The shift described in the man page as the following:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is
0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater
than $# or less than zero; otherwise 0.
So basically it drops -a and shift the remaining arguments so the second parameter will be $1 in the next cycle.
--
is also described in the man page:
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
One of the many things that getopt
does while parsing options is to rearrange the arguments, so that non-option arguments come last, and combined short options are split up. From man getopt
:
Output is generated for each element described in the previous section.
Output is done in the same order as the elements are specified in the
input, except for non-option parameters. Output can be done in
compatible (unquoted) mode, or in such way that whitespace and other
special characters within arguments and non-option parameters are
preserved (see QUOTING). When the output is processed in the shell
script, it will seem to be composed of distinct elements that can be
processed one by one (by using the shift command in most shell
languages).
[...]
Normally, no non-option parameters output is generated until all
options and their arguments have been generated. Then '--' is
generated as a single parameter, and after it the non-option parameters
in the order they were found, each as a separate parameter.
This effect is reflected in your code, where the option-handling loop assumes that all option arguments (including arguments to options) come first, and come separately, and are finally followed by non-option arguments.
So, TEMP
contains the rearranged, quoted, split-up options, and using eval set
makes them script arguments.
As for shift
, it does what it always does: remove the first argument, and shift all arguments (so that what was $2
will now be $1
). This eliminates the arguments that have been processed, so that, after this loop, only non-option arguments are left and you can conveniently use $@
without worrying about options.
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to-
. To do this, the first character in the short arguments list should be-
, as in-o -ab:c::
â bobpaul
Mar 20 at 17:51
add a comment |Â
up vote
3
down vote
accepted
One of the many things that getopt
does while parsing options is to rearrange the arguments, so that non-option arguments come last, and combined short options are split up. From man getopt
:
Output is generated for each element described in the previous section.
Output is done in the same order as the elements are specified in the
input, except for non-option parameters. Output can be done in
compatible (unquoted) mode, or in such way that whitespace and other
special characters within arguments and non-option parameters are
preserved (see QUOTING). When the output is processed in the shell
script, it will seem to be composed of distinct elements that can be
processed one by one (by using the shift command in most shell
languages).
[...]
Normally, no non-option parameters output is generated until all
options and their arguments have been generated. Then '--' is
generated as a single parameter, and after it the non-option parameters
in the order they were found, each as a separate parameter.
This effect is reflected in your code, where the option-handling loop assumes that all option arguments (including arguments to options) come first, and come separately, and are finally followed by non-option arguments.
So, TEMP
contains the rearranged, quoted, split-up options, and using eval set
makes them script arguments.
As for shift
, it does what it always does: remove the first argument, and shift all arguments (so that what was $2
will now be $1
). This eliminates the arguments that have been processed, so that, after this loop, only non-option arguments are left and you can conveniently use $@
without worrying about options.
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to-
. To do this, the first character in the short arguments list should be-
, as in-o -ab:c::
â bobpaul
Mar 20 at 17:51
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
One of the many things that getopt
does while parsing options is to rearrange the arguments, so that non-option arguments come last, and combined short options are split up. From man getopt
:
Output is generated for each element described in the previous section.
Output is done in the same order as the elements are specified in the
input, except for non-option parameters. Output can be done in
compatible (unquoted) mode, or in such way that whitespace and other
special characters within arguments and non-option parameters are
preserved (see QUOTING). When the output is processed in the shell
script, it will seem to be composed of distinct elements that can be
processed one by one (by using the shift command in most shell
languages).
[...]
Normally, no non-option parameters output is generated until all
options and their arguments have been generated. Then '--' is
generated as a single parameter, and after it the non-option parameters
in the order they were found, each as a separate parameter.
This effect is reflected in your code, where the option-handling loop assumes that all option arguments (including arguments to options) come first, and come separately, and are finally followed by non-option arguments.
So, TEMP
contains the rearranged, quoted, split-up options, and using eval set
makes them script arguments.
As for shift
, it does what it always does: remove the first argument, and shift all arguments (so that what was $2
will now be $1
). This eliminates the arguments that have been processed, so that, after this loop, only non-option arguments are left and you can conveniently use $@
without worrying about options.
One of the many things that getopt
does while parsing options is to rearrange the arguments, so that non-option arguments come last, and combined short options are split up. From man getopt
:
Output is generated for each element described in the previous section.
Output is done in the same order as the elements are specified in the
input, except for non-option parameters. Output can be done in
compatible (unquoted) mode, or in such way that whitespace and other
special characters within arguments and non-option parameters are
preserved (see QUOTING). When the output is processed in the shell
script, it will seem to be composed of distinct elements that can be
processed one by one (by using the shift command in most shell
languages).
[...]
Normally, no non-option parameters output is generated until all
options and their arguments have been generated. Then '--' is
generated as a single parameter, and after it the non-option parameters
in the order they were found, each as a separate parameter.
This effect is reflected in your code, where the option-handling loop assumes that all option arguments (including arguments to options) come first, and come separately, and are finally followed by non-option arguments.
So, TEMP
contains the rearranged, quoted, split-up options, and using eval set
makes them script arguments.
As for shift
, it does what it always does: remove the first argument, and shift all arguments (so that what was $2
will now be $1
). This eliminates the arguments that have been processed, so that, after this loop, only non-option arguments are left and you can conveniently use $@
without worrying about options.
answered Aug 4 '17 at 7:30
muru
34.6k579151
34.6k579151
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to-
. To do this, the first character in the short arguments list should be-
, as in-o -ab:c::
â bobpaul
Mar 20 at 17:51
add a comment |Â
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to-
. To do this, the first character in the short arguments list should be-
, as in-o -ab:c::
â bobpaul
Mar 20 at 17:51
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to
-
. To do this, the first character in the short arguments list should be -
, as in -o -ab:c::
â bobpaul
Mar 20 at 17:51
At least in the GNU enhanced getopt, you can prevent the re-ordering of the options list by setting the scanning mode to
-
. To do this, the first character in the short arguments list should be -
, as in -o -ab:c::
â bobpaul
Mar 20 at 17:51
add a comment |Â
up vote
1
down vote
The script works correctly when it gives you an error for -a 10
. The -a
option needs no parameter in this script. You should only use -a
.
The shift described in the man page as the following:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is
0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater
than $# or less than zero; otherwise 0.
So basically it drops -a and shift the remaining arguments so the second parameter will be $1 in the next cycle.
--
is also described in the man page:
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
add a comment |Â
up vote
1
down vote
The script works correctly when it gives you an error for -a 10
. The -a
option needs no parameter in this script. You should only use -a
.
The shift described in the man page as the following:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is
0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater
than $# or less than zero; otherwise 0.
So basically it drops -a and shift the remaining arguments so the second parameter will be $1 in the next cycle.
--
is also described in the man page:
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The script works correctly when it gives you an error for -a 10
. The -a
option needs no parameter in this script. You should only use -a
.
The shift described in the man page as the following:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is
0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater
than $# or less than zero; otherwise 0.
So basically it drops -a and shift the remaining arguments so the second parameter will be $1 in the next cycle.
--
is also described in the man page:
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
The script works correctly when it gives you an error for -a 10
. The -a
option needs no parameter in this script. You should only use -a
.
The shift described in the man page as the following:
shift [n]
The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is
0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater
than $# or less than zero; otherwise 0.
So basically it drops -a and shift the remaining arguments so the second parameter will be $1 in the next cycle.
--
is also described in the man page:
-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
answered Aug 4 '17 at 7:35
asalamon74
24122
24122
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f383862%2fbash-why-is-used-eval-and-shift-in-a-script-that-parses-command-line-arguments%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Your outputs with and without the comment are the same. Is that the problem?
â muru
Aug 4 '17 at 7:17
Fixed it! Thanks for noticing.
â Dimitrios Desyllas
Aug 4 '17 at 7:19