在bash中为变量分配文件名

在bash中为变量分配文件名,bash,Bash,我正在编写一个脚本,通过接收用户指定的.s文件,在Linux中生成一个可执行文件(arm可执行文件)。因此,用户输入一个输入文件,比如说“input.s”和一个输出文件名,比如说“output.axf”,脚本生成所需的输出(可执行文件-.axf)。现在我需要一个附加选项,如果用户在参数中没有给出输出文件名,我想自己创建一个默认输出文件。脚本如下: #!/bin/bash echo Enter the names of the input file and output file read inp

我正在编写一个脚本,通过接收用户指定的.s文件,在Linux中生成一个可执行文件(arm可执行文件)。因此,用户输入一个输入文件,比如说“input.s”和一个输出文件名,比如说“output.axf”,脚本生成所需的输出(可执行文件-.axf)。现在我需要一个附加选项,如果用户在参数中没有给出输出文件名,我想自己创建一个默认输出文件。脚本如下:

#!/bin/bash
echo Enter the names of the input file and output file
read input_file output_file

if [ -z "$input_file" ] 
    then
        echo "No input supplied"

elif [ -z "$output_file" ]
    then
        $output_file=brot.axf

elif [ -z "$input_file" && -z "$output_file" ]
    then
        echo "No input/output file supplied"
fi

ifilename=$(basename "$input_file")
ifilename="${input_file%.*}"

armasm -g --cpu=8-A.64 "$input_file"
armlink "$ifilename.o" -o "$output_file"
fromelf --test -c $output_file > disassembly.txt
现在我的问题是,每次我运行脚本并且没有为$output\u文件指定任何内容时,我都会遇到以下错误:

./script_test.sh:第12行:=brot.axf:未找到命令

致命错误:L3901U:缺少选项“o”的参数

但是,当我使用扩展名指定输入和输出文件名时, 它按预期工作。
如果用户没有指定默认名称,如何修复错误并为输出文件指定默认名称

变量赋值不采用
bash
shell中的
$
符号。您只需要在下面输入,而不需要
$

output_file="brot.axf"
在脚本的后面,如果
filename
是一个变量,并且试图构造一个附加了
.o
的名称,请将变量名括在
{}
中,以便正确展开变量

armlink "${filename}.o" -o "$output_file"

另外,从外观上看,
filename
作为变量
ifilename
可能有一个输入错误。如果您愿意尝试使用它,请如上所述对其进行双引号引用。

您必须删除第12行输出文件前面的$

output_file=brot.axf

在shellcheck.net中复制并粘贴脚本。乍一看,我发现有几处语法冲突。1) 变量赋值在
bash
,2中的LHS上没有
$
。Unterminated
我在将脚本放入shellcheck.net后对其进行了更改。这是一个有用的工具。谢谢