Dika Dika - 4 months ago 8
Linux Question

Send value through double qoute/single qoute?

So i have a ash script that send command to device via telnet

The command look like this.


echo 'dofile("lcdprint.lua").lcdprint("date")' | telnet 192.168.1.23 23


I want the output date like this

Tue Jul 12 17:10:51 WIB 2016


But instead of above output, the code run unexpected with this result

date


How do i send the correct command? The output should contain date value, not 'date' string.

Thankyou :)

Answer

If you want to incorporate the output from locally executing the date utility, before sending the command string to the target machine, use $(...), Bash's command substitution:

echo "dofile('lcdprint.lua').lcdprint('$(date)')" | telnet 192.168.1.23 23

Note how the overall string is double-quoted to ensure that $(...) is expanded.

If you want to run date on the target machine, use Lua's os.execute() to run a shell command:

echo 'dofile("lcdprint.lua").lcdprint(os.execute("date"))' | telnet 192.168.1.23 23

Note how the overall string is single-quoted, because no interpretation by Bash is needed in this case, and using single quotes ensures that the string is passed as-is.

Comments