0

In the Bismon static source code analyzer (GPLv3+ licensed, git commit 49dd1bd232854a) for embedded C and C++ code (using a plugin for GCC 10 straight compiler on Debian bookworm for x86-64) I have a test Bash script Hello-World-Analyze which uses a GNU array variable bismon_hello_args.

That variable is declared (at line 56) using:

declare -a bismon_hello_args

and I would like to fill that bismon_hello_args array variable from script arguments starting with --bismon, and later invoke the bismon executable (compiled from C source files) with several arguments to its main being the elements of that bismon_hello_args array variable.

So if my Hello-World-Analyze script is invoked as Hello-World-Analyze --bismon-debug-after-load --bismon-anon-web-cookie=/tmp/bismoncookie --gcc=/usr/local/bin/gcc-11 I want the bismon ELF executable to be started with two arguments (so argc=3, in C parlance) : --bismon-debug-after-load followed by --bismon-anon-web-cookie=/tmp/bismoncookie

For some reason, the following code (lines 58 to 64) in that Hello-World-Analyze script:

for f in "$@"; do
    case "$f" in
    --bismon*) bismon_hello_args+=$f;;
    --asm) export BISMON_PLUGIN_ASMOUT=/tmp/gcc10_metaplugin_BMGCC.s;;
    --gcc=*) export BISMON_GCC=$(echo $f | /bin/sed -e s/--gcc=//);;
    esac
done

does not work as expected. It should be (and was in a previous git commit e8c3d795bc9dc8) later followed with

./bismon $bismon_hello_args &

But debugging prints show that bismon is invoked with argc=2 so one long argv[1] program argument...

What am I doing wrong?

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Merely `+=` adds a string to an existing string. You probably want `bismon_hello_args+=("$f");;` (notice also the quotes). To call the program, use `./bismon "${bismon_hello_args[@]}" &` (notice the quotes, again). – tripleee Sep 20 '21 at 08:22
  • Thanks. Why was my question closed? If your comment was an answer, I would accept it! – Basile Starynkevitch Sep 20 '21 at 08:23
  • This does not seem to be closed yet, but basic syntax questions usually get closed as "typo / unreproducible" or as duplicates of previous similar questions. I could not find a suitable duplicate so I voted to close as typo. – tripleee Sep 20 '21 at 08:25
  • It is not a syntax question, but a semantics one.... – Basile Starynkevitch Sep 20 '21 at 08:28
  • The syntax for getting array semantics is different. It's clumsy (mostly for reasons of backwards compatibility with the original Bourne shell) but well documented. – tripleee Sep 20 '21 at 08:31
  • @BasileStarynkevitch : Since `bismon_hello_args` is an array, you should pass its element to another script by `./bismon "${bismon_hello_args[@]}" &`. – user1934428 Sep 20 '21 at 09:28
  • @BasileStarynkevitch : `bismon_hello_args+=$f` catenates to the first element of the array (i.e. to index postition 0). See [here](https://linuxhandbook.com/bash-arrays/) for how to properly handle arrays. – user1934428 Sep 20 '21 at 09:31

1 Answers1

2

Merely += adds a string to an existing string. You probably want bismon_hello_args+=("$f");; (notice also the quotes). To call the program, use ./bismon "${bismon_hello_args[@]}" & (notice the quotes, again).

The syntax to use an array variable is different than the syntax for simple scalars. This syntax was inherited from ksh, which in turn needed to find a way to introduce new behavior without sacrificing compatibility with existing Bourne shell scripts.

Without the array modifiers, Bash simply accesses the first element of the array. (This confuses beginners and experienced practitioners alike.)

tripleee
  • 175,061
  • 34
  • 275
  • 318