Function is calling only once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function is calling only once
# 1  
Old 06-01-2013
Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly,
but for the second time entire script is not executing it just exiting
my code is
Code:
#!/bin/sh
checkpo()
{
echo "Checking the entered PO to create output text file ";
IFS=$'\n'
set -f
var=0
for i in  $(cat PO_NO.txt)
do
var=`expr $var + 1`
if [ ${#i} -gt 8 ]
then
echo "Error in PO,contains more than 8  char in line number $var with ${#i} characters";
#calling error function
error
elif [ ${#i} -lt 8 ]
then
echo "Erro in PO has less than 8 char in line number $var ";
#calling error function
error
elif [ ${#i} -eq 8 ]
then
echo $i > POF_NO.txt
fi
done
return
#echo " Text file containing PO created successfully ,check POF_NO.txt";
}
error()
{
echo "Do you want to enter PO again ";
echo "If yes press y else press n/N ";
read ANSWER;
if expr "$ANSWER" : "[yY]"
then
echo "Re enter the PO"
#calling main
main
checkpo
elif expr "$ANSWER" : "[nN]"
then
echo "Going to Exit";
exit
fi
return
}
main()
{
echo "Enter the PO's and type exit once you finished !!!!";
while read LINE
do
if [ "$LINE" != "exit" ];then
echo $LINE > PO_NO.txt
else
break 2
fi
done
return
}
#calling main for the first time
main
checkpo

Output is as below
Enter the PO's and type exit once you finished !!!!
1362786278
exit
Checking the entered PO to create output text file
Error in PO,contains more than 8 char in line number 1 with 10 characters
Do you want to enter PO again
If yes press y else press n/N
y
1
Re enter the PO
Enter the PO's and type exit once you finished !!!!
qdqw
exit

after reentering the whole process is not repeating,i dont know where i have commited mistake.Please help me.Thanks in advance.
# 2  
Old 06-01-2013
This is very convoluted code.

Is this a homework assignment?

What OS are you using? Is /bin/sh on your system a Bourne shell, a bash shell, a Korn shell, or something else? (If something else, what is it?)

What is the output from the commands:
Code:
uname
/bin/sh --version

# 3  
Old 06-01-2013
i executed the program under bash environment.it works fine for the 1st execution,but saving only the last input in output file.
# 4  
Old 06-01-2013
If that is all that is wrong, change:
Code:
echo $LINE > PO_NO.txt

to:
Code:
echo $LINE >> PO_NO.txt

and I won't bother looking at the rest of the code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

3. UNIX for Dummies Questions & Answers

Getting a error while calling a function

Hi Friends, While calling a function in below scipt func_serv_logs () { find . -type f \( \( -name 'WLS*' -o -name 'access*' \) -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print | while read FILENAME do echo "hi" done } func_serv_logs I am getting error... (4 Replies)
Discussion started by: Jcpratap
4 Replies

4. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

5. Shell Programming and Scripting

Calling Function in KSH

I have a script with 2 functions 1) show_menu 2) create Ths show_menu function works fine....... Sort of.... When I select option 2 of the menu the code does a few commands and then calls another function called create. It's at this point that I get "create: not found"..... However,... (2 Replies)
Discussion started by: hxman
2 Replies

6. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

9. Shell Programming and Scripting

Calling other file function

Hi, I am pretty new to unix. Lets say i have a program(run_program) that will call another file function(functiona, in same directory): hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.... (3 Replies)
Discussion started by: maldini
3 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies
Login or Register to Ask a Question