EXIT from a Program not from the function..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers EXIT from a Program not from the function..
# 1  
Old 09-03-2007
EXIT from a Program not from the function..

Hi,
I want a command in unix shell script which will exit my whole program, not only from the function it's using. For e.g:

$1 == "m_who" && $4 == "Extrnl Vendor" {
print "You don have access"

exit (0);
}


If this error is showing on the screen, then nothing should not proceed further. I want this program to stop here instead of going forward for next function or steps. Finally, I want this error on the screen.

I have tried the 'break' command too, but did not get any proper result.

Any command apart from exit and break, we can use ? Hope I am clear with my doubt.

Thanks,
Rohit..
# 2  
Old 09-03-2007
Please clarify what language (or, more likely, languages) you are using here. If you have a shell script that is invoking awk, you need to realize that awk is a child process. The parent process must decide when it wants to exit. Assuming ksh invoking an awk script, you could do something like:
if awk '{ ... }' ; then exit 0; fi
And in the awk script, use either:
exit(1) to get out of awk and cause the script to exit
exit(0) to get out of awk but allow the shell script to continue.
# 3  
Old 09-04-2007
Thanks for your response. Yes, this is an AWK program. But, after using exit(1), also I am not getting the result.. Here is the sample code :

(
cat <<!
HTTP/1.0 200
Server: TEST/1.0
Mime-version: 1.0
Content-type: text/html

!

exbin/gu -i -r 'm_who(user,group,role,name,addr,phone)' | nawk '

BEGIN {
system("echo Beginning userInfo >&2");
FS = "\t";
state = "waiting";
}

$1 == "m_who" && $4 == "External" {
print "You don have access"

exit (1);
}
'
) >&3
./exbin/mk_ht ./high/practice.ihtml

echo


After the error "You don have access" exists, I do not want it should execute the next line "./exbin/mk_ht ./high/practice.ihtml".

Both exit (0) and exit (1) are giving the same result. Please let me know if there is any other command for it.

Thanks,
Rohit..
# 4  
Old 09-04-2007
Please let me know if you need anymore information.

Last edited by ronix007; 09-04-2007 at 04:46 AM..
# 5  
Old 09-04-2007
Reread my post. I said to do:
if awk '{ ... }' ; then exit 0; fi
Somehow the shell script must test that exit code from awk and then decide to exit. The script you show is ignoring the exit code from awk.
# 6  
Old 09-04-2007
Could you please update the code and let me know ? I was trying with both exit (0) and exit (1), but still getting the same result. I do not know whether doing in the same way, you want me or not. So, Please modify my sample code in the way you want to explain me.

Is it possible to use if condition along with the executable file inside an awk program ? For e.g.

if ( ($1 == "m_who") && ($4 == "External")) {
print "You don have access"
exit(0)
};
else{
system("./exbin/mk_ht ./high/practice.ihtml")
}


Please reply soon ..

Thanks,
Rohit..

Last edited by ronix007; 09-04-2007 at 08:07 AM.. Reason: Some more details..
# 7  
Old 09-04-2007
Look at it this way... "true" does an "exit(0)" while "false" does an "exit(1)". Now consider this script:
Code:
$ cat one
#! /usr/bin/ksh

echo one
true
echo two
false
echo three
exit 0
$ ./one
one
two
three
$

Those exit codes did nothing to stop the script from running. This is because the script is ignoring the exit codes. To fix that, I must change the script to test the exit codes:
Code:
$ cat two
#! /usr/bin/ksh

echo one
if true ; then exit 0 ; fi
echo two
if false ; then exit 0 ; fi
echo three
exit 0
$ ./two
one
$

Now because I tested the exit codes, that exit code from "true" stopped the script from finishing. You have a nawk script embedded in a shell script. You must change you outer shell script to test the exit code from nawk. I don't have gu or mk_ht on my box (never even heard of them before) and I don't know what shell you're using so I cannot fix your code snippet.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Programming

exit function doubts

At many places in program I see exit() being called with 2 passed as parameter to it. exit(2); What does this 2 stands for? Can I get a list all such parameters to exit? (6 Replies)
Discussion started by: rupeshkp728
6 Replies

3. Shell Programming and Scripting

I dont want to exit the program by ctrl + c

Hey , guys I am new to shell programing ,, so need a help from you guys ... I have to write a shell script that accepts file name or directory name from the user if it is a directory then throw an error if it is a file then give the user two options . 1.overwrite the content 2.append the... (2 Replies)
Discussion started by: coolashu
2 Replies

4. Shell Programming and Scripting

Replacement of exit function

I am reading multiple numbers from user and getting its respective string value from the One.txt.But while putting this text value to the two.txt file , i want to check that if this string already there in the file , if the string already exists in the two.txt file .....the string will be ignored.... (1 Reply)
Discussion started by: jalpasoni
1 Replies

5. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

6. UNIX for Advanced & Expert Users

Cannot exit from a function?

Hi, Is there a way to exit from a subcommand, which is a function in my example below? #!/bin/ksh function exitFunction { if ]; then echo "success" elif ]; then echo "failed" exit 1 # the exit problem fi exit 0 } ... (2 Replies)
Discussion started by: chstr_14
2 Replies

7. Shell Programming and Scripting

Exit from function

Hi, I just want to exit from function if some condition doesnt meet then control should go back to main program and start running from where it left.. When i used "exit" inside the function, its simply exited from entire script and it didnt run anymore.. Any idea how to do this.. Thanks... (3 Replies)
Discussion started by: nram_krishna@ya
3 Replies

8. Shell Programming and Scripting

exit from function

Hi all, My kshell code is not working, when I use a function to return something. But when I use the same function as without returning any values, it is working. Pls help me here. Code1 : func1 () { y=`echo $x | grep XXX| cut -f 2 -d ' '` if ; then exit 100 ... (2 Replies)
Discussion started by: poova
2 Replies

9. Programming

capturing enter and exit of every function

If I have a "Hello World" function (just prints that) and a similar "Goodbye World" function... is there a way (maybe a compiler option?) that I can get them to be executed directly as the absolute first and last things run in every function call. So for example, the following code: int foo()... (5 Replies)
Discussion started by: jjinno
5 Replies

10. Programming

How to get exit value of an executable that gets called from function?

How to get exit value of an executable that gets called from function? I have an executable called “myexec” which returns 0 on success and different values for different fail scenarios. I need to call this (myexec) executable from “myprog()” function of other executable and get the exit value... (1 Reply)
Discussion started by: sureshreddi_ps
1 Replies
Login or Register to Ask a Question