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

Re: Substring search in dash [ _NOT_ bash ] shell



On 27 September 2016 at 13:49, Richard Owlett <rowlett@cloud85.net> wrote:
> On 9/26/2016 10:42 AM, Greg Wooledge wrote:
>> On Mon, Sep 26, 2016 at 10:39:59AM -0500, Richard Owlett wrote:
>>>
>>> #      string if [ "`hostname`" = bob ]; then echo bob.cfg; fi
>>>
>>> I need for the case when evaluates to marybobsam .
>>> I can find examples in bash shell, but I need dash.
>>
>> Are you asking how to check for the substring "bob" in your input,
>> in POSIX sh?  Use case, and a glob:
>>
>> case $input in
>>    *bob*) echo "Bob found." ;;
>> esac
>>
>> case $(hostname) in
>>    *bob*) echo "I must be a Bob." ;;
>> esac
>
> Thank you. I was expecting an if...fi construct.

Shell programming is an arcane topic full of corner cases and traps for
new players. Thus very often the answers to simple questions are far more
complex than we might expect. Consequently you are unlikely to find any
document that spoon feeds you what you need. Diligence and
apprenticeship is the only way here.

And I have to say too, because you are probably unaware ... Greg is a guru.
Consider yourself blessed to be receiving advice from an expert, without
having made much effort :)

> Is dash documented anywhere?

man dash

> All I've found are pages saying that some "bashisms" do not work in dash :<

And the most useful of those is probably this:
http://mywiki.wooledge.org/Bashism

Also you can read this:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

Asking #bash on freenode.net is another source of excellent guidance.


Reply to: