How to copy script output to a variable using same script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy script output to a variable using same script?
# 1  
Old 11-29-2017
How to copy script output to a variable using same script?

I'm trying to copy script output and use it in this same script as a variable, and call the variable when script is compiled.
The script is below.
Code:
#!/bin/bash

output=$(script)

while read line; do
      if [[ $line =~ Failed ]];
      then
      grep "$line" logfile.txt
# Create text file
echo "From: IT 
Subject: Failure found
Body: $Output from this script goes here" >> myFile.txxt

fi
done  < logfile.txt >> myFile

If I use syntax OUTPUT=$(/usr/local/bin/script.sh) and run the script it hangs and does nothing.
# 2  
Old 11-29-2017
We will assume that you understand that invoking script and invoking script.sh do not run the same code.

Most BSD, Linux, and UNIX systems have a utility named script installed. If you name your shell script script, whether you will invoke your shell script or the system's script utility will depend on which will be found first in the directories named by the PATH variable in your current shell execution environment.

If your script is named script and the directory containing your script is found in the PATH environment variable before the system's script utility, having the first thing that your code does be:
Code:
output=$(script)

creates a recursive sequence of invocations of your script until you exceed that number of processes you are allowed to have running with your user ID, the system runs out of available resources to create another copy of your process, or a system administrator kills your job when other users on your system complain that you are slowing down their jobs by unreasonably consuming all available system resources.
# 3  
Old 11-30-2017
The script is actually named read7.sh. It's not named script. I just wanted to emphasize that I'm running a script to capture it's output. Thanks for the valuable information.
# 4  
Old 11-30-2017
Why don't you show us your actual script so we can see what it is really doing? There are obvious inconsistencies and/or misspellings that we don't know if they are typos in the way you have tried to emphasize features in your script or actual errors in your script.
# 5  
Old 11-30-2017
It would help if the target were known - what do you want to achieve? Your code with the while loop and the grep gives wide room for interpretation and guesses - it doesn't realy make sense to me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Oracle output to an array variable in script

Hi- I am returning output of an query into a array variable in my shell script. set -A DATE_RANGE `sqlplus -s **/**@** << EOF set termout off set echo off set serveroutput off set pagesize 0 set linesize 500 set heading off set verify off set feedback off select... (1 Reply)
Discussion started by: vputtas@gmail.c
1 Replies

2. Shell Programming and Scripting

Getting a variable and output to use later on in script

Hi Guys! I'm trying to get this script going but running into a little issue. not sure what is going on. basicly how i'm writing this script is to ask you what is the password you want to be encrypted, then ask you what the user id is, takes that info run's the encryption script with the... (2 Replies)
Discussion started by: vpundit
2 Replies

3. Shell Programming and Scripting

Output of matlab as a variable for shell script

I'm running a matlab code within a shell script. This is how I do it, matlab -nodesktop -nosplash -nojvm -r "my_program;quit" This works fine. My matlab code prints out a single number, say "ans = 10" for example. I want to assign this to a variable in the shell script. I tried doing this... (18 Replies)
Discussion started by: lost.identity
18 Replies

4. Shell Programming and Scripting

Capturing output of procedure in variable in shell script

Hi guys I am calling one DB2 stored proc through unix. It is giving me below output. I want to capture the value 150 in one UNIX variable in shell script. Please let me know how I can achieve this. Thanks in advance Value of output parameters -------------------------- Parameter Name :... (5 Replies)
Discussion started by: vnimavat
5 Replies

5. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

6. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

7. Shell Programming and Scripting

Parse file and copy value to variable in sh script.

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given... (6 Replies)
Discussion started by: script2010
6 Replies

8. Shell Programming and Scripting

how to assign the output of the interective script to the variable

Hi, I work in ksh88. I have an interective script which prompts the user for the input and returns numeric value depending on the input provided. I need to call this script inside another script and then assign the resulting output the the variable. The call like that A=`my script` obviously... (6 Replies)
Discussion started by: aoussenko
6 Replies

9. UNIX for Dummies Questions & Answers

Storing lines of output into a script variable

I'm sure this is a simple thing but I can't figure it out. In a script that I'm writing, I'd like to be able to store each line of output from "ls -l" into a variable. Ultimately I'd like to end up with something like: for a in `ls -l` do something with $a doneBut that's reading each... (2 Replies)
Discussion started by: ewoods
2 Replies

10. UNIX for Dummies Questions & Answers

ls command output to variable in script

Hi, I wrote a script to get the oldest file from a directory path (which is passed as a parameter to the script) ######################################################### XMLFILE_PATH={$1} cd $XMLFILE_PATH JPM_FILENAME = `(ls -tr User* | head -1)` #echo $JPM_FILENAME ###### END... (1 Reply)
Discussion started by: dsrookie
1 Replies
Login or Register to Ask a Question