getopts Unix input
up vote
0
down vote
favorite
so in my code below I'm like trying to get my code to read the input that the user enters in my code as seen below
#./MyProject -a -b OR -b -a
However I keep getting a Syntax error and its not passing the tests given by the program: The following tests are: having inputs that aren't a&b (i.e c-z), having no inputs at all, having too little arguments, having too many arguments,
#Use just prints out the format like this : ./MyProject -a -b
- if ( ! getopts ":ab" arg) then
echo $use
fi
while [getopts ":ab" arg2]
do
case $arg2 in
t) if (($1 != "t" && $1 != "o")); then
echo $use
fi
esac
done
}
shell-script getopts
add a comment |
up vote
0
down vote
favorite
so in my code below I'm like trying to get my code to read the input that the user enters in my code as seen below
#./MyProject -a -b OR -b -a
However I keep getting a Syntax error and its not passing the tests given by the program: The following tests are: having inputs that aren't a&b (i.e c-z), having no inputs at all, having too little arguments, having too many arguments,
#Use just prints out the format like this : ./MyProject -a -b
- if ( ! getopts ":ab" arg) then
echo $use
fi
while [getopts ":ab" arg2]
do
case $arg2 in
t) if (($1 != "t" && $1 != "o")); then
echo $use
fi
esac
done
}
shell-script getopts
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
so in my code below I'm like trying to get my code to read the input that the user enters in my code as seen below
#./MyProject -a -b OR -b -a
However I keep getting a Syntax error and its not passing the tests given by the program: The following tests are: having inputs that aren't a&b (i.e c-z), having no inputs at all, having too little arguments, having too many arguments,
#Use just prints out the format like this : ./MyProject -a -b
- if ( ! getopts ":ab" arg) then
echo $use
fi
while [getopts ":ab" arg2]
do
case $arg2 in
t) if (($1 != "t" && $1 != "o")); then
echo $use
fi
esac
done
}
shell-script getopts
so in my code below I'm like trying to get my code to read the input that the user enters in my code as seen below
#./MyProject -a -b OR -b -a
However I keep getting a Syntax error and its not passing the tests given by the program: The following tests are: having inputs that aren't a&b (i.e c-z), having no inputs at all, having too little arguments, having too many arguments,
#Use just prints out the format like this : ./MyProject -a -b
- if ( ! getopts ":ab" arg) then
echo $use
fi
while [getopts ":ab" arg2]
do
case $arg2 in
t) if (($1 != "t" && $1 != "o")); then
echo $use
fi
esac
done
}
shell-script getopts
shell-script getopts
edited Nov 25 at 23:42
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Mar 7 '17 at 10:37
Bob
11
11
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38
add a comment |
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
The following example should work for you.
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments usingif (($# < 2 )); then usage; fi
.
– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The following example should work for you.
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments usingif (($# < 2 )); then usage; fi
.
– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
|
show 2 more comments
up vote
4
down vote
The following example should work for you.
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments usingif (($# < 2 )); then usage; fi
.
– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
|
show 2 more comments
up vote
4
down vote
up vote
4
down vote
The following example should work for you.
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b
The following example should work for you.
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b
answered Mar 7 '17 at 10:46
daisy
28.2k48167298
28.2k48167298
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments usingif (($# < 2 )); then usage; fi
.
– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
|
show 2 more comments
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments usingif (($# < 2 )); then usage; fi
.
– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
Thank you so much daisy for answering so quickly, Do you know tho about having too little arguments?
– Bob
Mar 7 '17 at 11:09
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
too little or none :) if you can let me know i'll appreciate it!
– Bob
Mar 7 '17 at 11:16
@Bob you can check arguments using
if (($# < 2 )); then usage; fi
.– Valentin Bajrami
Mar 7 '17 at 11:16
@Bob you can check arguments using
if (($# < 2 )); then usage; fi
.– Valentin Bajrami
Mar 7 '17 at 11:16
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
Thanks and if there was an extra argument after the list? I tried if (($# > 3 )); then usage; fi but it didn't work
– Bob
Mar 7 '17 at 12:19
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
extra argument (not beeing -a/-b will be caught by *) in case.
– Archemar
Mar 7 '17 at 13:01
|
show 2 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f349705%2fgetopts-unix-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry everyone I've been having a hard time trying to convert my code into the coding here.... I don't know how to format it correctly...
– Bob
Mar 7 '17 at 10:38