2 number multiplying and adding
M="Wrong"
P="Wrong"
I=$1
while [ "$I" != "" ]
do
case $I in
-m | --multiplying)
shift
M=`expr $1 * $2`
;;
-p | --plus )
shift
P=`expr $1 + $2`
;;
*) echo "Something wrong"
exit 1
;;
esac
shift
I=$1
done
echo "Multiplying=$M, Adding=$P"
I give two number after each -m and -p
So when i run my code i want this to happen
./code.sh -m 2 2 -p 5 5
Multiplying=4 Adding=10
my code simply skip the and -p part and goes straigh to
*) echo "Something wrong"
What am i doing wrong?
linux shell-script
|
show 2 more comments
M="Wrong"
P="Wrong"
I=$1
while [ "$I" != "" ]
do
case $I in
-m | --multiplying)
shift
M=`expr $1 * $2`
;;
-p | --plus )
shift
P=`expr $1 + $2`
;;
*) echo "Something wrong"
exit 1
;;
esac
shift
I=$1
done
echo "Multiplying=$M, Adding=$P"
I give two number after each -m and -p
So when i run my code i want this to happen
./code.sh -m 2 2 -p 5 5
Multiplying=4 Adding=10
my code simply skip the and -p part and goes straigh to
*) echo "Something wrong"
What am i doing wrong?
linux shell-script
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Addset -x
to your script to see what is happening.
– G-Man
Dec 19 '18 at 5:30
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) Theset -x
should illuminate what's happening.
– G-Man
Dec 19 '18 at 5:34
1
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49
|
show 2 more comments
M="Wrong"
P="Wrong"
I=$1
while [ "$I" != "" ]
do
case $I in
-m | --multiplying)
shift
M=`expr $1 * $2`
;;
-p | --plus )
shift
P=`expr $1 + $2`
;;
*) echo "Something wrong"
exit 1
;;
esac
shift
I=$1
done
echo "Multiplying=$M, Adding=$P"
I give two number after each -m and -p
So when i run my code i want this to happen
./code.sh -m 2 2 -p 5 5
Multiplying=4 Adding=10
my code simply skip the and -p part and goes straigh to
*) echo "Something wrong"
What am i doing wrong?
linux shell-script
M="Wrong"
P="Wrong"
I=$1
while [ "$I" != "" ]
do
case $I in
-m | --multiplying)
shift
M=`expr $1 * $2`
;;
-p | --plus )
shift
P=`expr $1 + $2`
;;
*) echo "Something wrong"
exit 1
;;
esac
shift
I=$1
done
echo "Multiplying=$M, Adding=$P"
I give two number after each -m and -p
So when i run my code i want this to happen
./code.sh -m 2 2 -p 5 5
Multiplying=4 Adding=10
my code simply skip the and -p part and goes straigh to
*) echo "Something wrong"
What am i doing wrong?
linux shell-script
linux shell-script
edited Dec 19 '18 at 6:21
Rui F Ribeiro
39k1479130
39k1479130
asked Dec 19 '18 at 5:24
Knap
11
11
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Addset -x
to your script to see what is happening.
– G-Man
Dec 19 '18 at 5:30
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) Theset -x
should illuminate what's happening.
– G-Man
Dec 19 '18 at 5:34
1
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49
|
show 2 more comments
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Addset -x
to your script to see what is happening.
– G-Man
Dec 19 '18 at 5:30
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) Theset -x
should illuminate what's happening.
– G-Man
Dec 19 '18 at 5:34
1
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Add
set -x
to your script to see what is happening.– G-Man
Dec 19 '18 at 5:30
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Add
set -x
to your script to see what is happening.– G-Man
Dec 19 '18 at 5:30
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) The
set -x
should illuminate what's happening.– G-Man
Dec 19 '18 at 5:34
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) The
set -x
should illuminate what's happening.– G-Man
Dec 19 '18 at 5:34
1
1
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49
|
show 2 more comments
1 Answer
1
active
oldest
votes
#!/bin/sh
M="Wrong"
P="Wrong"
while [ "$#" -gt 0 ]; do
case $1 in
-m|--multiplying)
M=$(( $2 * $3 ))
;;
-p|--plus)
P=$(( $2 + $3 ))
;;
*) echo 'Error in command line options' >&2
exit 1
esac
shift 3
done
printf 'Multiplying=%s, Adding=%sn' "$M" "$P"
Your code is a bit difficult to follow due to the shift
s being spread out, but the main issue is that you are lacking one shift
operation per loop. Your script calculates the result of the first arithmetic operation correctly, but when it comes to looking at the second arithmetic operation, it has only performed two shift operations (each -p A B
requires three shift
to be processed and for the following -m C D
to be in $1
, $2
and $3
).
My modified version of your code only does a single shift 3
after each operation, while the operation itself uses $1
, $2
and $3
. I'm also using the more modern arithmetic expansion $(( ... ))
rather than the antiquated expr
utility. I'm testing on $#
whether there are more command line arguments to process and I've update the output slightly so that printf
is used for variable data and so that diagnostic messages are outputted onto the standard error stream.
Also note that having options that take more than one option argument is a bit unusual.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f489826%2f2-number-multiplying-and-adding%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
#!/bin/sh
M="Wrong"
P="Wrong"
while [ "$#" -gt 0 ]; do
case $1 in
-m|--multiplying)
M=$(( $2 * $3 ))
;;
-p|--plus)
P=$(( $2 + $3 ))
;;
*) echo 'Error in command line options' >&2
exit 1
esac
shift 3
done
printf 'Multiplying=%s, Adding=%sn' "$M" "$P"
Your code is a bit difficult to follow due to the shift
s being spread out, but the main issue is that you are lacking one shift
operation per loop. Your script calculates the result of the first arithmetic operation correctly, but when it comes to looking at the second arithmetic operation, it has only performed two shift operations (each -p A B
requires three shift
to be processed and for the following -m C D
to be in $1
, $2
and $3
).
My modified version of your code only does a single shift 3
after each operation, while the operation itself uses $1
, $2
and $3
. I'm also using the more modern arithmetic expansion $(( ... ))
rather than the antiquated expr
utility. I'm testing on $#
whether there are more command line arguments to process and I've update the output slightly so that printf
is used for variable data and so that diagnostic messages are outputted onto the standard error stream.
Also note that having options that take more than one option argument is a bit unusual.
add a comment |
#!/bin/sh
M="Wrong"
P="Wrong"
while [ "$#" -gt 0 ]; do
case $1 in
-m|--multiplying)
M=$(( $2 * $3 ))
;;
-p|--plus)
P=$(( $2 + $3 ))
;;
*) echo 'Error in command line options' >&2
exit 1
esac
shift 3
done
printf 'Multiplying=%s, Adding=%sn' "$M" "$P"
Your code is a bit difficult to follow due to the shift
s being spread out, but the main issue is that you are lacking one shift
operation per loop. Your script calculates the result of the first arithmetic operation correctly, but when it comes to looking at the second arithmetic operation, it has only performed two shift operations (each -p A B
requires three shift
to be processed and for the following -m C D
to be in $1
, $2
and $3
).
My modified version of your code only does a single shift 3
after each operation, while the operation itself uses $1
, $2
and $3
. I'm also using the more modern arithmetic expansion $(( ... ))
rather than the antiquated expr
utility. I'm testing on $#
whether there are more command line arguments to process and I've update the output slightly so that printf
is used for variable data and so that diagnostic messages are outputted onto the standard error stream.
Also note that having options that take more than one option argument is a bit unusual.
add a comment |
#!/bin/sh
M="Wrong"
P="Wrong"
while [ "$#" -gt 0 ]; do
case $1 in
-m|--multiplying)
M=$(( $2 * $3 ))
;;
-p|--plus)
P=$(( $2 + $3 ))
;;
*) echo 'Error in command line options' >&2
exit 1
esac
shift 3
done
printf 'Multiplying=%s, Adding=%sn' "$M" "$P"
Your code is a bit difficult to follow due to the shift
s being spread out, but the main issue is that you are lacking one shift
operation per loop. Your script calculates the result of the first arithmetic operation correctly, but when it comes to looking at the second arithmetic operation, it has only performed two shift operations (each -p A B
requires three shift
to be processed and for the following -m C D
to be in $1
, $2
and $3
).
My modified version of your code only does a single shift 3
after each operation, while the operation itself uses $1
, $2
and $3
. I'm also using the more modern arithmetic expansion $(( ... ))
rather than the antiquated expr
utility. I'm testing on $#
whether there are more command line arguments to process and I've update the output slightly so that printf
is used for variable data and so that diagnostic messages are outputted onto the standard error stream.
Also note that having options that take more than one option argument is a bit unusual.
#!/bin/sh
M="Wrong"
P="Wrong"
while [ "$#" -gt 0 ]; do
case $1 in
-m|--multiplying)
M=$(( $2 * $3 ))
;;
-p|--plus)
P=$(( $2 + $3 ))
;;
*) echo 'Error in command line options' >&2
exit 1
esac
shift 3
done
printf 'Multiplying=%s, Adding=%sn' "$M" "$P"
Your code is a bit difficult to follow due to the shift
s being spread out, but the main issue is that you are lacking one shift
operation per loop. Your script calculates the result of the first arithmetic operation correctly, but when it comes to looking at the second arithmetic operation, it has only performed two shift operations (each -p A B
requires three shift
to be processed and for the following -m C D
to be in $1
, $2
and $3
).
My modified version of your code only does a single shift 3
after each operation, while the operation itself uses $1
, $2
and $3
. I'm also using the more modern arithmetic expansion $(( ... ))
rather than the antiquated expr
utility. I'm testing on $#
whether there are more command line arguments to process and I've update the output slightly so that printf
is used for variable data and so that diagnostic messages are outputted onto the standard error stream.
Also note that having options that take more than one option argument is a bit unusual.
edited Dec 19 '18 at 7:00
answered Dec 19 '18 at 6:55
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
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%2f489826%2f2-number-multiplying-and-adding%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
(1) Thank you for describing the behavior you want. But, when you ask a "What am I doing wrong?" question, you should also describe the behavior that you are getting. (2) Add
set -x
to your script to see what is happening.– G-Man
Dec 19 '18 at 5:30
my code simply skip the and -p part and goes straigh to *) echo "Something wrong"
– Knap
Dec 19 '18 at 5:32
and doesnt display any result
– Knap
Dec 19 '18 at 5:33
(1) Thank you. I forgot to say, "Please do not respond in comments; edit your question to make it clearer and more complete." (2) The
set -x
should illuminate what's happening.– G-Man
Dec 19 '18 at 5:34
1
@KhushrajRathod Why? This is the correct site for it.
– Kusalananda
Dec 19 '18 at 6:49