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
}









share|improve this question
























  • 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















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
}









share|improve this question
























  • 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













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
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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





share|improve this answer





















  • 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 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










  • extra argument (not beeing -a/-b will be caught by *) in case.
    – Archemar
    Mar 7 '17 at 13:01











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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





share|improve this answer





















  • 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 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










  • extra argument (not beeing -a/-b will be caught by *) in case.
    – Archemar
    Mar 7 '17 at 13:01















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





share|improve this answer





















  • 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 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










  • extra argument (not beeing -a/-b will be caught by *) in case.
    – Archemar
    Mar 7 '17 at 13:01













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • 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










  • 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










  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre