简体   繁体   English

Shell 脚本:有没有办法使用变量作为 output 而不是可执行文件?

[英]Shell Scripting: Is there a way to use variable as output instead of a file for executable?

For a command like:对于像这样的命令:

/command -in input_filename -of output_filename

is it possible to use a variable instead of output_filename是否可以使用variable而不是output_filename

I have tried redirections but that didn't work我尝试过重定向,但没有用

Edit:编辑:

After reading comments and answers I feel perhaps the question is confusing.阅读评论和答案后,我觉得这个问题可能令人困惑。 Executable expects filename for its output .可执行文件需要其output的文件名。 However, I want to save that output in a variable.但是,我想将output保存在一个变量中。

# Declare your variable at
# some point earlier in script
variable="somevalue.txt"

.
.
.

# Use it later in script
command -f $variable

Good practice, declare variable as readonly. 好的做法,将变量声明为只读。 This way you do not accidently overwrite it's value at some later point in script. 这样,您就不会在脚本稍后的某个地方意外覆盖它的值。

# Declare as readonly
readonly variable="somevalue"

You can not specify a variable as an output file and then have it populated. 您不能将变量指定为输出文件,然后将其填充。

Instead, have the program write to stdout and capture it. 而是让程序写入stdout并捕获它。 How you do this depends on the command. 您如何执行此操作取决于命令。 Since you don't specify, here is an example with curl : 由于您未指定,因此以下是curl的示例:

# Many programs automatically write to stdout when a file is not specified
myvar=$(curl http://stackoverflow.com)

# Many programs accept - to mean stdout for output files
myvar=$(curl -o - http://stackoverflow.com)

# Otherwise, you can often specify /dev/stdout
myvar=$(curl -o /dev/stdout http://stackoverflow.com)

If this isn't possible because the command doesn't have clean output, you may be forced to write it to a temporary file and read it back. 如果由于命令没有干净的输出而无法执行此操作,则可能会迫使您将其写入临时文件并重新读取。

Save the output of a command: 保存命令的输出:

# Capture output into variable
output="$(command -f filename)"

# Print it / do whatever you want with it
echo "$output"

Edit: 编辑:

If the command requires this format: 如果命令要求这种格式:

/command -in input_filename -of output_filename

But, you also want to capture the contents of what goes to the output file in a variable, you can read the output file into a variable once the original command is done. 但是,您还希望捕获变量中输出文件的内容,可以在完成原始命令后将输出文件读入变量。

output="$(cat output_filename)"

This would come after the original command because the output file must be written before you can read it again. 这将出现在原始命令之后,因为必须先编写输出文件,然后才能再次读取它。

Your question is a bit unclear, but with your last comments, I guess you want this: 您的问题尚不清楚,但是在最后的评论中,我想您想要这样:

#!/bin/bash
#
variable=$(./command -f $inputfile)

echo $variable

This will print the result output from the command. 这将打印命令输出的结果。 Note that this does not store the status of the command, it stores the output text (if any). 请注意,这不存储命令的状态,而是存储输出文本(如果有)。


For version 2 of your question (!): 对于您的问题的第2版(!):

/command -in input_filename -of output_filename

Most commands, if you do not specify -of options will send the output to STDOUT. 如果不指定-of选项,大多数命令会将输出发送到STDOUT。 So use the first method above. 因此,请使用上面的第一种方法。

If you have to use that, the output must go to a file. 如果必须使用它,则输出必须转到文件中。 So read the file into a variable then, after the command is done. 因此,在完成命令后,将文件读入变量。

variable=$(cat output_filename)

Note that you will loose the format of the file, hopefully it is a single line file. 请注意,您将丢失文件的格式,希望它是单行文件。

There's a way to do this using special brackets.有一种方法可以使用特殊括号来做到这一点。 I believe {} and back ticks (near the 1 key) Something like我相信 {} 和反勾号(靠近 1 键)之类的

VAR1=${`cat /file`} but that's formatted incorrectly.  

I can't remember the exact format which is why I'm here but doing it that way is possible if it's formatted right.我不记得确切的格式,这就是我来这里的原因,但如果格式正确,这样做是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM