[clug] Pointless time-wasting bash questions

Bob Edwards Robert.Edwards at anu.edu.au
Thu Jun 25 18:06:36 MDT 2015


On 26/06/15 09:57, Bob Edwards wrote:
> On 25/06/15 22:15, Scott Ferguson wrote:
>> On 25/06/15 21:44, Eyal Lebedinsky wrote:
>>> On 25/06/15 19:29, Scott Ferguson wrote:
>>>> Messing with bracket contractions.
>>>>
>>>> *1*.
>>>> cp Kernel/Config/GenericAgent.pm.dist Kernel/Config/GenericAgent.pm
>>>> I can shorten the process with:-
>>>> cp Kernel/Config/GenericAgent.pm{.dist,}
>>>>
>>>> *2*. likewise
>>>> cp Kernel/Config.pm.dist Kernel/Config.pm
>>>> I can shorten the process with:-
>>>> cp Kernel/Config.pm{.dist,}
>>>>
>> <snipped>
>>>
>>> Really?
>>
>> No.   :D
>>> cp with four arguments does what when the last one is not a
>>> directory?
>>
>> Good catch Eyal!
>>
>> It was an interview question (not mine). I don't know what the correct
>> answer is - the first two example are similar to the two processes that
>> are meant to be combined into "a single command using only cp and bash
>> builtins". The interviewee doesn't recall whether the question was to
>> find "the shortest way" - or "the fastest way". (he also said the
>> interviewers spent the entire test ignoring him while giggling and
>> browsing on their laptops)
>>
>> That's "a single command" - not a "single-line command". The OS was a
>> recent Ubuntu.
>>
>> I 'thought' I was on the path to the answer until I tested it. The
>> cricket bat and ball question was much easier.
>>
>> Someone else who interviewed successfully and was asked similar
>> questions said it does use brackets - but wouldn't elaborate. I'm not
>> convinced there is a right/best answer but it's no less annoying (and
>> no, I'm not trying for the position, just curious as I seriously could
>> use the answer for various tasks).
>>
>> Both people are adamant only cp and bash builtins were allowed. I tried
>> xargs anyway because it's been bugging me for days, but couldn't work
>> out how to solve it that way either.
>>
>
> I could argue that this is "one command", using only bash builtins
> and cp:
>
> for i in .pm /GenericAgent.pm ; do cp Kernel/Config${i}.dist
> Kernel/Config${i} ; done
>
> Bob Edwards

Or maybe I should have shortened it as per the initial examples:

for i in /GenericAgent.pm .pm ; do cp Kernel/Config${i}{.dist,} ; done

Bob Edwards.

>
>>
>> <snipped>
>>
>>>>
>>>> Any dangers I may have overlooked?
>>>
>>> Well, in general, and expansion (meaning not an explicit list) hides the
>>> possibility of unexpected results...
>>
>> Agreed, though obfuscation does have it's charms. For actual use, *if*
>> it worked, it 'might' not be portable.
>>
>> <snipped>
>>
>> Thanks for giving it thought. (I did warn it was pointless time-wasting)
>>
>>
>> Kind regards
>>
>



More information about the linux mailing list