Creating Cmds with variable number of args

I’m trying to create a scp command to copy over a variable number of files to another computer. I’d like for it to be a single scp call rather than one per file as it is much more efficient. I have something looking like this:

function CreateStagingCommand(ip_addr::AbstractString, operations::Vector{ProcessReplacement})
	files = ""
	for replacement in operations
		# Add the file-path to the list of things to copy
		files *= "$(replacement.replacement_location) "
	end
	command = `scp -i "~"/real/file/path/here/id_rsa $(files) root@$(ip_addr):$(staging_area)`
	return command
end

Problem is, Julia sees the $(files) as a single parameter with spaces in it and (not so) helpfully puts quotes around. It’s possible to do something like:

function CreateStagingCommand(ip_addr::AbstractString, operations::Vector{ProcessReplacement})
	files = ""
	for replacement in operations
		# Add the file-path to the list of things to copy
		files *= "$(replacement.replacement_location) "
	end
	command = `scp -i "~"/real/file/path/here/id_rsa $(a)$(b)$(c)$(d)$(etc) root@$(ip_addr):$(staging_area)`
	return command
end

but that means I have to be aware of the max number of files a user is going to copy ahead of time, and also makes the code much uglier to read and maintain.

Anyone have a good way to handle this?

You can interpolate Cmd objects:

julia> cmd = `echo`
`echo`

julia> for i in 1:10
           cmd = `$cmd $i`
       end

julia> run(cmd)
1 2 3 4 5 6 7 8 9 10
1 Like

Just interpolate an array of filenames.

julia> files = ["README", "default_par.yaml"]
2-element Vector{String}:
 "README"
 "default_par.yaml"

julia> run(`ls -l $files`)
-rw-rw-r-- 1 gunnar gunnar 1325 mar 21 13:32 default_par.yaml
-rw-rw-r-- 1 gunnar gunnar  126 mar 21 13:32 README
1 Like