1834209 Members
2235 Online
110066 Solutions
New Discussion

Menu script help

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

Menu script help

At one time, I thought I remembered someone referencing a command on this forum that could output a list into columns. For instance, if I had a file listing our servers and wanted to create a menu for the helpdesk, how could I output

server1
server2
server3
server4
server5
server6

into:
##########################################
Which Server would you like to administer?
server1 server2 server3
server4 server5 server6
##########################################

or:
##########################################
Which Server would you like to administer?
server1 server3 server5
server2 server4 server6
##########################################

Thanks again,
Greg
5 REPLIES 5
John Strang
Regular Advisor
Solution

Re: Menu script help

Hi Greg,

You can use paste. For your example file of

server1
server2
server3
server4
server5
server6

typing
cat file | paste - - -

will output
server1 server2 server3
server4 server5 server6

Note you use 1 "-" for each column required

Hope this is helpful,

John
If you never make a mistake you'll never make anything.
James R. Ferguson
Acclaimed Contributor

Re: Menu script help

Hi Greg:

You can use 'paste':

Consider this:

# echo "1\n2\n3\n4\n5\n6"| paste - - -

...will return:

1 2 3
4 5 6

Regards!

...JRF...
Rita C Workman
Honored Contributor

Re: Menu script help

Hi Greg,

What you want to do is write a script using a case statement. Within your case statement you can define with maybe some if statements on the response what tasks you want done; then you close your case statement with esacs and done. I generally preface mine with a trap statement that as long as the result is true...run my case statement otherwise exit.

You will notice I did not write the script for you...if you would like to begin the script I'll be happy to edit and make suggestions....BUT you will never learn to write scripts if you don't start.

Looking forward to assisting you,
Rit
Ian Dennison_1
Honored Contributor

Re: Menu script help

Why not use the mighty 'awk'?

# aaa = File name of list of servers
# linelimit = words per line
cat /tmp/aaa |
awk -v linelimit=3 'BEGIN { cntr=0 } { cntr=cntr+1; printf("%s ", $1) }
cntr == linelimit { printf("\n"); cntr=0 } END { print }'

This does not handle the one scenario when the number of items is even divisible by the linelimit, which will produce a blank line at the end.

Share and enjoy! Ian
Building a dumber user
Rob Galloway_1
Frequent Advisor

Re: Menu script help

Use the 'pr' command which is designed for this job

e.g.

echo "which server would you ...."
echo "*......"
pr -3t yourfilename


Where the file 'yourfilename' is a list of the items you want in the menu and -3 is the number of columns you want i.e -5 would list this in 5 columns.

This deals with different length strings & strings with spaces in them quite gracefully.

Hope this helps.
Rob.
Experience is a hard teacher. It tests first and teaches afterward.