Solved

Need Part of a Bash Script take input/generate array of menu choices/take user selection/appoint value...

Posted on 2009-04-08
5
474 Views
Last Modified: 2013-12-26
I have inherited a task to complete a deployment script -

It needs to query the user for a string in order to narrow down the choices of directories that were ftp or scp up to the server.

Based on the user input a partial directory list is returned of a local directory with the matching string request - this partial (or say full return if a very broad search (like just the letter a for example) needs to return to the user as input choices - aka like

1 - directory1
2 - directory2
3 - directory3

the array choices also need to be able to handle spaces and linux allowable special characters in the name


0
Comment
Question by:prestonhill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:letharion
ID: 24096555
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 24097552
You can use the dialog utility if its available on your system. Most system's come pre-installed with it, but you can install it if its not already there. An alternative would be whiptail which is more lightweight.

Lets say the directories are in /home/upload. That is:

/home/upload/dir1
/home/upload/dir2

etc

Here's a code snippet from the dialog utility program's examples
#!/bin/sh
exec 3>&1
FILE=`dialog --title "Please choose a directory" --dselect /home/upload/ 14 48 2>&1 1>
&3`
code=$?
exec 3>&-
 
case $code in
  0)
    echo "\"$FILE\" chosen";;
  1)
    echo "Button 1 (Cancel) pressed";;
  2)
    echo "Button 2 (Help) pressed";;
  3)
    echo "Button 3 (Extra) pressed";;
  255)
    echo "Box closed.";;
esac

Open in new window

0
 

Author Comment

by:prestonhill
ID: 24097902
I was starting down the dialogue path - I need to get it installed on the RedHat system this script needs to run on though (not available but should not be a problem) - I am going to check out whiptail - I haven't looked at it before.
0
 
LVL 7

Accepted Solution

by:
Morne Lategan earned 500 total points
ID: 24098513
If you don't have dialog, how about:
#!/bin/bash
OLDIFS="$IFS"
IFS=$'\n'
done=no
validoptions=""
while [ "$done" != "yes" ]
do
        i=0
        clear
        echo "Please select a directory:"
        echo
        for DNAME in $(find . -maxdepth 1 -type d | sed "s?^./??" | grep -v "^\.")
        do
                i=$(($i + 1))
                opt[$i]="$DNAME"
                echo "$i. $DNAME"
        done
        echo -en "Select Option [ ]\b\b"
        read ans
        if [ "x$ans" \< "x1" -o "x$ans" \> "x$i" ]
        then
                echo "Invalid Option. Enter 1 to $i."
                echo "Press ENTER."
                read
        else
                done=yes
        fi
done
echo "You selected: \"${opt[ans]}\""
IFS="$OLDIFS"

Open in new window

0
 

Author Comment

by:prestonhill
ID: 24105970
Thanks uberpappa - that was enough of a bit of code to get me going on what I needed.
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question