Re: OT: bash help

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

 



On 17 August 2014 08:36, Cameron Simpson <cs@xxxxxxxxxx> wrote:
> On 16Aug2014 14:44, Mike Wright <mike.wright@xxxxxxxxxxxxxx> wrote:
>>
>> I'm trying to write a simple script that if provided an argument, uses
>> that, or if nothing is provided, uses a predefined string.
>
>
> This is general shell stuff, not bash specific.
>
>> if [ -n $# ]
>
>
> This is always true. Even "0" is a nonempty string.
>
> Test [ $# -gt 0 ] instead.
>
>

Though it's a matter of preference I'd prefer [ $# = 0 ] or  [ $# != 0
] tests unless you really need one of the numeric comparisons. The
reason (and it doesn't actually apply for $#, hence matter of
preference) is that if you have somehow ended up with a string rather
than a number (e.g. an error from wc or something) then you have to
think about what your comparison is going to do for that case, whereas
the =! and == comparisons will do what you expect. If you do need -gt,
-lt and the rest then (except for $#) it's best to check what you've
got is a number first.


-- 
imalone
http://ibmalone.blogspot.co.uk
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org




[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux