Re: bash null conditional

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

 



Craig White wrote:
On Mon, 2009-03-30 at 13:33 -0500, Justin Willmert wrote:
Craig White wrote:
I'm in my bash book and looking on web but can't seem to resolve this
simple problem.

$ if [ -n "grep A121 myfile.csv" ]; then echo "null"; fi
null

$ if [ -n "grep A125 myfile.csv" ]; then echo "null"; fi
null

A125 definitely is null when I just run the grep command in the quotes
but A121 definitely is not null.

What am I missing on the if/null operator here?

Craig

Have you tried
    $ if [ -n $(grep A121 myfile.csv) ]; then echo "null"; fi
    $ if [ -n $(grep A125 myfile.csv) ]; then echo "null"; fi

The double quotes make a string, but it looks like you which to be executing grep which is accomplished by using the $() notation. You could also use a pair of backticks surrounding the command.
----
I started with backticks...

$ if [ -n `grep A125 ARdebtorsmaster.csv` ]; then echo "null"; fi
null
$ if [ -n `grep A121 ARdebtorsmaster.csv` ]; then echo "null"; fi
bash: [: too many arguments

which interestingly is also the same problem that I get with your first
suggestion...

$ if [ -n $(grep A125 ARdebtorsmaster.csv) ]; then echo "null"; fi
null
$ if [ -n $(grep A121 ARdebtorsmaster.csv) ]; then echo "null"; fi
bash: [: too many arguments

and then of course, light bulb goes off...I have to figure out how to
get a basic exit code from grep, but seemingly the -s and the -q aren't
the answer.

They are. You want
  if ! grep -q pat file; then echo null; fi
or my personal choich
  grep -q pat file || echo null

--
Bill Davidsen <davidsen@xxxxxxx>
  "We have more to fear from the bungling of the incompetent than from
the machinations of the wicked."  - from Slashdot

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