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
# 8  
Old 05-02-2012
ahhhhh haha. i should have forseen this. this is normal here. the problem is you are in a subshell!
everything done by | while read is not executed in same context. this shows more clearly with variables:

Code:
[mute@geek ~]$ cat subshell
#!/bin/busybox sh

i=0
printf '%s\n' foo bar baz | while read var; do
        let i++
        echo "subshell i = $i"
done

echo "main shell i = $i"
[mute@geek ~]$ ./subshell
subshell i = 1
subshell i = 2
subshell i = 3
main shell i = 0

this is same for exit!

Code:
[mute@geek ~]$ cat subshell
#!/bin/busybox sh

i=0
printf '%s\n' foo bar baz | while read var; do
        exit # from subshell ...
        echo "subshell is alive"
done

echo "main shell is alive"
[mute@geek ~]$ ./subshell
main shell is alive

You do not need to use ls and then read it. The shell can iterate over files:

Code:
check() {
    local file=$1

    # do something with file
}

for file in *.tar; do
    [ -e "$file" ] || continue # if no *.tar file, $file will be '*.tar' so check if file is really existing
    check "$file"
done

This User Gave Thanks to neutronscott For This Post:
# 9  
Old 05-02-2012
Quote:
Originally Posted by neutronscott
ahhhhh haha. i should have forseen this. this is normal here. the problem is you are in a subshell!
everything done by | while read is not executed in same context. this shows more clearly with variables:

Code:
[mute@geek ~]$ cat subshell
#!/bin/busybox sh

i=0
printf '%s\n' foo bar baz | while read var; do
        let i++
        echo "subshell i = $i"
done

echo "main shell i = $i"
[mute@geek ~]$ ./subshell
subshell i = 1
subshell i = 2
subshell i = 3
main shell i = 0

this is same for exit!

Code:
[mute@geek ~]$ cat subshell
#!/bin/busybox sh

i=0
printf '%s\n' foo bar baz | while read var; do
        exit # from subshell ...
        echo "subshell is alive"
done

echo "main shell is alive"
[mute@geek ~]$ ./subshell
main shell is alive

You do not need to use ls and then read it. The shell can iterate over files:

Code:
check() {
    local file=$1

    # do something with file
}

for file in *.tar; do
    [ -e "$file" ] || continue # if no *.tar file, $file will be '*.tar' so check if file is really existing
    check "$file"
done


OOMMMGGG!!! I can't believe it XD

OK, thank for open my eyes, I'm feel really really stupid with this basic errors. Now works like a charm. Thanks!! Smilie
 
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