Sponsored Content
Top Forums Shell Programming and Scripting Select command with variable options having spaces or null contents Post 302794217 by andyatit on Monday 15th of April 2013 01:56:55 PM
Old 04-15-2013
ah, sorry, yes, I updated the code inbetween posting originally and trying to get it right

Now I have :

Code:
select choice in $opt1 "$opt2" "$opt3" "$opt4";do

which gives :

Code:
1) dir1
2) dir2
3)
4) Return to previous menu
5) Quit
   Select the directory sub path or option required :

but it still leaves the blank option.

I have tried also :

Code:
select choice in $opt1 $opt2 "$opt3" "$opt4";do

which does remove the blank option in the first iteration but then makes the 2nd iteration split out the string into seperate options :

Code:
1) dir1
2) dir2
3) Return to previous menu
4) Quit
   Select the directory sub path or option required : 1
 
 
  The following scripts are in this directory /export/home/aspsys/scripts/andy/testdir1/dir1) :
 
  script1.sh
  script2.sh
 
1) Run
2) scripts
3) listed
4) in
5) this
6) directory
7) Return to previous menu
8) Quit
   Select the directory sub path or option required :

---------- Post updated at 06:56 PM ---------- Previous update was at 05:36 PM ----------

I managed a solution using arrays instead :

Code:
#!/usr/bin/ksh
. $HOME/.profile
cd $HOME
function menu {
menuDone=false
PS3="   Select the directory sub path or option required : "
while [[ $menuDone = false ]];do
 
  clear
  thisDir=$1
  set -A menuList --
  idx=0
 
  for d in $(ls -l ${thisDir}|grep "^d"|awk '{print $9}');do
    menuList[idx]=$d
    idx=$(($idx+1))
  done
 
  if [ -f $thisDir/*.sh ];then
    menuList[idx]="Run scripts listed in this directory"
    idx=$(($idx+1))
    print "\n\n  The following scripts are in this directory (${thisDir}) :\n"
    for file in `ls ${thisDir}/*.sh`;do
      echo "  `basename ${file}`"
    done
    echo
  fi
 
  menuList[idx+1]="Return to previous menu"
  menuList[idx+2]="Quit"
 
  select choice in "${menuList[@]}";do
        case $choice in
           "Run scripts listed in this directory" ) runThisDirScripts
                                                    break ;;
                        "Return to previous menu" ) return ;;
                               q|Q|quit|Quit|QUIT ) exit ;;
                                               '' ) print "\n\n  Invalid Option !"
                                                    sleep 3
                                                    break ;;
                                                * ) if [[ ! -z $choice ]];then
                                                      menu "$thisDir/$choice"
                                                      break
                                                    fi ;;
        esac
        print $choice
  done
done
}
 
#-- MAIN
dir=/export/home/aspsys/scripts/andy/testdir1
menu "$dir"

I would interested in anything more elegant, it still looks a little clunky
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

adding contents of a variable to a command

hi all, i'm new to shell scripting, so i'm not sure how to work this. Is it possible to read in the contents of a variable and add it to a command? for example: ------------------------ #!/bin/sh set example = -dfr rm ${example} ------------------------ when i run the script, i want... (2 Replies)
Discussion started by: gammarays
2 Replies

2. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

3. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies

4. Shell Programming and Scripting

select contents between two delimiters (not working if newline in encountered)

Hi, I am facing difficulties in selecting the contents between two delimiters when there is a new line occurs.. Eg: >more sample.txt abcd -- this is the first line % efgh-- this is the second line and not able to print % ijkl -- this is the 3rd line % when i search for abcd and... (8 Replies)
Discussion started by: Balaji PK
8 Replies

5. Shell Programming and Scripting

Help setting variable from file contents with spaces

I suppose the easiest way to explain my problem is to show you some code and then show you what the code outputs: -------------------------------------------------- #!/bin/bash echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt" export Foo=$(cat "/tmp/This Filename Has... (4 Replies)
Discussion started by: nrogers64
4 Replies

6. Shell Programming and Scripting

Issue with spaces in Java command line options

Hi, I am writing a shell script to build Java options dynamically in a variable array and pass them to java.exe. If an option value contains a space, I cannot find a way to get it interpreted correctly. Here is my example: #!/bin/bash JAVA_HOME=/opt/jvm/jre1.5.0_18 JAVA_OPTS=("-Xms256m... (4 Replies)
Discussion started by: Romain
4 Replies

7. Shell Programming and Scripting

Select the exact matching contents using grep

Hi everyone I've two files.. The contents of file1 are as shown below 4 5 12 13 36 37 45 46 47 The contents of file2 are as shown below 21 hello 13 world (5 Replies)
Discussion started by: abk07
5 Replies

8. Shell Programming and Scripting

How do i select all contents between two numbers?

I want to select contents between two numbers say 1. and 2. from an output file which has many numbers but only the these two ending with a dot(.) eg 1. 2 . 32. etc I was looking to select with the use of a counter then modify the selected contents and put it in an output file where again the... (3 Replies)
Discussion started by: ausfragen
3 Replies

9. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

10. Shell Programming and Scripting

How Select numbers from a line of text, and remove leading spaces?

I have a text file with a line of text that contains numbers and text formatted into groups. I need to extract the number that can be either 1,2 or 3 digits long. Then write it to a variable, but i need to remove any leading spaces in the number first. I can get the numbers out but how to remove... (12 Replies)
Discussion started by: kcpoole
12 Replies
All times are GMT -4. The time now is 08:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy