Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a Multiline,Quoted string as a single command line argument in unix?

I just need to pass any HTML content as a command line argument which includes characters like ', " , ` and what not? How can I pass this as a single argument in unix?

like image 917
Cookies Avatar asked Sep 25 '12 05:09

Cookies


1 Answers

If you were typing at the command line, you could use the following hack:

some_program -some_option some_argument "$(cat)"
any text you like with ` or " or $ or < or whatever
^D

The last thing is control-D (end of file)

To put that into a bash script, you can use a Here document:

some_program -some_option some_argument "$(cat <<'EOF'
any text you like with ` or " or $ or < or whatever
EOF
)"

That will work as long as the text is not exactly the characters EOF (and if it is, you just have to change EOF in both places to something else).

But I don't think either of those are what you want to do. I think you are trying to invoke a program from another program (in Lua). Unfortunately, Lua out-of-the-box does not provide any function which can do that. It only provides os.execute, which uses the shell (/bin/sh, not bash) to interpret the command line.

There was a nice implementation for Lua of spawn written by Mark Edgar, but I don't know if it is still maintained. Failing that, you can still use the second hack:

require("os")

function call_with_arg(prog, rawtext)
  return os.execute(prog.." \"$(cat <<'EOF'\n"..rawtext.."\nEOF\n)\"")
end
local html =
  [=[<html><body><img src="image.png" onload='alert("loaded")'/></body></html>]=]

call_with_arg("echo >somefile.txt", html)        
like image 192
rici Avatar answered Sep 29 '22 13:09

rici