Hi guys, i would like to create a program that allow user to show the information of certain thing such as network card.
I would like the menu to be dynamic, for example:
my computer system have 2 network card inserted at the moment, therefore the menu will have 2 choice for the user.
eth0
lo
However, if there's changes made, for example another network card was added which name lo1. The program will update the changes and allow user to have the latest choice.
eth0
lo
lo1
I am up to here at the moment. guide me guys..
#!/bin/bash
clear
ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p' > somefile
array=($(<somefile))
whiptail --title Networking --menu "select your choice" 16 78 5 "${array[@]}"
Regards,
Malfolozy
What's your system? There may be more convenient ways than ifconfig to get device names.
Dumping data into arrays is often -- usually -- not the answer.
BASH -- and most modern Bourne shells -- has a built-in automatic menu, which acts like a while loop:
select X in a b c d q
do
echo "You chose $X"
[ "$X" = "q" ] && break
done
Hi Corona668,
My system is Linux, Red hat.
I am working on the Whiptail script, not Select. Therefore, its kinda hard to make a dynamic menu as a beginner. Guide me pls,
Thanks,
Malfolozy
Hi try:
array=($(ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1 \1/p') )
You need two strings per item..
Hello Scrutinizer,
After i try it in my code,
the output are as such
Is there anw to remove the duplicate into something else like number for the choices?
Thanks,
Malfolozy
You could try:
array=($(ifconfig -a | awk '{print $1, "Interface_"NR}' RS= ) )
There are many possibilities
Yay! The first code works!!
After that, how do i go about doing the code for the choices?
I forward the choice chosen by the user to a file "somefile" by using this
2>somefile
and this is the code for choice that i want it to be, how do i go about doing this? the chosen network card part.
CHOICE=$(cat somefile)
case $CHOICE in
"$(cat somefile)") echo $(ifconfig (chosen networkcard))
esac
Thanks,
Malfolozy
I would use the number in the second column so that the value that you obtain is the name of the interface (and so then you do not need a case statement).. Otherwise you just get a number and how would you relate that to the interface?
Hmm, i don't get what you mean, sry my english is bad.
So, i went to try something like this,
CHOICE=$(cat somefile)
case $CHOICE in
"$(cat somefile)") whiptail --msgbox "$(ifconfig $1)" 20 78 5 ;;
esac
and the output turn out to be it show all the network card info.
There are two fields for every menu item in whiptail. The first field is the choice that will be returned after selection, the second field is just a description..
So then you could just use:
whiptail --msgbox "$(ifconfig $CHOICE)" 20 78 5
and you do not need a case statement..
Hello,
It doesn't work, because my CHOICE read the first field which is "1." instead of "eth0".
I use
2>somefile
and it read the first field.
How do i go about doing it so that it read the second field?
That is because 1. is in the first field, you should use it in the second field, see my example in post #6 .
Ahh!!!! It works!
So, the only way to do it is to change the position. got it!!
Thanks a lot for guiding me through!! Appreciate it!!
Thanks,
Malfolozy