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"
1) dir1
2) dir2
3)
4) Return to previous menu
5) Quit
Select the directory sub path or option required :
select option 1 and we get :
Code:
The following scripts are in this level (/export/home/aspsys/scripts/andy/testdir1/dir1) :
script1.sh
script2.sh
1) Run scripts listed in this directory
2) Return to previous menu
3) Quit
Select the directory sub path or option required :
Its the blank option in the first iteration that I cant get, maybe I need just a single concatenated option in the select command but when I try it it seperates the options with spaces in into seperate selects...
Thanks but didn't help pinpoint what quotes I need around which variables..
The problem (as I see it, perhaps I'm looking at it wrong) is that I have say for example for variables being passed in the select command, one is a list one is a single word string, one is a string with spaces and one is either a string with spaces or nothing.
The list is done by having $var, the string is done by having "$var", however having "$var" means that a blank string is parsed if it contains nothing which select parses as a blank option... I think.
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)