Storing outputs into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing outputs into variables
# 1  
Old 05-03-2011
Storing outputs into variables

I need to know how to store output from one command so that it can initiate another command.
Code:
chktraf -s | cut -c1-4

output would look like
Code:
321
142
256
342
123

Then if the value of the output = 0, then initiate next command.
Code:
if [b=0]
then
  for xx in 01 02 03 04 05 06 07 08 09 10
  do
    siteadm -u vp$xx
  done
else
  break
fi

I know the for loop is good, because it works. But the if statement is not correct. Any help would be greatle appreciated.

Last edited by Franklin52; 05-03-2011 at 01:10 PM.. Reason: Please indent your code and use code tags
# 2  
Old 05-03-2011
If syntax

You need spaces on either side of the square brackets.

Oh and you need to test $b (assuming b holds the value you want to test for). Note the dollar sign.
# 3  
Old 05-03-2011
For storing the value, try this
Code:
b=`chktraf -s | cut -c1-4`

or
Code:
b=$(chktraf -s | cut -c1-4)

And if loop doesn't work, reason as gary_w said, space is the issue
Code:
if [ $b -eq 0 ]
then
  ...
fi

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 4  
Old 05-03-2011
Syntax?

I am new to shell programming. I knw my syntax is not great, which is due to my lack of experience. If anyone can look over the following script and critique it, it would be much appreciated.



Code:
z=$(chksite -s | cut -c3-4) #Stores variable z as number of active vp's
export $z
 
 
y=$(chktraf -s | cut -c39-41) #cut the channels in use column here
echo $y #Stores number of channels in sevice per vp
if [ $y=0 ] # If active channels = 0, perform siteadm -u on active vp's
 
then
for $z=xx
do
siteadm -u vp$xx
wait # Will not proceed until all processes are complated
 
for $z=xx # reboots only active vp's
do
chgstate -m vp$xx reboot
wait
 
for $z=xx #pings only active vp's
echo ping vp$xx
wait
done
 
else
break
fi

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-03-2011 at 05:12 PM.. Reason: please start using code tags!
# 5  
Old 05-08-2011
This has got lot of syntax error.
Please check the previous post for how to use "if".
"for" loop construct you have used is wrong. One type is
Code:
for val in values
do
  #do something with $val
done

You have 3 for loops but only one done?? every for loop should end with a done.

Let ask now the functionality you are looking for and correct the scripts.

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

2. UNIX for Dummies Questions & Answers

multiple variables assignement (stdout/stderr outputs)

Hi all, I've been looking around for this for a while and can't seem to find a satifactory way to do what I want: I would like to assign the output of stdout to a variable and that of stderr to another one, and this without using temporary files/named pipes. In other words be able to assign... (4 Replies)
Discussion started by: anthalamus
4 Replies

3. UNIX for Dummies Questions & Answers

Storing variables and using them..

Hello Apologies for not having the most accurate of thread titles.. I'm using IBM Rational Synergy CM software. I use the Synergy commands in tandem with Unix commands. I have a directory containing source code objects: bash-3.00$ ccm ls *.fmb *.rdf *.pll *.mmb cre_applications.fmb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

4. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

5. Shell Programming and Scripting

Script that takes in variables, and outputs to another text or script file

Ok, I sort of need to create a command files that will be ftped to another server to run. I have some input variable that will need to be read, and then transformed into another script file. Here are some examples. Server 1: outputCmd.sh passing in ./outputCmd.sh nh8oaxt Release_4_0... (1 Reply)
Discussion started by: orozcom
1 Replies

6. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

7. Shell Programming and Scripting

Storing commands in $variables.

Hi I'm trying to store commands in variables... like so.. # lastcmd=" $t1 | $t2 | $t3 | $t4 | sort | uniq" t1="sed -e 's/http:/<li><a href=\"http:/'" t2="sed -e 's/http:.*/&\">&<\/a>Web Link<br>/'" t3="sed -e 's/.*. mailto:/<li><a href=\"mailto:/'" t4="sed -e... (7 Replies)
Discussion started by: Paulw0t
7 Replies

8. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

9. Shell Programming and Scripting

Splitting the data and storing it into 2 variables

Hi, I have a file in the given format: |aaa |hhh |fat hat chat |make sure I need to get store in a variable,say 'error' the value of the string 'fat hat chat' In short, echo $error should give the result fat hat chat Is this possible using awk? Also, can this be split.For... (10 Replies)
Discussion started by: jisha
10 Replies

10. Shell Programming and Scripting

awk - storing data in variables

In AWK script how do I store data in variables for later use. I have a multiline input and I do not want to print the data read on the console Thnaks in advance. Nilotpal. (7 Replies)
Discussion started by: 2nilotpal
7 Replies
Login or Register to Ask a Question