Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create bash script menu with labels

I would like to create a bash script so I can select a server I can connect to using mosh.
Currently I have this small script that shows me the username and ip of the server.
Its difficult to remember what server does what so I would like to add some remark to the ip.

How could I modify it in such way that it also shows me a label or remark to the menu entry so its easier for me to identify the server.

Currently it looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash

PS3='Pick a server (or ctrl-c to quit): '
select item in \
"username@someip" \
"username@someip" \
"username@someip" \
"username@someip" \
"username@someip" \
"username@someip"
do
               mosh $item
exit 0
done

which creates the following menu:

1) username@someip  3) username@someip  5) username@someip
2) username@someip  4) username@someip  6) username@someip

how could I modify this script so it creates the following menu:

1) username@someip (some remark) 3) username@someip (some remark)   5) username@someip (some remark)
2) username@someip (some remark) 4) username@someip (some remark)   6) username@someip (some remark)

Thank you!

>Solution :

Learn (Parameter Expansion Rules)[https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html]

#!/bin/bash
PS3='Pick a server (or ctrl-c to quit): '
select item in "u1@ip1 (foo)" "u1@ip2 (bar)" "u2@ip1 (baz)" "u2@ip2 (foo bar)" 
do mosh "${item%% *}" # strips the unwanted part
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading