LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2015, 05:35 AM   #1
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Rep: Reputation: Disabled
Bash Shell Script : nested select case not working


Hi I am trying to get, the "back)" option in the 2nd select statment to work.

It does not react.


To give you and idea.

links.hist.txt contains http links.

e.g: http://www.google.com


Code:
#!/bin/bash
 
options=(link back quit)
 
PS3="Please select an option: "
 
select opt in "${options[@]}"
do
   case "$opt" in
        link)
        echo "Please enter the web address you wish to view, e.g: <http://www.google.com>"
        read address
        echo "$address" >> "$HOME"/links.hist.txt
        links -dump "$address"
        ;;
        back)
        hist=( $(head -n10 "$HOME"/links.hist.txt) quit )
        select histlist in "${hist[@]}"
                do
                case "$histlist" in
                 [1-10])
                 links -dump
                  ;;
                 quit)
                 exit
                   ;;
                 esac
        done
 
        #printf '%s\n' "${hist[@]}"
        links -dump "$address"
        ;;
        quit)
        echo "Thanks for using $0"
        exit 0
        ;;      
   esac
done

This is the section that does not work, I am trying to figure out what to place on the line:

Code:
links -dump "$histlist"
This does not choose the output from the 1-10 elements selected to be used in the next command for the -dump argument....



Code:
back)
        hist=( $(head -n10 "$HOME"/links.hist.txt) quit )
        select histlist in "${hist[@]}"
                do
                case "$histlist" in
                 [1-10])
                 links -dump "$histlist"
                  ;;
                 quit)
                 exit
Thanks for your time.
 
Old 04-26-2015, 07:20 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
    back)
        hist_last10=( $(head -n10 "$HOME"/links.hist.txt) quit )
        select hist_sel in "${hist_last10[@]}"
        do
            if [ "$hist_sel" = quit ]; then exit; fi
            links -dump "$hist_sel"
        done
        ;;
 
1 members found this post helpful.
Old 04-26-2015, 07:22 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would put set -xv at the beginning of the script and you will see what's happening.
 
Old 04-26-2015, 07:27 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Change [1-10]) to be default i.e. *

Code:
hist=( $(head -n10 links.hist.txt) quit )
        select histlist in "${hist[@]}"
                do
                case "$histlist" in
                  quit)
                      exit
                   ;;
                 *)
                   links -dump "$hist_sel"
                 ;;
                 esac
        done
FYI the number selected is saved to the variable $REPLY

Last edited by michaelk; 04-26-2015 at 07:38 AM.
 
Old 04-26-2015, 07:16 PM   #5
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Original Poster
Rep: Reputation: Disabled
Thanks everyone who replied, I tired NevemTeve's solution and it worked fine.

Thanks NevemTeve for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash: create random script with select case thecazz Programming 8 08-04-2013 12:29 PM
Help with bash select and case neocontrol Programming 1 01-03-2013 09:42 PM
[SOLVED] BASH Question - nested case statements jbeiter Linux - Software 2 02-13-2012 10:08 AM
Nested Bash Script downbound010 Programming 1 12-10-2005 02:37 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration