0

never done shell script so seeking some guidance here. I have to write a script in which user would have list of options i.e.,

1. Upwork
2. Fiverr
3. FindAcoder

Now based on what user selects, I have to find configurations (which are stored in different file) for that option. The configurations are stored like this (can't change the format)

config file

<url_without_http>:<username>:<password>

The url_without_http will always have the string that is part of the menu (or i can change the menu to do so).

How can i create the menu, then take the put from uer in a variable and then use that variable to do a regex search in accounts.config file?

2 Answers 2

1

Use select to create a simple menu in bash.

#! /bin/bash
config=/path/to/config.cfg

select keyword in $(cut -d: -f1 "$config") ; do
    if [[ $keyword ]] ; then
        line=$(grep "$keyword.*:.*:" "$config")
        break
    fi
done

url=${line%%:*}
passwd=${line##*:}
user=${line%:*}
user=${user#*:}

echo "URL:      $url"
echo "User:     $user"
echo "Password: $passwd"

Tested against:

google.com:googler:g00gle
youtube.com:youtuber:u2b
stackoverflow.com:developer:L337
Sign up to request clarification or add additional context in comments.

3 Comments

would be safer to do mapfile -t choices < <(cut -d: -f1 "$config"); select keyword in "${choices[@]}"; ... -- otherwise you're at the mercy of teh contents of the config file.
Yes. Storing the password in the config is another thing that could be changed :-)
how would i use mapfile command in the script and why using mapfile is better?
0

Assuming the menu could have a variable list of items we can let select build the menu for us.

Sample data:

$ cat accounts.config
www.upwork.com:user1:password1
something.fIvErr.org:user22:password22

One idea using select:

$ cat menu.bash
#!/usr/bin/bash

menu_list='Upwork Fiverr FindACoder'
PS3='Select number: '                    # prompt displayed by 'select' command

select option in ${menu_list}
do
    break                                # we don't want an infinite loop that we need to ^C out of
done

echo "Menu number selected: ${REPLY}"
echo "Menu option selected: ${option}"

echo "++++++++++ matching config entry(s):"

grep -i "${option}" accounts.config

Sample runs:

$ menu.bash
1) Upwork
2) Fiverr
3) FindACoder
Select number: 1
Menu number selected: 1
Menu option selected: Upwork
++++++++++ matching config entry(s):
www.upwork.com:user1:password1

$ menu.bash
1) Upwork
2) Fiverr
3) FindACoder
Select number: 2
Menu number selected: 2
Menu option selected: Fiverr
++++++++++ matching config entry(s):
something.fIvErr.org:user22:password22

$ menu.bash
1) Upwork
2) Fiverr
3) FindACoder
Select number: 3
Menu number selected: 3
Menu option selected: FindACoder
++++++++++ matching config entry(s):

OP can add more code to address user picking a non-menu item (eg, 9), parsing the results from the grep, what to do if there's no match in the config file, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.