Re: strange shell behavior...thoughts?

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

 




Cameron Simpson wrote:
> On 14Sep2008 23:50, Kevin Martin <kevintm@xxxxxxxxxxxxx> 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"
>
> You know you need to escape " inside ", yes? As it happens it has no
> effect in your script here, but ...
>
> | + grep ABCD b
> | ABCD="C" ; export ABCD
> | + echo 1
> | 1
> | + [ 1 = 0 ]
> | 
> | WHAT? 
>
> The "echo" command has a return status, and trashes your $?.
>
> | Then do it again without unsetting $?:
>
> You don't ever unset $?. It is not a meaningful idea.
>
> If you want a particular $?, do this:
>
>   grep ABCD b
>   xit=$?
>   echo $xit     # overwrites $?, but $xit is untouched
>   if [ $xit = 0 ]
>   then
>    ...
>
> but almost invariably when someone does:
>
>   some_command ...
>   if [ $? = 0 ]
>   then
>
> they would be MUCH better off just going:
>
>   if some_command ...
>   then
>
> Remember that the predicate in an if/while if a command_list, and the
> exit status of the last command in the command_list is what is tested.
> The command "[" (aka "test") is just another command suited to simple
> Boolean tests. But it is not "special".
>
> Example:
>
>   # repeat prompt until EOF or empty reply
>   while echo "Enter something:"
>         read foo && [ -n "$foo" ]
>   do
>     echo "foo=$foo"
>   done
>
> | [...snip...] Is this a ksh bug?
>
> No.
>
> Cheers,
>   

Uh, yeah, duh on my part on the "unset $?" stupidity.  I was trying to
duplicate what I'm seeing in our application.  Essentially we have an
application that can run remote commands on a schedule (kind of like a
network scron) and it basicly execs a "/bin/sh" and runs whatever
command you've passed it.  What I've been seeing is if I try to pass the
"/bin/ksh -c "if...."" script the $? of the grep never seems to be 0 for
some reason so I essentially get the results that I showed in my example
where I was doing the dumb "unset $?" (making, at that point, $? = 1). 
I agree that the "if some_command" would be a much better way of doing
this but I get the same results if I do that as I do if I do the
"some_command" the "if [ $? = 0 ]" test.  From reading the man page for
ksh I may be running into a POSIX vs non-POSIX issue.

Kevin

-- 
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