在Bash中将命令作为字符串执行

时间:2021-11-06 01:09:01

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

我正在测试一个简短的bash脚本。我想执行一个字符串作为命令。

#!/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-gcc命令吗?错误如下:

./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

2 个解决方案

#1


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.)

我不知道您的最终目标是什么,但是您可以考虑使用以下更健壮的方法:在bash中使用数组。(我不会讨论您的脚本中存在的几个语法错误。)

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):

不要像以前那样将命令和它的参数放在一个字符串中,然后对字符串求和(顺便说一句,在您的例子中,eval是没有用的)。我理解你的脚本(这个版本不会给你你提到的错误,与你的版本相比,特别是没有变量赋值的美元符号):

#!/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:

例如,当您遇到带有空格或有趣符号的文件时(请考虑一个名为;rm rf *)。而不是:

#!/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.

试着理解这里发生了什么(我可以澄清您将要求我做的任何特定点),并认识到这比将命令放在字符串中要安全得多。

#2


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"

#1


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.)

我不知道您的最终目标是什么,但是您可以考虑使用以下更健壮的方法:在bash中使用数组。(我不会讨论您的脚本中存在的几个语法错误。)

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):

不要像以前那样将命令和它的参数放在一个字符串中,然后对字符串求和(顺便说一句,在您的例子中,eval是没有用的)。我理解你的脚本(这个版本不会给你你提到的错误,与你的版本相比,特别是没有变量赋值的美元符号):

#!/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:

例如,当您遇到带有空格或有趣符号的文件时(请考虑一个名为;rm rf *)。而不是:

#!/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.

试着理解这里发生了什么(我可以澄清您将要求我做的任何特定点),并认识到这比将命令放在字符串中要安全得多。

#2


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"