Problems with "exit" called from function in bourne script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problems with "exit" called from function in bourne script
# 1  
Old 05-01-2012
[SOLVED] Problems with "exit" called from function in bourne script

Hi everyone.

Code:
#!/sbin/sh

EXITING()
{
        umount /FOLDER
        rm -Rf /FOLDER 
        echo "EXIT"
        exit 0      
} 


EXITING 

echo "OK"

This is my stripped script for android environment and is launched in the recovery mode. No bash or ksh, must be /sbin/sh provided and symlinked by busybox toolbox.

When function is called, the commands "exit", "exit 0" and "exit 1" only exit from function and always show "EXIT" and "OK", but not end the script as I would like. Whats wrong?

I need call EXITING many times in the script and need the "exit" command working, is there an alternative to force exit?

thank in advance and sorry for my english.

Last edited by vacadepollo; 05-02-2012 at 10:59 AM..
# 2  
Old 05-02-2012
I cannot reproduce this using busybox 1.17.1
# 3  
Old 05-02-2012
I'm using busybox "BusyBox v1.19.4-cm9 bionic" included in the cyanogenmod 9 kernel.

I'm pretty sure that in others kernels/busybox this code is working flawless but I need to find an universal method that provide an exit in whatever busybox version.
# 4  
Old 05-02-2012
If you are looking to do cleanup on script exit, maybe you really want just trap:

Code:
cleanup() {
    rm ... etc ...
}
trap cleanup EXIT

but no matter what I do, exit has always exit the script... maybe is issue elsewhere in script? this small snippet alone reproduces "OK" for you ?
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 05-02-2012
Ok I've found the problem.

When EXITING is called from a "for-done" loop doesn't work and only break it.
But if I call it in other situation works fine.

Is it a normal behavior? For future referencies...

Thanks for the help Smilie
# 6  
Old 05-02-2012
No... Seems like behavior of break. Are you sure /sbin/sh is busybox's? Seems like an odd place. Thought Android put it in /system/xbin or something. It may have -x option to trace. if you run sh -x script you should see nothing after + exit ...
# 7  
Old 05-02-2012
Quote:
Originally Posted by neutronscott
No... Seems like behavior of break. Are you sure /sbin/sh is busybox's? Seems like an odd place. Thought Android put it in /system/xbin or something. It may have -x option to trace. if you run sh -x script you should see nothing after + exit ...
mmmm I know, but it happens.

OK, my fault, sorry, EXITING is called from other function, this is the problem.

the "for" loop call "CHECKING" and then call "EXITING" , but only break the "for" loop, and get the same behavior, show me "OK" echo.

(*this is a sample to show the concept)



test.sh
Code:
#!/sbin/sh

CHECKING()
{
	EXITING 0
}



EXITING()
{
        exit 0      
} 



for i in ls ;do
  ls *.tar* | while read i;do
      echo "Hello"
      CHECKING
  done
done

echo "OK"



sh -x test.sh
Code:
/emmc # sh -x test.sh
+ sh -x test.sh
+ read i
+ ls Backup.tar p7zip.tar.bz2
+ echo Hello
Hello
+ CHECKING
+ EXITING 0
+ exit 0
+ echo OK
OK


I use "#!/sbin/sh" because my script in some point makes a /system format and need the busybox integrated in the recovery, not the included in /system. And /sbin/sh is the standard path to launch the shell from recovery.

Last edited by vacadepollo; 05-02-2012 at 10:27 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

"help me!!" if and function problems

I am trying to allow the user to be notified that the id has already taken from the file "record" and that the user has to contain a numerical figure as well. however when i run it, it will only stay at the please enter a number section and does not change. do u know where is the problem? ... (2 Replies)
Discussion started by: bassmasta1
2 Replies

3. UNIX for Dummies Questions & Answers

Expect "interact" fails when called from another script

So, I have an expect script (let's call it expect.exp) that takes 3 arguments. It logs into a remote server, runs a set of commands, then hands control over to the user by the "interact" command. If I call this script from the command line, it works properly. Now I'd like to apply this script... (2 Replies)
Discussion started by: treesloth
2 Replies

4. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

5. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. UNIX for Advanced & Expert Users

All alias in .profile lost when "script" command is called

Hi, I was trying to call "script <an ip add>" command from .profile file to log everything whenever anyone logs in to this user. I did the following at the end of .profile. 1) Extracted the IP address who logged in 2) Called script < ip add> . The problem I am facing is all, aliases etc. written... (3 Replies)
Discussion started by: amicon007
3 Replies

10. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies
Login or Register to Ask a Question