condition's statement gets skipped
i am new to scripting.how do i correct this bash script. for all values it gives me the "else" condition - "wrong value"
#!/bin/bash
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
echo $first
if [ "$pro" == "1" ];then
echo $first
echo $last
elif [ "$pro" == "2" ];then
echo $first
echo $last
echo $pro
else
echo "wrong value!"
fi
bash
add a comment |
i am new to scripting.how do i correct this bash script. for all values it gives me the "else" condition - "wrong value"
#!/bin/bash
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
echo $first
if [ "$pro" == "1" ];then
echo $first
echo $last
elif [ "$pro" == "2" ];then
echo $first
echo $last
echo $pro
else
echo "wrong value!"
fi
bash
2
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
you need to pass the variable name toread
without a dollar sign.
– oguzismail
Nov 11 at 7:48
add a comment |
i am new to scripting.how do i correct this bash script. for all values it gives me the "else" condition - "wrong value"
#!/bin/bash
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
echo $first
if [ "$pro" == "1" ];then
echo $first
echo $last
elif [ "$pro" == "2" ];then
echo $first
echo $last
echo $pro
else
echo "wrong value!"
fi
bash
i am new to scripting.how do i correct this bash script. for all values it gives me the "else" condition - "wrong value"
#!/bin/bash
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
echo $first
if [ "$pro" == "1" ];then
echo $first
echo $last
elif [ "$pro" == "2" ];then
echo $first
echo $last
echo $pro
else
echo "wrong value!"
fi
bash
bash
edited Nov 11 at 7:59
Red Cricket
4,24483382
4,24483382
asked Nov 11 at 7:43
vamsi
62
62
2
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
you need to pass the variable name toread
without a dollar sign.
– oguzismail
Nov 11 at 7:48
add a comment |
2
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
you need to pass the variable name toread
without a dollar sign.
– oguzismail
Nov 11 at 7:48
2
2
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
you need to pass the variable name to
read
without a dollar sign.– oguzismail
Nov 11 at 7:48
you need to pass the variable name to
read
without a dollar sign.– oguzismail
Nov 11 at 7:48
add a comment |
1 Answer
1
active
oldest
votes
You need to change …
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
… to …
read -p "First Name: " first
read -p "Last Name: " last
read -p "profession 1 for software and 2 for hardware (1/2): " pro
Note the lack of $
on first
, last
and pro
. In shell you do not use the $
on a variable when assigning it a value.
Further explanation: you use$
to get the value of a variable, but not to set it. E.g.read varname
,varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g.echo "$pro"
instead of justecho $pro
.
– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
More information (and probably more information than you want): iffirst
isn't set, thenread $first
is equivalent toread
, which implicitly uses the variableREPLY
to store the input. Iffirst
is set, then the expansion produces one (or possibly more) words to use as variables. For instance,first=foo; read $first
sets the variablefoo
.first="foo bar"; read $first
would set the variablesfoo
andbar
after performing word-splitting on the input. All of this is to say, it's not wrong to useread $first
; it just doesn't do what you expected. :)
– chepner
Nov 11 at 20:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53246779%2fconditions-statement-gets-skipped%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
You need to change …
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
… to …
read -p "First Name: " first
read -p "Last Name: " last
read -p "profession 1 for software and 2 for hardware (1/2): " pro
Note the lack of $
on first
, last
and pro
. In shell you do not use the $
on a variable when assigning it a value.
Further explanation: you use$
to get the value of a variable, but not to set it. E.g.read varname
,varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g.echo "$pro"
instead of justecho $pro
.
– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
More information (and probably more information than you want): iffirst
isn't set, thenread $first
is equivalent toread
, which implicitly uses the variableREPLY
to store the input. Iffirst
is set, then the expansion produces one (or possibly more) words to use as variables. For instance,first=foo; read $first
sets the variablefoo
.first="foo bar"; read $first
would set the variablesfoo
andbar
after performing word-splitting on the input. All of this is to say, it's not wrong to useread $first
; it just doesn't do what you expected. :)
– chepner
Nov 11 at 20:18
add a comment |
You need to change …
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
… to …
read -p "First Name: " first
read -p "Last Name: " last
read -p "profession 1 for software and 2 for hardware (1/2): " pro
Note the lack of $
on first
, last
and pro
. In shell you do not use the $
on a variable when assigning it a value.
Further explanation: you use$
to get the value of a variable, but not to set it. E.g.read varname
,varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g.echo "$pro"
instead of justecho $pro
.
– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
More information (and probably more information than you want): iffirst
isn't set, thenread $first
is equivalent toread
, which implicitly uses the variableREPLY
to store the input. Iffirst
is set, then the expansion produces one (or possibly more) words to use as variables. For instance,first=foo; read $first
sets the variablefoo
.first="foo bar"; read $first
would set the variablesfoo
andbar
after performing word-splitting on the input. All of this is to say, it's not wrong to useread $first
; it just doesn't do what you expected. :)
– chepner
Nov 11 at 20:18
add a comment |
You need to change …
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
… to …
read -p "First Name: " first
read -p "Last Name: " last
read -p "profession 1 for software and 2 for hardware (1/2): " pro
Note the lack of $
on first
, last
and pro
. In shell you do not use the $
on a variable when assigning it a value.
You need to change …
read -p "First Name: " $first
read -p "Last Name: " $last
read -p "profession 1 for software and 2 for hardware (1/2): " $pro
… to …
read -p "First Name: " first
read -p "Last Name: " last
read -p "profession 1 for software and 2 for hardware (1/2): " pro
Note the lack of $
on first
, last
and pro
. In shell you do not use the $
on a variable when assigning it a value.
answered Nov 11 at 7:51
Red Cricket
4,24483382
4,24483382
Further explanation: you use$
to get the value of a variable, but not to set it. E.g.read varname
,varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g.echo "$pro"
instead of justecho $pro
.
– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
More information (and probably more information than you want): iffirst
isn't set, thenread $first
is equivalent toread
, which implicitly uses the variableREPLY
to store the input. Iffirst
is set, then the expansion produces one (or possibly more) words to use as variables. For instance,first=foo; read $first
sets the variablefoo
.first="foo bar"; read $first
would set the variablesfoo
andbar
after performing word-splitting on the input. All of this is to say, it's not wrong to useread $first
; it just doesn't do what you expected. :)
– chepner
Nov 11 at 20:18
add a comment |
Further explanation: you use$
to get the value of a variable, but not to set it. E.g.read varname
,varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g.echo "$pro"
instead of justecho $pro
.
– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
More information (and probably more information than you want): iffirst
isn't set, thenread $first
is equivalent toread
, which implicitly uses the variableREPLY
to store the input. Iffirst
is set, then the expansion produces one (or possibly more) words to use as variables. For instance,first=foo; read $first
sets the variablefoo
.first="foo bar"; read $first
would set the variablesfoo
andbar
after performing word-splitting on the input. All of this is to say, it's not wrong to useread $first
; it just doesn't do what you expected. :)
– chepner
Nov 11 at 20:18
Further explanation: you use
$
to get the value of a variable, but not to set it. E.g. read varname
, varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g. echo "$pro"
instead of just echo $pro
.– Gordon Davisson
Nov 11 at 8:53
Further explanation: you use
$
to get the value of a variable, but not to set it. E.g. read varname
, varname=value
, etc. BTW, you should also use double-quotes around variable references, e.g. echo "$pro"
instead of just echo $pro
.– Gordon Davisson
Nov 11 at 8:53
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
It worked, and thank you so much for the explanation...
– vamsi
Nov 11 at 9:26
1
1
More information (and probably more information than you want): if
first
isn't set, then read $first
is equivalent to read
, which implicitly uses the variable REPLY
to store the input. If first
is set, then the expansion produces one (or possibly more) words to use as variables. For instance, first=foo; read $first
sets the variable foo
. first="foo bar"; read $first
would set the variables foo
and bar
after performing word-splitting on the input. All of this is to say, it's not wrong to use read $first
; it just doesn't do what you expected. :)– chepner
Nov 11 at 20:18
More information (and probably more information than you want): if
first
isn't set, then read $first
is equivalent to read
, which implicitly uses the variable REPLY
to store the input. If first
is set, then the expansion produces one (or possibly more) words to use as variables. For instance, first=foo; read $first
sets the variable foo
. first="foo bar"; read $first
would set the variables foo
and bar
after performing word-splitting on the input. All of this is to say, it's not wrong to use read $first
; it just doesn't do what you expected. :)– chepner
Nov 11 at 20:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53246779%2fconditions-statement-gets-skipped%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
2
Please take a look: shellcheck.net
– Cyrus
Nov 11 at 7:47
you need to pass the variable name to
read
without a dollar sign.– oguzismail
Nov 11 at 7:48