Shell script help

Colin Law clanlaw at gmail.com
Wed Oct 15 16:29:10 UTC 2014


On 15 October 2014 15:58, Kelly Dunlop <kelly at xyzzy.org.uk> wrote:
> On Wed, Oct 15, 2014 at 03:43:38PM +0100, Colin Law wrote:
>> This is not strictly a Ubuntu issue but I hope someone will take pity
>> on me.  I am trying to write a shell script (it is in ash rather than
>> bash but I don't think that is an issue here) that is given a
>> parameter that is a partial directory name and may have spaces in it.
>> I will call it with something like
>>
>> myscript "dir name"
>>
>> In the script I want to do something like
>> for f in /some/path/$1/*.txt
>> do
>>   blah blah
>> done
>>
>> but I can't work out out how to quote it so that it keeps the words of
>> the parameter together but still expands the glob.  Google has failed
>> to help me to find the right incantation to make it work.
>
> Colin,
>
> Just put the quotes round the part you don't want it to change ie:
>
> for f in /some/path/"$1"/*.txt

Thanks Kelly and Paul
That's much too easy, I thought, I am sure I have tried that, but when
I repeated your example it worked, of course.  Then I realised why I
have been having problems.  What I am trying to do is more complex in
that I am actually calling it using
myscript "path name*"
so I see now that this will not as easily work.

Now that I understand it I have amended my script so that it does not
need globbing characters in the parameter but it would be interesting
to know how to solve the problem where the parameter is multiple words
and includes globbing characters.

Thanks again

Colin

>
>
> I tried calling this script as myscript "dir name":
>
> #!/bin/bash
>
> echo /tmp/"$1"/*.txt
>
> with a directory called /tmp/dir name containing one.txt, two.txt and three.txt
> and it gives:
>
> /tmp/dir name/one.txt /tmp/dir name/three.txt /tmp/dir name/two.txt
>
> so it works on bash.  Can't test it on ash because I don't have it installed.
>
> HTH
> Kelly
>>
>> Any help would be much appreciated.
>>
>> Cheers
>>
>> Colin
>>
>> --
>> ubuntu-users mailing list
>> ubuntu-users at lists.ubuntu.com
>> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>>
>
> --
> Kelly Dunlop
> kelly at xyzzy.org.uk
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users




More information about the ubuntu-users mailing list