Sponsored Content
Top Forums Shell Programming and Scripting Select command with variable options having spaces or null contents Post 302794119 by andyatit on Monday 15th of April 2013 08:27:13 AM
Old 04-15-2013
Select command with variable options having spaces or null contents

Hi,

I'm having an issue trying to produce a hierarchical directory menu that has either any directories listed in a specific directory as options or options with spaces in the option content or null content.

So the menu function gets passed a base directory, it then lists any .sh scripts in that directory or any other sub directories.

A select command lists the directories as seperate options, if there any shell scripts then an option "Run scripts in this dir" is given.

My problem is that if there are no shell scripts it gives a blank option in the select command, also a blank if there are no sub directories.

Code:
#!/usr/bin/ksh
 
. $HOME/.profile
cd $HOME
 
function menu {
thisDir=$1
opt1=""
opt2=""
menuDone=false
PS3="   Select the directory sub path or option required : "
 
while [[ $menuDone = false ]];do
 
  if ls -l $thisDir|grep "^d" >/dev/null;then
    opt1=`ls -l ${thisDir}|grep "^d"|awk '{print $9}'`
  fi
 
  if [[ -f $thisDir/*.sh ]];then
    opt2="Run scripts listed in this directory"
  fi
 
  opt3="Return to previous menu"
  opt4="Quit"
 
  select choice in "$opt1" "$opt2" "$opt3" "$opt4";do
        case $choice in
                     "$opt2" ) runThisDirScripts
                               break ;;
                     "$opt3" ) return ;;
          q|Q|quit|Quit|QUIT ) exit ;;
                          '' ) print "\n\n  Invalid Option !"
                               sleep 3
                               break ;;
                           * ) if [[ ! -z ${REPLY} ]];then
                                 menu $thisDir/$choice
                                 break
                               fi ;;
        esac
        print $choice
  done
done
}
 
#-- MAIN
dir=/export/home/aspsys/deploy/upgrade/configBackend
menu "$dir"

---------- Post updated at 12:55 PM ---------- Previous update was at 12:51 PM ----------

sorry, that line with REPLY should read $choice

---------- Post updated at 01:27 PM ---------- Previous update was at 12:55 PM ----------

I made some changes but still have the blank options problem

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
  opt1=""
  opt2=""
  if ls -l $thisDir|grep "^d" >/dev/null;then
    opt1=`ls -l ${thisDir}|grep "^d"|awk '{print $9}'`
  fi
  if [ -f $thisDir/*.sh ];then
    opt2="Run scripts listed in this directory"
    print "\n\n  The following scripts are in this level (${thisDir}) :\n"
    for file in `ls ${thisDir}/*.sh`;do
      echo "  `basename ${file}`"
    done
    echo
  fi
  opt3="Return to previous menu"
  opt4="Quit"
  select choice in "$opt1" "$opt2" "$opt3" "$opt4";do
        case $choice in
                     "$opt2" ) runThisDirScripts
                               break ;;
                     "$opt3" ) 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/deploy/upgrade/configBackend
menu "$dir"

 

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 05:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy