How to put output of one command into a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to put output of one command into a variable
# 1  
Old 03-31-2010
How to put output of one command into a variable

Hi,
Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA.


~~~~~~~~~~~~~~~

Code:
> cat /home/state
CA
 
> cat /home/list
CA 100 50 20
AUS 120 61 10
 
> cat myscript
#!/bin/sh
st='cat /home/state'
grep $st /home/list
 
> ./myscript    # this is the output I want.
CA 100 50 20


Last edited by Franklin52; 03-31-2010 at 04:32 AM.. Reason: Please use code tags.
# 2  
Old 03-31-2010
Quote:
Originally Posted by joker_789us
Hi,
Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA.


~~~~~~~~~~~~~~~

Code:
> cat /home/state
CA
 
> cat /home/list
CA 100 50 20
AUS 120 61 10
 
> cat myscript
#!/bin/sh
st='cat /home/state'
grep $st /home/list
 
> ./myscript    # this is the output I want.
CA 100 50 20

If your grep allows the -f flag, then you could do this

Code:
#!/bin/sh
grep -f /home/state /home/list

# 3  
Old 03-31-2010
How do I get/compare the $st value, in "if-then" statement?

Code:
> cat myscript1
#!/bin/sh
st='cat /home/state'
if [ $st == "CA" ]
then
   do_this
else
   do_that
fi


Last edited by pludi; 03-31-2010 at 05:57 AM.. Reason: code tags, please...
# 4  
Old 03-31-2010
Try this:

Code:
#!/bin/sh
st='cat /home/state'
if [ "$st" = "CA" ]
then
do_this
else
do_that
fi


Last edited by pludi; 03-31-2010 at 06:10 AM.. Reason: code tags, please...
# 5  
Old 03-31-2010
I tried that before. it will run the "do_that" part, instead of "do_this".
If I add a line after "do_that" e.g. echo "$st", I got "cat /home/state" (without the double quote).

Code:
> ./myscript1
cat /home/state
¨


Last edited by vbe; 03-31-2010 at 07:31 AM.. Reason: code tags
# 6  
Old 03-31-2010
Try this
st=`cat /home/state`
instead of
st='cat /home/state'
# 7  
Old 03-31-2010
Great! it works.
Thanks to Jairaj and Vino.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script variable $1 used with put command

I have the following script used, i am new to shell scripting. tryign to understand. in the put $BASE_FOLDER/$base_name holds which path. What does it mean by $1 second path in put command is it constructing this path: /user/hive/warehouse/stage.db/$1 what is $1 holding in above path. ... (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

3. UNIX for Advanced & Expert Users

ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE. I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it. #! /bin/sh # Start the newest... (2 Replies)
Discussion started by: binary-ninja
2 Replies

4. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

5. Shell Programming and Scripting

set variable to command output

I'm hoping you guys can help me out here. I've been trying different methods to try and get what IW as hoping would be a fairly simple script but has turned into a pain. Bit of background - I am writing a script to check values in certain failes to ensure they are corerct. I'm runnign this on... (2 Replies)
Discussion started by: stuc
2 Replies

6. Programming

Command output into a variable

Hi, with this command: cu -l /dev/ttyACM0 -s 9600 > name.txt I put the output of the port in a txt Is posible to do the same (or similar) in a var directly, inside a C program? cu -l /dev/ttyACM0 -s 9600 > variable ? I have trying this withs pipes, but i dont know how to... (6 Replies)
Discussion started by: daaran
6 Replies

7. Shell Programming and Scripting

get characters from output of a command in a variable

Hi, i have two questions, I am new to programming 1. I have an output of a command and i want to get some specific part of it in a variable. i am trying sr=`some comand xyz| grep 'Last Changed Rev:' | cut -c19-` now variable sr gets a end of line character at end. output of the command... (3 Replies)
Discussion started by: muaz
3 Replies

8. Shell Programming and Scripting

Put the output of grep in a variable

Hi, in a shell script how can I put the result of a grep command in a variable : myvariable=grep mystring myfilename Thank you. (3 Replies)
Discussion started by: big123456
3 Replies

9. UNIX for Dummies Questions & Answers

How to store output in variable when put in background

Hi, How do I store following command output: export RESULT=`date` & It works when I do : export RESULT=`date` But what I need is when command put it background, I also need that output going to RESULT variable. Is there any way ? Thanks Sanjay (1 Reply)
Discussion started by: sanjay92
1 Replies

10. Shell Programming and Scripting

Command output to a variable.

With cut -c 8-13 myfile, I am getting some numeric value. In my shell script I am trying to assign something like this, var=cut -c 8-13 myfile But at the time of execution I am getting -c is not found. If I dont assign, then script executes well. Can we not simply use the value from one... (8 Replies)
Discussion started by: videsh77
8 Replies
Login or Register to Ask a Question