11

I'm testing a short bash script. I'd like to execute a string as a command.

#!/bin/bash

echo "AVR-GCC"
$elf=" main.elf"
$c=" $main.c"
$gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"
eval $gcc
echo "AVR-GCC done"

I know it's ugly and all, but shouldn't it execute the avr-gcc command? The errors are the following:

./AVR.sh: line 4: = main.elf: command not found
./AVR.sh: line 5: = .c: command not found
./AVR.sh: line 6: =avr-gcc -mmcu=atmega128 -Wall -Os -o : command not found
jww
  • 97,681
  • 90
  • 411
  • 885
erbal
  • 727
  • 3
  • 12
  • 32
  • 2
    You should not have dollar signs left of the assignment: i.e. `elf=" main.elf"` not `elf=" main.elf` – imp25 Nov 05 '13 at 10:02

2 Answers2

17

I don't know what your final goal is, but you might instead consider using the following more robust way: using arrays in bash. (I'm not going to discuss the several syntax errors you have in your script.)

Don't put your commands and its argument in a string as you did and then eval the string (btw, in your case, the eval is useless). I understand your script as (this version will not give you the errors you mentioned, compare with your version, especially there are no dollar signs for variable assignments):

#!/bin/bash

echo "AVR-GCC"
elf="main.elf"
c="main.c"
gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf $c"
eval $gcc
echo "AVR-GCC done"

You'll very soon run into problems when, for example, you encounter files with spaces or funny symbols (think of a file named ; rm -rf *). Instead:

#!/bin/bash

echo "AVR-GCC"
elf="main.elf"
c="main.c"
gcc="avr-gcc"
options=( "-mmcu=atmega128" "-Wall" -"Os" )
command=( "$gcc" "${options[@]}" -o "$elf" "$c" )
# execute it:
"${command[@]}"

Try to understand what's going on here (I can clarify any specific points you'll ask me to), and realize how much safer it is than putting the command in a string.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
5

You don't use the dollar sight when creating variables, only when accessing them.

So change

$elf=" main.elf"
$c=" $main.c"
$gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"

to

elf=" main.elf"
c=" $main.c"
gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621