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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with functions

Status
Not open for further replies.

smithia6

Technical User
Aug 28, 2002
35
US
Hello I wonder if someone could help me with a korn script.
I am new to Unix Shell scripting and have only been doing it for about a week.
I want to tidy up a menu script and looking at functions.
Could anyone give me any pointers.
regards
Ian.

#!/bin/ksh

# Set system variables

AgentsData=/tmp/agentsData.$$
AgentsRead=/tmp/agentsRead.$$
DiffFile=/tmp/difffile.$$
AllAgents=/export/home/nz9jyp/test/allagents

clear
echo ""
echo "\\t \\t *******************************************"
echo "\\t \\t * Please select an option *"
echo "\\t \\t *******************************************"
echo
echo "\\t \\t a - Show Running Pega Agents"
echo "\\t \\t b - Show None Running Agents"
echo "\\t \\t c - Count Number Of Running Pega Agents"
echo "\\t \\t d - Count Number Of None Running Pega Agents"
echo "\\t \\t e - Start a Pega Agent"
echo "\\t \\t q - Quit"
echo ""
echo "\\t \\t Select an option \\c"
read ans

case $ans in
a) clear

echo "\\t RUNNING A SEARCH ON ALL RUNNING PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11,$2,$3,$5}' | tr -s ' ' ' ' > $AgentsRead
echo "\\t All PEGA AGENTS" > $AgentsRead
echo "" >> $AgentsRead
echo "AGENT PID PPID TIME STATUS" >>$AgentsRead
echo "____ ___ ____ ____ ______" >>$AgentsRead
cat $AgentsData | awk '{print $11,$2,$3,$5}' | tr -s ' ' ' ' |sort |sed 's/$/ Status Running/' >> $AgentsRead
more $AgentsRead
rm $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
$0;;

b) clear
echo "\\t RUNNING A SEARCH ON ALL PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11}' |sort > $DiffFile
echo "\\t All NONE RUNNING PEGA AGENTS"
echo ""
diff $DiffFile $AllAgents |grep ">" |cut -d " " -f2 |sed 's/$/ Not Running/' >> $AgentsRead
AgentsRead=$(more $AgentsRead)
echo "\\t" $AgentsRead
rm $DiffFile $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
$0;;

c) clear
echo "\\t RUNNING A SEARCH ON ALL RUNNING PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

AgentsCount=$(more $AgentsData |wc -l)
echo "\\t There are" $AgentsCount "Pega Agents Running Out Of An Expected 39 Agents"
rm $AgentsData
echo ""
echo "\\t Hit Return to go back to the main menu"
read
$0;;

d) clear
echo "\\t RUNNING A SEARCH ON ALL PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11}' |sort > $DiffFile
diff $DiffFile $AllAgents |grep ">" |cut -d " " -f2 |sed 's/$/ Not Running/' >> $AgentsRead
AgentsRead=$(more $AgentsRead |wc -l)
echo "\\t There is" $AgentsRead "Pega Agent(s) not running out of the expected 39 Agents"
rm $DiffFile $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
$0;;
e) clear
echo "\\t \\t *******************************************"
echo "\\t \\t * *"
echo "\\t \\t * Pega Support (Start A Pega Agent) *"
echo "\\t \\t * *"
echo "\\t \\t *******************************************"
echo ""
echo "\\t \\t Enter the Pega Agent Name You Wish To Start \\c"

read imput
echo "Starting The Pega Agent ...."
echo ""
/app/pegatst/zbin/zpddrvr ^yas2^ $imput
sleep 5
ps -ef |grep $imput
echo ""
echo "\\t Hit Return to go back to the main menu"
read
$0;;

q) echo ""; echo "\\t \\t Quitting the Pega Agents menu program ...." ; sleep 1; echo "" ; exit;;
*) echo \\t \\t \\t \\t ^ is not a valid option; sleep 1; $0;;
esac
 
Your existing script needs to split the separate parts into functions. They must come first in the script. Your menu part then calls each function as required.

E.G

#!/bin/ksh

# Set system variables

AgentsData=/tmp/agentsData.$$
AgentsRead=/tmp/agentsRead.$$
DiffFile=/tmp/difffile.$$
AllAgents=/export/home/nz9jyp/test/allagents

function yefirst
{

echo "\\t RUNNING A SEARCH ON ALL RUNNING PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11,$2,$3,$5}' | tr -s ' ' ' ' > $AgentsRead
echo "\\t All PEGA AGENTS" > $AgentsRead
echo "" >> $AgentsRead
echo "AGENT PID PPID TIME STATUS" >>$AgentsRead
echo "____ ___ ____ ____ ______" >>$AgentsRead
cat $AgentsData | awk '{print $11,$2,$3,$5}' | tr -s ' ' ' ' |sort |sed 's/$/ Status Running/' >> $AgentsRead
more $AgentsRead
rm $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
}
#End of 1st function

function yesecond
{

echo "\\t RUNNING A SEARCH ON ALL PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11}' |sort > $DiffFile
echo "\\t All NONE RUNNING PEGA AGENTS"
echo ""
diff $DiffFile $AllAgents |grep ">" |cut -d " " -f2 |sed 's/$/ Not Running/' >> $AgentsRead
AgentsRead=$(more $AgentsRead)
echo "\\t" $AgentsRead
rm $DiffFile $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
}
#End of 2nd function

function yethird
{

echo "\\t RUNNING A SEARCH ON ALL RUNNING PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

AgentsCount=$(more $AgentsData |wc -l)
echo "\\t There are" $AgentsCount "Pega Agents Running Out Of An Expected 39 Agents"
rm $AgentsData
echo ""
echo "\\t Hit Return to go back to the main menu"
read
}
#End of 3rd function

function yeforth
{

echo "\\t RUNNING A SEARCH ON ALL PEGA AGENTS PLEASE WAIT......"
sleep 1
echo ""
ps -ef | grep zasmain |egrep -v grep |grep -v edsuat > $AgentsData

# Now for the results screen

cat $AgentsData | awk '{print $11}' |sort > $DiffFile
diff $DiffFile $AllAgents |grep ">" |cut -d " " -f2 |sed 's/$/ Not Running/' >> $AgentsRead
AgentsRead=$(more $AgentsRead |wc -l)
echo "\\t There is" $AgentsRead "Pega Agent(s) not running out of the expected 39 Agents"
rm $DiffFile $AgentsData $AgentsRead
echo ""
echo "\\t Hit Return to go back to the main menu"
read
}
#End of 4th function

function yefifth
{

echo "\\t \\t *******************************************"
echo "\\t \\t * *"
echo "\\t \\t * Pega Support (Start A Pega Agent) *"
echo "\\t \\t * *"
echo "\\t \\t *******************************************"
echo ""
echo "\\t \\t Enter the Pega Agent Name You Wish To Start \\c"

read imput
echo "Starting The Pega Agent ...."
echo ""
/app/pegatst/zbin/zpddrvr ^yas2^ $imput
sleep 5
ps -ef |grep $imput
echo ""
echo "\\t Hit Return to go back to the main menu"
read
}
#End of 5th function

#Main part
clear
echo ""
echo "\\t \\t *******************************************"
echo "\\t \\t * Please select an option *"
echo "\\t \\t *******************************************"
echo
echo "\\t \\t a - Show Running Pega Agents"
echo "\\t \\t b - Show None Running Agents"
echo "\\t \\t c - Count Number Of Running Pega Agents"
echo "\\t \\t d - Count Number Of None Running Pega Agents"
echo "\\t \\t e - Start a Pega Agent"
echo "\\t \\t q - Quit"
echo ""
echo "\\t \\t Select an option \\c"
read ans
clear
case $ans in
a) yefirst
;;
b) yesecond
;;
c) yethird
;;
d) yeforth
;;
e) yefifth
;;
q) echo ""; echo "\\t \\t Quitting the Pega Agents menu program ...." ; sleep 1; echo "" ; exit;;
*) echo \\t \\t \\t \\t ^ is not a valid option; sleep 1; $0;;
esac

You can pass parameters too - eg
d) yeforth(day,month,year,name)

and ... function(dd,mm,yy,username)
{
etc etc

HTH
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Thanks Dickie,

That was very helpful

Regards

Ian
 
Ian:

Not only can you pass parameters, but you can return values from functions. See thread:

thread822-311461

Regards,


Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top