bash 在bash中为变量分配文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46929684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:33:13  来源:igfitidea点击:

Assigning a variable a filename in bash

bash

提问by the_mamba

I am writing a script to generate an executable (arm executable) in Linux by taking in user-specified .s file. So the user enters an input file, say "input.s" and an output file name, say "output.axf" and the script generates the desired output (executable - .axf). Now I want an additional option wherein, if the user does not give an output filename in the arguments, I want to create a default output file myself. The script is as follows:

我正在编写一个脚本,通过接收用户指定的 .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

Now my problem is that every time I run the script and do not specify anything for $output_file, I get this error:

现在我的问题是,每次我运行脚本并且没有为 $output_file 指定任何内容时,我都会收到此错误:

./script_test.sh: line 12: =brot.axf: command not found

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

Fatal error: L3901U: Missing argument for option 'o'.

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

However, when I do specify the input and output file names with extensions, it works as expected. How do I fix the error and assign a default name to the output file if the user doesn't assign one ?

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

回答by Inian

Variable assignments don't take the $notation in bashshell. You just need below without the $

变量赋值不采用shell 中的$符号bash。你只需要下面没有$

output_file="brot.axf"

And later in the script if filenameis a variable and trying to construct a name with .oappended, enclose the variable name within {}, so that the variable is expanded properly

稍后在脚本中 iffilename是一个变量并尝试构造一个带有.o附加的名称,将变量名称括在 中{},以便正确扩展变量

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

Also by the looks of it you have a likely typo in the filenameas variable ifilename. If you care trying to use it double-quote it as above.

此外,从它的外观来看,您可能在filenameas 变量中有一个错字ifilename。如果您想尝试使用它,请像上面一样将其双引号。

回答by Pierre Fran?ois

You have to remove the $ in front of output_file on line 12.

您必须删除第 12 行 output_file 前面的 $。

output_file=brot.axf