0

I am creating a script for my router (running busybox).

In my script I generate a command in a variable like nvram set option='value with spaces' but when it is executed it complains that arguments are invalid. If I echo the same command and copy/paste it into the terminal it works fine.

#!/bin/sh

client="client1" option="desc" value="Client number 4"

cmd="nvram set vpn_${client}_${option}='${value}'" echo $cmd; echo $cmd;

When running this script, it outputs the following:

nvram set vpn_client1_desc='Client number 4'
usage: nvram [get name] [set name=value] [unset name] [show] [commit] [save] [restore] [erase] [fb_save file]
usage: nvram [save_ap file] [save_rp_2g file] [save_rp_5g file] [save_rp_5g2 file]

The first line is my echo of the command and the two next are output from the nvram set command which complains about the arguments. It works if spaces in the value are omitted.

What am I missing here?

PS. My router is an ASUS RT-AC87U running latest version of Merlin firmware.

ulvesked
  • 101
  • 1
    https://mywiki.wooledge.org/BashFAQ/050 – ilkkachu Apr 17 '21 at 11:20
  • Use functions to store commands: cmd() { nvram set vpn_${client}_${option}='${value}'; } and then run var=$(cmd); echo "$var" . (echo "$(cmd)" is useless, you could just run cmd directly instead of capturing the output and printing that out in two steps.) – ilkkachu Apr 17 '21 at 11:22
  • Thank you so much. As a programmer not very familiar with shell scripting this is a new way to think. The natural stuff was to execute with backticks as I did in PHP or using process.spawn in node. I got it working now by calling nvram set vpn_${client}_${option}="$value" directly. It appears that single quotes were an issue so double quotes solved it. – ulvesked Apr 17 '21 at 11:56
  • yes, if you run nvram set vpn_${client}_${option}='$value', the variable $value isn't expanded. That's the difference between double and single quotes, one expands, the other doesn't. (Then again, if you did cmd="nvram set vpn_${client}_${option}="${value}"", that would be completely different again, the result wouldn't have any quotes within it, since the second " terminates the first and so for the two others.) https://mywiki.wooledge.org/Quotes and https://unix.stackexchange.com/questions/503013/what-is-the-difference-between-and-quotes/503014#503014 – ilkkachu Apr 17 '21 at 13:19

0 Answers0