LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2017, 12:55 AM   #1
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Rep: Reputation: Disabled
How to Assign Script Output to a Variable


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?
 
Old 11-28-2017, 02:28 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?
 
Old 11-28-2017, 05:22 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Something like this?
Code:
#!/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
 
Old 11-28-2017, 07:31 AM   #4
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
Try this
Code:
Script=$(wc -l /etc/motd)
Tmp=$(tmpfile)

cat > $Tmp <<DONE
line 1
Line 2
Line 3
DONE

echo $Script >> $Tmp

cat $Tmp | mail
 
Old 11-28-2017, 07:40 AM   #5
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
Or even better
Code:
Tmp=$(tempfile)

cat > $Tmp <<DONE
Line 1
Line 2
Line 3
DONE

wc -l /etc/motd >> $Tmp
cat $Tmp | mail
 
Old 11-28-2017, 09:52 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by ep7network0819 View Post
I tried using a variable named “output=$(./scrip2.sh)” and from line 4 I call variable using “Body:$output” and script does nothing.
This sounds close to correct, but it's hard to say for sure since you didn't actually post any code or any results.

Code:
output=$(/path/to/script)
echo "Body: $output" >> file.txt
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.
 
Old 11-28-2017, 10:11 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 11-28-2017, 09:28 PM   #8
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-28-2017, 10:04 PM   #9
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
Your grep line will hang waiting for input from stdin change it to
Code:
 grep "$line" "$file"
. Pressing CTRL-D will send the EOF signal/symbol to grep. I also doubt you actually want this line
Code:
 while IFS=read line; do
never reset variables like IFS unless that's what you really want to do
 
Old 11-28-2017, 10:29 PM   #10
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
Hi rhubarbdog,

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?
 
Old 11-28-2017, 10:37 PM   #11
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
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
 
Old 11-28-2017, 10:43 PM   #12
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
is this not what you want
Code:
#!/bin/bash

var="IT Department"
file="/some/location/in/space/somewhere/log.txt"
OUTPUT=$(/some/location/in/space/somewhere/script.sh)

email=$(tempfile)
tmp=$(tempfile)

grep 'Failed' "$file" > $tmp

# creating txt file 
echo "from: $var
to: an email address
subject: esxi test server
body: $OUTPUT" >> $email

echo >> $email
cat $tmp >>$email

cat $email |mail -t user@host

Last edited by rhubarbdog; 11-28-2017 at 11:55 PM.
 
Old 11-29-2017, 12:09 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So now with code your example makes more sense.

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)
 
Old 11-29-2017, 12:27 AM   #14
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-29-2017, 12:59 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by grail View Post
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)
please try this.
 
  


Reply

Tags
bash scripting, grep, read, script, shell scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
assign output of a function to a variable iFunction Linux - Newbie 2 03-04-2016 03:44 PM
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
Can not assign variable to output of sed protocol Linux - General 5 10-24-2011 04:36 PM
How to assign a variable to the output value of a program BeyondSora Linux - Newbie 3 02-07-2011 12:46 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration