I'm trying to write a script that I can run from the context menu in Caja (a clone of Nautilus). Its purpose is to pass the files that are selected in the file manager to a given program (for example, 7zip). A list of selected files is provided in the system variable $CAJA_SCRIPT_SELECTED_FILE_PATHS, separated by newlines. I want to pass each line of this variable to the program as an argument, even if they contain spaces.
After failing at quote-wrapping, I tried the following line:
echo "$CAJA_SCRIPT_SELECTED_FILE_PATHS" | xargs -d "\n" 7z a archive.7z
While it worked for 7zip, I changed the script so that the arguments are passed to a program I wrote and something went wrong, so I want a way to execute this in a terminal to get output from the program. I was using xterm -hold -e, but I haven't been able to make a command with a pipe in it execute this way because the shell will try to pipe output from xterm. I also tried shell redirection (by adding > log.txt to the end of the line), and it made a file for 7zip's usual output but a blank file for my program, which outputs messages just fine in the terminal.
Finally, I want a POSIX-compliant (or otherwise standard) solution so that it will work the same way on BSD and Linux.
EDIT: To clarify, I want to change some thing like this:
/path/with/a space
/another/path
Into what I would hypothetically get from something like this:
char *argv[]={"/path/with/a space", "/another/path"};
EDIT2: That's how it should be in the program recieving the arguments, not the shell!