Do you know about this issue ?
Why not a sample {exit 1; } ?
Thanks you !
Why not a sample {exit 1; } ?
Thanks you !
[...]Quote:> Do you know about this issue ?
> Why not a sample {exit 1; } ?
That's to set $?, for a trap on EXIT (0) to get it.
--
Stphane
>>Do you know about this issue ?
>>Why not a sample {exit 1; } ?
> [...]
> That's to set $?, for a trap on EXIT (0) to get it.
[/tmp]$ cat trap.sh
#! /bin/sh
touch "/tmp/test.trap"
ls -l "/tmp/test.trap"
trap 'rm -rf /tmp/test.trap;' 0
{ exit 1 ; }
[/tmp]$ ls -l /tmp/trap.test
ls: /tmp/trap.test: No such file or directory
[/tmp]$ ./trap.sh
-rw-r--r-- 1 xxxxxxxx g900 0 Feb 28 04:06 /tmp/test.trap
[/tmp]$ echo $?
1
[/tmp]$ ls -l /tmp/trap.test
ls: /tmp/trap.test: No such file or directory
[/tmp]$
The same is the case if I introduce,
{ (exit 1); exit 1;}
[/tmp]$ cat trap.sh
#! /bin/sh
touch "/tmp/test.trap"
ls -l "/tmp/test.trap"
trap 'rm -rf /tmp/test.trap;' 0
{ (exit 1); exit 1; }
[/tmp]$ !vi
vi trap.sh
[/tmp]$ ./trap.sh
-rw-r--r-- 1 xxxxxxxx g900 0 Feb 28 04:09 /tmp/test.trap
[/tmp]$ echo $?
1
[/tmp]$
If the above illustration is not right, could you let me know how
exactly the OP works ?
Thanks.
Vino
>>>Do you know about this issue ?
>>>Why not a sample {exit 1; } ?
>> [...]
>> That's to set $?, for a trap on EXIT (0) to get it.
> So why do we need an extra { exit 1 } for that ? EXIT (0) gets called
> just before the termination of the shell script.
Compare this:
sh -c '
trap "
ret=\$?
echo in exit trap
exit \"\$ret\"
" EXIT
exit 1
'; echo $?
with:
sh -c '
trap "
ret=\$?
echo in exit trap
exit \"\$ret\"
" EXIT
(exit 1); exit 1
'; echo $?
In the first case, the return status of the script will be "0"
(success) even though we called "exit 1" (which meant we want to
report an error).
[...]Quote:> [/tmp]$ cat trap.sh
> #! /bin/sh
> touch "/tmp/test.trap"
> ls -l "/tmp/test.trap"
> trap 'rm -rf /tmp/test.trap;' 0
> { exit 1 ; }
> [/tmp]$ ls -l /tmp/trap.test
> ls: /tmp/trap.test: No such file or directory
> [/tmp]$ ./trap.sh
> -rw-r--r-- 1 xxxxxxxx g900 0 Feb 28 04:06 /tmp/test.trap
> [/tmp]$ echo $?
> 1
> [/tmp]$ ls -l /tmp/trap.test
> ls: /tmp/trap.test: No such file or directory
> [/tmp]$
> The same is the case if I introduce,
> { (exit 1); exit 1;}
See info -f autoconf --index-search=exit
The point is you may get rm's exit status in some shells if
it's not null.
--
Stephane
>> That's to set $?, for a trap on EXIT (0) to get it.
> So why do we need an extra { exit 1 } for that ?
In some shells, this exit status is still "pending"
and not visible in the handler yet; there you see
the next recent status instead.
It's for example pending in Bourne, ksh88, zsh.
It's already available in ksh93, bash, ash, pdksh
1. ksh: trap '...' exit int ... or just trap '...' exit?
trap '...' exit int ...
int will cause the script to exit, so '...' get run twice:
#!/bin/ksh
trap 'print a' exit int
sleep 10
^Ca
a
But the natural exit will always causes '...' to run,
which is often not desired.
So should we always write something like
trap 'print a' exit int
...
trap - exit
exit 0
?
--
Michael Wang
http://www.unixlabplus.com/
2. Weird question #1: sendmail
3. What is this supposed to return: "trap 'exit ' 0; exit"?
4. PATCH: Support tera byte disk
6. strange process about sendmail
7. Why does 'exit' close my xterm...
8. problem intalling the jdk13 port
9. Using 'exit' in a Bourne shell script
10. 'write' won't write; exits immediately.
11. hung after 'exit()' is called...what's it doing?
12. Determining parameter used with "exit" command before exiting
13. "Quit and save - Exit and don't save" why?