[NTLUG:Discuss] What scripting change do I need to make?
Leroy Tennison
leroy.tennison at verizon.net
Tue Oct 6 21:51:09 CDT 2015
According to 'man bash' I see exactly what you're saying but 'mkdir -p
test/{`seq -s, 1 3`}' creates "test/{1,2,3}" (literally, the test
directory has one child directory named {1,2,3}) and 'echo -p test/{`seq
-s, 1 3`}' produces "-p test/{1,2,3}" so, for whatever reason, brace
expansion isn't operating as we think it should.
On 10/05/2015 11:07 PM, Patrick R. Michaud wrote:
> On Mon, Oct 05, 2015 at 09:46:08PM -0500, Leroy Tennison wrote:
>> If only there was a way to tell mkdir "this isn't a string!".
> It's important to note that mkdir(1) itself never sees quotes, braces,
> or anything like that -- all of these expansions and substitutions
> are handled by the shell *before* the mkdir command is ever called.
>
> So, according to the bash(1) man page ("EXPANSIONS"):
>
> The order of expansions is: brace expansion; tilde expansion, parameter
> and variable expansion, arithmetic expansion, and command substitution
> (done in a left-to-right fashion); word splitting; and pathname
> expansion.
>
> Thus, in the case of
>
> mkdir -p test/{`seq -s, 1 3`}
>
> the brace expansion occurs before the command substitution. Since
> the part inside the braces doesn't have any bare commas in it, the
> result of the command line after performing brace expansion is:
>
> mkdir -p test/`seq -s, 1 3`
>
> and after performing command substitution the command line becomes
>
> mkdir -p test/1,2,3
>
> The mkdir command then receives the "-p" and "test/1,2,3" arguments.
>
> Try the above using echo(1) instead of mkdir(1) and you'll see what
> I mean. :-)
>
> Pm
>
>
> On Mon, Oct 05, 2015 at 09:46:08PM -0500, Leroy Tennison wrote:
>> Thanks for asking, the original thinking was concerning building a
>> "calendar" tree. There are (usually compliance-related) situations where
>> documents as proof of activities are required daily. Something like 'mkdir
>> -p 2016/{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}/{`seq -s, 1 31`}'
>> would produce a year's structure (and, of course, changing 2016 t0
>> {2016,2017,2018} could produce multi-year - that's if `seq ...` would work).
>> Granted, you have to remove 31 for five months and 30 along with (usually)
>> 29 for February but that's small "retrofit" for a calendar year's worth of
>> structured directory creation.
>>
>> What I've discovered about the problem is that I can get the undesired
>> results with 'mkdir -p test/{'1,2,3'}' which leads me to believe that
>> possibly the problem is that the `seq ...` substitution is causing mkdir to
>> believe it's a string (even though the return of `seq ...` contains no
>> quotes). The same thing happens with $variable.
>>
>> Yes, I could use progressive 'for ...' loops but I was hoping for something
>> simple (it's known as being lazy...). If only there was a way to tell mkdir
>> "this isn't a string!".
>>
>> On 10/05/2015 12:44 AM, Robert Citek wrote:
>>> I find your original version using braces more readable. What's your
>>> rationale for wanting to use seq?
>>>
>>> Regards,
>>> - Robert
>>>
>>> On Saturday, October 3, 2015, Leroy Tennison <leroy.tennison at verizon.net>
>>> wrote:
>>>
>>>> Thanks, that works (had to refresh my printf understanding).
>>>>
>>>> On 10/03/2015 04:31 AM, Patrick R. Michaud wrote:
>>>>
>>>>> How about...?
>>>>>
>>>>> mkdir -p `seq -f "test/%g" 1 3`
>>>>>
>>>>> Pm
>>>>>
>>>>> On Sat, Oct 03, 2015 at 12:36:25AM -0500, Leroy Tennison wrote:
>>>>>
>>>>>> Using
>>>>>>
>>>>>> mkdir -p test/{1,2,3}
>>>>>>
>>>>>> creates test/1, test/2 and test/3 as expected. However
>>>>>>
>>>>>> mkdir -p test/{`seq -s, 1 3`}
>>>>>>
>>>>>> creates test/{1,2,3}
>>>>>>
>>>>>> What do I need to do to get the "seq" variant to produce the same result
>>>>>> as
>>>>>> the first command?
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> http://www.ntlug.org/mailman/listinfo/discuss
>>>>>>
>>>>> _______________________________________________
>>>>> http://www.ntlug.org/mailman/listinfo/discuss
>>>>>
>>>>>
>>>> _______________________________________________
>>>> http://www.ntlug.org/mailman/listinfo/discuss
>>>>
>>> _______________________________________________
>>> http://www.ntlug.org/mailman/listinfo/discuss
>>>
>>
>> _______________________________________________
>> http://www.ntlug.org/mailman/listinfo/discuss
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
More information about the Discuss
mailing list