Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk script

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
I am writing a script at the moment that prints an option menu for the user, when the user hits a number, they call a kshell script... like the ( not working ) example below, can somebody help me

awk '
BEGIN {
printf("Enter Number: ")
}
$1 ~ /^[0-9]+$/ {
number = $1

if (number==0)
./script - this is where I am having problems

}

Thanks
 
.... and exactly why can't you do it all in ksh?

In awk do:
system(&quot;<path2script>&quot;); vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi stu78,

I am sending along the following script. It's something that I did a long time ago. I think I took the idea from either Effective Awk Programming by Arnold Robbins, or O'Reilly's Sed & Awk by Dale Dougherty and Arnold Robbins, but I am not sure.

The code is for an easy-to-implement menu that takes its menu options (along with associated commands) from an ascii file. An example of the ascii file will be shown further below.


The script:

% cat menu.awk
#!/usr/bin/awk -f

function ShowOptions()
{
#system(&quot;clear&quot;);
printf(&quot;\n\n%s\n&quot;,MenuName);
for (i=1; i<=Elements; ++i)
{
printf(&quot;%d. %s\n&quot;,i,Options);
}
printf(&quot;%d. Quit\n&quot;, i);
printf(&quot;Select: &quot;);
}



BEGIN {
UtilDir=&quot;/410&quot;;
OptionList=UtilDir &quot;/.util/MenuCommands&quot;;

if(ARGC>1)
{
printf(&quot;%s\n&quot;,ARGV[1]);
CommandStr=UtilDir &quot;/.util/&quot; ARGV[1];
for(i=2; i<ARGC; i++)
{
CommandStr=CommandStr &quot; &quot; ARGV;
}
system(CommandStr);
exit;
}


FS=&quot;|&quot;;
if (( getline < OptionList ) > 0)
{
MenuName=$1;
}
else
{
exit 1;
}

while (( getline < OptionList ) > 0)
{
Elements++;
Options[Elements] = $1;
UnixCommand[Elements] = $2;
}
ShowOptions();
}

{
if ($1>0 && $1<=Elements)
{
do
{
printf(&quot;\n\n===============================================================\n&quot;);
printf(&quot; %s: \n&quot;, UnixCommand[$1]);
printf(&quot;===============================================================\n&quot;);
system(UnixCommand[$1]);
printf(&quot;\n[Hit enter to return to the main menu, or 'R' to repeat.] &quot;);
getline var < &quot;-&quot;;
}
while(toupper(var)==&quot;R&quot;)
#exit;
}
else
{
exit;
}

ShowOptions();
}






The ascii file (menu label | associated commands):
% cat MenuCommands
MY GENERAL PURPOSE MENU:
Create text file from sequential file | /410/.util/maketext -f /410/.util/lists
Search for variables assignments | /410/.util/findassign -f /410/.util/lists
Search for an exact string | /410/.util/findstring -f /410/.util/lists
Search using regular expressions | /410/.util/findreg -f /410/.util/lists
Make GOTO's and GOSUB's explicit in code | /410/.util/flow -f /410/.util/lists
See lines where files changed | /410/.util/filechanges -f /410/.util/lists
Shell out to source code directories (submenu) | /410/.util/dir
Ksh | ksh



Explanations and instructions:

Set up the necessary path info:
On our system the above menu script is in one directory (on a path everybody can access), but ascii file containing the menu options (and their associated commands) is in another directory.

/usr/opt/util/menu.awk
/410/.util/MenuCommands

We mostly use it to call other awk scripts, which are in /410/.util.

So, you will need to change to following lines to reflect where you will be putting the menu commands file.

In the BEGIN{} section:
UtilDir=&quot;/410&quot;;
OptionList=UtilDir &quot;/.util/MenuCommands&quot;;


Also, this script gives users the ability to call the scripts that are in the directory that is not on their path, but first calling the menu script, then passing the name of script they want to run as an argument. When they do this, the menu doesn't appear -- it just runs the script that they requested. For this, you will need to modify:
CommandStr=UtilDir &quot;/.util/&quot; ARGV[1];



Set up the menu commands file:
In the directory you specified above, place the MenuCommands file. You can put a Title at the top, then for each menu item, add a separate line following the format:
'menu option label | unix command'


The sample MenuCommands file in the example above is mostly calling awk scripts (which won't work for you because you don't have access to those scripts), but you could modify it call any kind of script you want -- ksh scripts, for example. The 'ksh' menu option in the sample file should work for you as is.


Run it:
USAGE: menu.awk [scriptname]

If you include the [scriptname] option, the menu does not appear -- it just runs the requested script. In this case, menu.awk is really just helping the users access a script that is otherwise outside of their path. You may not ever want to use this option.



It's been a few years since I wrote it, but you it's quite flexible. With a little fiddling, you can call submenu's etc.

Hope it helps (and doesn't scare you),
Grant.


 
Hi stu78,

By the way, here is an example of what the menu looks like:

MY GENERAL PURPOSE MENU:
1. Create text file from sequential file
2. Search for variables assignments
3. Search for an exact string
4. Search using regular expressions
5. Make GOTO's and GOSUB's explicit in code
6. See lines where files changed
7. Shell out to source code directories (submenu)
8. Ksh
9. Quit

Grant.
 
Hi stu78,

Actually, I left out the 'Select:' prompt. It should be:


MY GENERAL PURPOSE MENU:
1. Create text file from sequential file
2. Search for variables assignments
3. Search for an exact string
4. Search using regular expressions
5. Make GOTO's and GOSUB's explicit in code
6. See lines where files changed
7. Shell out to source code directories (submenu)
8. Ksh
9. Quit
Select:



Grant.


Select:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top