Re: strange shell behavior...thoughts?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Kevin Martin wrote:
> So I'm fairly confused at this point.
>
> Start with a /bin/sh shell.
>
> Then do:
>
> sh-3.2$ unset $?
> sh: unset: `0': not a valid identifier
>
> ok, no problem; then do
>
> sh-3.2$ /bin/ksh -c " set -xv  ; grep ABCD  b ; echo $? ; if [ "$?" =
> "0" ] ; then echo yes ; fi"
> + grep ABCD b
> ABCD="C" ; export ABCD
> + echo 1
> 1
> + [ 1 = 0 ]
>
> WHAT? 
>
> Then do it again without unsetting $?:
> sh-3.2$ /bin/ksh -c " set -xv  ; grep ABCD b ; echo $? ; if [ "$?" = "0"
> ] ; then echo yes ; fi"
> + grep ABCD b
> ABCD="C" ; export ABCD
> + echo 0
> 0
> + [ 0 = 0 ]
> + echo yes
> yes
>
>
> Why doesn't the first iteration work?  Is this a ksh bug?
>
>
>   
I don't think you are testing what you think you are testing....

I believe that when you execute "ksh -c" you won't get a return value
for the commands in the string as you have them listed.

Take the 3 potential return codes for grep.

0 = string found
1 = string not found
2 = file not found

$ cat b
ABCD
$ grep ABCD b
ABCD
$ echo $?
0
$ grep K b
$ echo $?
1
$ grep ABCD kkk
grep: kkk: No such file or directory
$ echo $?
2

$ ksh -c "set -xv ; grep ABCD kkk ; echo $?"
+ grep ABCD kkk
grep: kkk: No such file or directory
+ echo 0
0

Clearly the "echo $?" is not returning the code from grep...but from the
last command of the parent shell...

As in....

$ grep ABCD kkk
grep: kkk: No such file or directory
$ ksh -c "set -xv ; grep ABCD kkk ; echo $?"
+ grep ABCD kkk
grep: kkk: No such file or directory
+ echo 2
2

I don't do much shell scripting these days....but it may not even be the
result of "ksh -c" it could just be what is true when you use ";" to
string commands together on the same line.


-- 
Car sickness is the feeling you get when the monthly car payment is due.

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux