create menu with x items depending on variable
I am trying to get a menu with x lines depending on a variable.
setup:
i have a network with x ip addresses
i want a user to get a menu where he can simply choose an ip address by entering a number:
for instance:
i have 4 ip adresses
ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4
i want the user to get a menu like
1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:
however, the number of ip addresses can variate.
I am trying to do this in a case:
echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt
1) echo ${!ipnum}
esac
but i cant get it into a loop
shell-script
add a comment |
I am trying to get a menu with x lines depending on a variable.
setup:
i have a network with x ip addresses
i want a user to get a menu where he can simply choose an ip address by entering a number:
for instance:
i have 4 ip adresses
ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4
i want the user to get a menu like
1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:
however, the number of ip addresses can variate.
I am trying to do this in a case:
echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt
1) echo ${!ipnum}
esac
but i cant get it into a loop
shell-script
Any specific reason for using thecase
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g.for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01
add a comment |
I am trying to get a menu with x lines depending on a variable.
setup:
i have a network with x ip addresses
i want a user to get a menu where he can simply choose an ip address by entering a number:
for instance:
i have 4 ip adresses
ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4
i want the user to get a menu like
1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:
however, the number of ip addresses can variate.
I am trying to do this in a case:
echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt
1) echo ${!ipnum}
esac
but i cant get it into a loop
shell-script
I am trying to get a menu with x lines depending on a variable.
setup:
i have a network with x ip addresses
i want a user to get a menu where he can simply choose an ip address by entering a number:
for instance:
i have 4 ip adresses
ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4
i want the user to get a menu like
1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:
however, the number of ip addresses can variate.
I am trying to do this in a case:
echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt
1) echo ${!ipnum}
esac
but i cant get it into a loop
shell-script
shell-script
edited Dec 11 at 11:13
Ipor Sircer
10.5k11024
10.5k11024
asked Dec 11 at 10:47
WingZero
385
385
Any specific reason for using thecase
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g.for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01
add a comment |
Any specific reason for using thecase
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g.for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01
Any specific reason for using the
case
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
Any specific reason for using the
case
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01
add a comment |
2 Answers
2
active
oldest
votes
In case you are using bash
(or a similar shell):
ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )
select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done
printf 'You picked IP %sn' "$ip"
This would display a menu and a prompt like this:
1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?
The prompt may be changed by setting PS3
to the prompt string to use.
The select
loop verifies the input and loops until a valid input is had from the user.
After the loop, the IP address that was chosen is available in $ip
.
If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):
select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done
add a comment |
In my case i figured it out.
like @fra-san said why use case and not just print it and ask for the users choice.
i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .
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%2f487310%2fcreate-menu-with-x-items-depending-on-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In case you are using bash
(or a similar shell):
ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )
select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done
printf 'You picked IP %sn' "$ip"
This would display a menu and a prompt like this:
1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?
The prompt may be changed by setting PS3
to the prompt string to use.
The select
loop verifies the input and loops until a valid input is had from the user.
After the loop, the IP address that was chosen is available in $ip
.
If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):
select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done
add a comment |
In case you are using bash
(or a similar shell):
ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )
select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done
printf 'You picked IP %sn' "$ip"
This would display a menu and a prompt like this:
1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?
The prompt may be changed by setting PS3
to the prompt string to use.
The select
loop verifies the input and loops until a valid input is had from the user.
After the loop, the IP address that was chosen is available in $ip
.
If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):
select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done
add a comment |
In case you are using bash
(or a similar shell):
ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )
select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done
printf 'You picked IP %sn' "$ip"
This would display a menu and a prompt like this:
1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?
The prompt may be changed by setting PS3
to the prompt string to use.
The select
loop verifies the input and loops until a valid input is had from the user.
After the loop, the IP address that was chosen is available in $ip
.
If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):
select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done
In case you are using bash
(or a similar shell):
ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )
select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done
printf 'You picked IP %sn' "$ip"
This would display a menu and a prompt like this:
1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?
The prompt may be changed by setting PS3
to the prompt string to use.
The select
loop verifies the input and loops until a valid input is had from the user.
After the loop, the IP address that was chosen is available in $ip
.
If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):
select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done
edited Dec 11 at 13:09
answered Dec 11 at 12:55
Kusalananda
121k16228372
121k16228372
add a comment |
add a comment |
In my case i figured it out.
like @fra-san said why use case and not just print it and ask for the users choice.
i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .
add a comment |
In my case i figured it out.
like @fra-san said why use case and not just print it and ask for the users choice.
i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .
add a comment |
In my case i figured it out.
like @fra-san said why use case and not just print it and ask for the users choice.
i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .
In my case i figured it out.
like @fra-san said why use case and not just print it and ask for the users choice.
i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .
answered Dec 11 at 12:10
WingZero
385
385
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%2f487310%2fcreate-menu-with-x-items-depending-on-variable%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
Any specific reason for using the
case
compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g.for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39
I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01