My 3-line code to copy a directory structure doesn't work

I wrote this dead simple code to reproduce a directory hierarchy:

for dir in $(find @args[1] -type d)
        mkdir -v $( echo $dir | sed "s/@args[1]/@args[2]/" )

With mkdir -p src/a src/b I create the following structure:

|-- a
`-- b

I then call my script like so:

./copy_tree.ion src out

This gives me the following error:

mkdir: out
out: No such file or directory

This equivalent script works in bash and ksh:

for dir in $(find $1 -type d)
        mkdir -v $( echo $dir | sed "s/$1/$2/" )

I can’t imagine where there could be a mistake. If I replace mkdir -v with another echo in the Ion version, it outputs the correct strings that mkdir needs, yet for some reason when I run the script it doesn’t work.

I found out something related. The command

find . -exec echo {} \;

doesn’t work on a Ion command line (it outputs empty lines). It works in any traditional shell.

Edit: Nevermind. The problem here is that { and } needs to be quoted or escaped with \

After lots of confusion, I figured it out. I just needed to split the arguments. It was right there in the manual. If I had read just a little further it would have saved me hours of debugging.

The correct code:

for dir in @split($(find @args[1] -type d))
        mkdir -v $( echo $dir | sed "s/@args[1]/@args[2]/" )

Here’s a more idiomatic version of that code using the replace method.

for dir in @split($(find @args[1] -type d))
        mkdir -v $replace($dir @args[1] @args[2])

Thank you for sharing code examples here, it is something I would like to see more of. I am a beginner but hope that seeing snippets of script and code in the conversations will have an effect like that claimed for learning a foreign language - being immersed will allow some of the knowledge to creep in eventually!

Hopefully it is of more immediate use to others too!

Are you reading the manual? There’s only so much you can absorb by osmosis alone, even when talking about foreign languages.

You’re right, although my only interest in languages at the moment is in using C to write keyboard stuff in QMK Firmware, and I don’t think I will be contributing operating system stuff in Rust anytime soon. Hopefully my postings here serve as a demonstration to lurkers that it is not necessary to be some sort of coding guru to join the community and contribute to the discussion!

Oh, well if you become interested in learning Ion I’d be happy to help. I’m almost done studying the manual and I think answering questions would be a great way to test what I’ve learned. It’s a pretty small language and there’s no complicated, low-level, operating system stuff, so it’s pretty easy too.