[SOLVED] How to Assign Script Output to a Variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I’m trying to capture script output and save it to a variable using only one script. When I run the script, it reads a log file and if failed is found, the script creates four lines in a text file.
The four text lines are:
1) From: a server
2) To: send email to some IT guy
3) Subject: HD failure
4) Body: $Script output should go here!
I’m trying to copy script output and save result to line 4. I would like to use a variable, but I’m having trouble saving script output and placing it in a variable using only one script.
I’ve tried several different things. I tried re-directing output using “./script > file.txt” but output results are placed on top of text file. Results need to be on line 4. I tried using a variable named “output=$(./scrip2.sh)” and from line 4 I call variable using “Body:$output” and script does nothing.
How can I run a second script and save results in a variable and call the variable using only one script?
I am a bit lost at the whole 'only one script' idea?
You run a script which outputs 4 lines. If this is the conclusion of the script I am not sure how the one script being run has anything to do with your
issue of getting output into a variable??
Maybe you could explain a little further what your requirement is?
#!/bin/sh
Script_output=$(wc -l /etc/motd) # edit this line accordingly
cat <<DONE | mail
From: a server
To: send email to some IT guy
Subject: HD failure
Body: ${Script_output} should go here!
DONE
will capture the output from /path/to/script into the variable "output", and then print "Body: " plus the contents of the variable to the file file.txt. It sounds like this didn't work for you, which means we need to see the actual code you tried to know why.
Just to clarify: every process has three "standard" I/O streams: STDIN, STDOUT, STDERR. These streams are often "piped" from one to the next, e.g. using the "|" symbol in any Shell command. (A mechanism known in Linux circles as a FIFO, for "first-in first-out queue.") This is exactly what the shell is doing here: it is running the command as a child process, and "piping" its STDOUT stream back to the parent, then reading the other end of that pipe and placing the data thus collected into a variable for your amusement and amazement.
FIFO queues ("pipes") enable the processes to run asynchronously: as one process writes, the other process may immediately read what is written. Reading processes wait for data to read, and writing processes wait whenever the pipe becomes full. From the point-of-view of any process, a pipe appears exactly like a sequential file.
The STDOUT stream is intended for "normal output" while the STDERR stream is intended for error and status messages. The two streams can be combined so that they will interleave their output (in an unpredictable sequence) into the same queue.
Last edited by sundialsvcs; 11-28-2017 at 10:13 AM.
Sure no problem, I'll post my code. Script is below:
Code:
#!/bin/bash
var="IT Department"
file="/some/location/in/space/somewhere/log.txt"
OUTPUT=$(/some/location/in/space/somewhere/script.sh)
while IFS= read line; do
if [[ $line =~ Failed ]];
then
# Reading log.txt for Failed
grep "$line" && "$file"
# creating txt file
echo "from: $var
to: an email address
subject: esxi test server
body:$OUTPUT" >> somefile.txt
fi
done < "$file"
When I run script the terminal does nothing, the cursor blinks a few times and disappears. I then enter Ctrl+c to end program. I receive no error message, so its hard to figure out what the problem is.
My goal is to run this script, capture output results in a variable, and call the variable in body:$OUTPUT".
I can't seem to figure this out. Any suggestions in making this script work. Thanks for everyone's help.
Last edited by ep7network0819; 11-28-2017 at 09:29 PM.
Reason: Miss spelled word
I made a few changes to the script. Updated changes to script is below:
Code:
#!/bin/bash
var="IT Department"
file="/some/location/in/space/somewhere/log.txt"
OUTPUT=$(/some/location/in/space/somewhere/script.sh)
while read line; do
if [[ $line =~ Failed ]];
then
# Reading log.txt for Failed
grep "$line" "$file"
# creating txt file
echo "from: $var
to: an email address
subject: esxi test server
body: $OUTPUT" >> somefile.txt
fi
done < "$file"
I ran the script with updated changes and it continues to hang. How can I capture script output and save it to a variable using this script?
Have you tried typing something like "Failed - this is a test line" and press enter, i think your grep inside a while is poor logic i'm going to try and write you a correct script
1. Place set -xv after the shebang at the top and you will at least see where you are getting up to prior to the hang (ie. it could be in script.sh which we currently know nothing about)
2. Your while loop is searching each line of the 'file' for Failed, but you then grep for the same information ... to what end?
3. Assuming you are only outputting information when if is true, you could also use the following:
Code:
done < "$file" >> somefile.txt
4. If you find Failed more than once is it your intention to put the exact same information in the output file each time? (this is the current scenario)
Script is almost working. I was able to find out why script would hang. I temporarily removed OUTPUT variable. I will need to fix that part of the script later.
I used the tee command to take standard input and copy it to standard output. I almost achieved what I want to do. Below is updated script. I will have to fix OUTPUT variable before script is completed. For now this is what I have so far. The email part has nothing to do with this script.
Code:
#!/bin/bash
var="IT Department"
file="/some/location/in/space/somewhere/log.txt"
while read line; do
if [[ $line =~ Failed ]];
then
# Reading log.txt for Failed
grep "$line" "$file"
# creating txt file
echo "from: $var
to: an email address
subject: esxi test server
body: $OUTPUT" >> somefile.txt
fi
done < "$file" | tee testfile.txt
When I ran updated script, the text file somefile.txt did what it was intended to do. Output result of somefile.txt looks like this,
from:IT Department
to: an email address
subject: esxi test server body:!!ITS BLANK HERE!!I want script output to appear here!!
The testfile.txt was able to copy the standard output of the script using the command tee. That is what I want. Now I need to put that into a variable, and place the variable into the text file body. That is what I am trying to do. I'm not trying to do anything else.
Last edited by ep7network0819; 11-29-2017 at 12:31 AM.
Reason: Fixed wrong text file.
1. Place set -xv after the shebang at the top and you will at least see where you are getting up to prior to the hang (ie. it could be in script.sh which we currently know nothing about)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.