Question On Command in Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question On Command in Variable
# 1  
Old 10-29-2007
Question On Command in Variable

I have been working on a script that executes on a number of different operating systems. As a result I was trying to set a variable or perhaps variable array depending on the OS. I tried the following using eval and such but so far have not had any luck. Is there a way to do something like the following.

DF="df -h | sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' | /usr/xpg4/bin/grep -ivE "filesystem|$FSIGNORE" | awk '{ print $6" "$5}'"

Later in the script do this:

$DF | while read LINE; do ...

I tried a host of different things, like putting eval into the variable and escaping the quotes but it just did not seem to work correctly. Any ideas, or am I better off writing the output of the command to a temp file and reading from the file?

Thanks in advance.
# 2  
Old 10-29-2007
Why not put all of that in a function which you can then call?

Code:
DF()
{
    df -h | sed .....
}

DF | while read ...

also if you want portability don't expect all platforms to have /usr/xpg4/bin/...
# 3  
Old 10-29-2007
I already have the df command broken out for every OS ( I realize I will have to make it different on each one. ) This was only two lines from the entire script.

I will try putting each version of the df into a function, that might work. Thanks for the reply. Any other suggestions are welcome. Smilie
# 4  
Old 10-29-2007
Quote:
Originally Posted by scotbuff
I have been working on a script that executes on a number of different operating systems. As a result I was trying to set a variable or perhaps variable array depending on the OS. I tried the following using eval and such but so far have not had any luck. Is there a way to do something like the following.

DF="df -h | sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' | /usr/xpg4/bin/grep -ivE "filesystem|$FSIGNORE" | awk '{ print $6" "$5}'"

Later in the script do this:

$DF | while read LINE; do ...

I tried a host of different things, like putting eval into the variable and escaping the quotes but it just did not seem to work correctly. Any ideas, or am I better off writing the output of the command to a temp file and reading from the file?

Thanks in advance.
I like porter's idea of using a function, but if you wanted to do it like you've specified, you'd put it in backticks.
Code:
DF=`df -h | sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' | /usr/xpg4/bin/grep -ivE "filesystem|$FSIGNORE" | awk '{ print $6" "$5}'`

# 5  
Old 10-29-2007
Quote:
Originally Posted by earnstaf
I like porter's idea of using a function, but if you wanted to do it like you've specified, you'd put it in backticks.
Code:
DF=`df -h | sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' | /usr/xpg4/bin/grep -ivE "filesystem|$FSIGNORE" | awk '{ print $6" "$5}'`

I know about backticks, but that will not work in this case. The output of the command returns on one line. Since I process the data with read line and there could be any number of filesystems that method is not useful. I needed a way to execute the command into the pipe. The function should work nicely, I will try in the morning. The other alternative would be to hold the data in a file and read each line in.

Thanks for your help! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

3. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

6. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

variable question

Hi i have two question 1. Yahoo! In above there are total three words 1.1. www 1.2. yahoo 1.3 com how can i take all three things in different variable, i dont understand that how to separate dot. it looks like that a=www b=yahoo c=com 2. i have variable xyz whois value... (7 Replies)
Discussion started by: learnbash
7 Replies

9. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

10. Shell Programming and Scripting

VAriable Question

hi there, In my shell script I'm using a variable $ICO to speicfy the type of file that I'm processing e.g. VFR = France. further in my shell I'm trying to programatically set the sql file thtat I want to run :- REPORTTXT=/tmp/$ICO3hr.dat however when I check the value (as below) :- ... (4 Replies)
Discussion started by: rjsha1
4 Replies
Login or Register to Ask a Question