简体   繁体   中英

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

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 . However, I want to save that output in a variable.

# 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. How you do this depends on the command. Since you don't specify, here is an example with 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 (!):

/command -in input_filename -of output_filename

Most commands, if you do not specify -of options will send the output to 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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