0

shell on a router, only /bin/sh, don't have bash, can't use bash's array a.sh:

#!/bin/sh
ARGS="-v -b 'b.sh start' -c 'b.sh stop' " # or '-v -b "b.sh start" -c "b.sh stop" '
PROC="./a.out"  # a.out is c program
$PROC $ARGS

in a.c

int main(int argc, char* argv[]) {
    for (int i = 0; i < argc; ++i){
        fprintf(stderr, "%s\n", argv[i]);
    }
}

gcc a.c

sh ./a.sh

./a.out
-v
-b
'b.sh
start'
-c
'b.sh
stop'

but ok when: ./a.out -v -b 'b.sh start' -c 'b.sh stop'

./a.out
-v
-b
b.sh start
-c
b.sh stop

How can I do right?

Thomas
  • 17
  • 2
  • https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia is Right. eval "$PROC $ARGS" is ok – Thomas Sep 01 '20 at 04:18
  • @Thomas : You correctly recognized that not having arrays is biting you. `sh` has instead a pretty inconvenient array replacement: Using the [`set` command](https://man7.org/linux/man-pages/man1/set.1p.html), you can re-assign your positional parameters, which gives you kind of an anonymous array, which you then can pass to your application using `"$@"`, which would preserve the spaces. – user1934428 Sep 01 '20 at 05:34

0 Answers0