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.
#!/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
#!/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"