Using pipes within variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using pipes within variables
# 1  
Old 07-21-2010
Using pipes within variables

Hi,

I'm trying avoid having a large number of 'if' statements in my script by setting up a variable within 'ksh' which can be expanded depending on parameters passed to the script. The output from the 'cat' command should be able pipe its output into any additional commands on the command line depending on the contents of the variable.

ie The code below should be able to list the complete file, only the executable lines, only the comment lines or be able to count the number of each type of line if required

Code:
extra_cmd=""

set -- $( getopt :ecl "$*" ) 

while [[ $1 != "--" ]] ; do
    case arg in
        "-e")  # Remove comments
                extra_cmd=" | grep -v \"^#\" " 
                ;;

        "-c") # Remove non comment lines
                extra_cmd=" | grep \"^#\" "
                ;;

         "-l")  # count lines
                extra_cmd="${extra_cmd} | wc -l "
                ;;

           *)  # Default
                extra_cmd=""           
                ;;
    esac
done
...
...
cat file ${extra_cmd}


However when this runs it displays the file on the screen rather than piping the output to 'grep' or 'wc' depending of the parameter passed to the script and then compains about the contents of the variable, which it takes as the name of another file to list

Quote:
cat: | grep -v "^#" : No such file or directory
I have tried a variety of different ways of quoting the contents or escaping the '|' when setting up the variable but none are having the desired effect of piping the output from 'cat' into 'grep' or 'wc'.

Any help would be greatly appreciated

Stv T
# 2  
Old 07-21-2010
Try:

Code:
eval cat file ${extra_cmd}

or

Code:
echo cat file1 $extra_cmd | sh

This User Gave Thanks to Scott For This Post:
# 3  
Old 07-21-2010
Try :
Code:
eval cat file ${extra_cmd}

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 4  
Old 07-21-2010
Quote:
Originally Posted by stv_t
Code:
case arg in

Code:
case $arg in

# 5  
Old 07-22-2010
MySQL

Thanks for the replies - 'eval' gives me exactly what I was looking for

Stv T
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Other way aside from putting more PIPES (|)

I already manage to get the output that i want.. but wat if removing all the pipes and convert it 1 liner with less pipes. My command below can get the ouput that i want. i just want to remove the pipes or less pipes. #cat file1 us-west-2a running i-3397a421... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

2. Shell Programming and Scripting

Pipes with the < and >> meanings.

Hey, Well, we're starting scripting next week for my class, and I have my command done, but I don't actually understand what the meaning is, so I was just wondering if someone could "translate" this in to words so that I might be able to do this better... command1 | command2 | command3... (5 Replies)
Discussion started by: sso
5 Replies

3. Programming

Question on pipes in C

If a code forks 2 childs, what can the values be for the process id's of each of the child? I child pid is supposed to be 0, but what if you fork 2 of them? (5 Replies)
Discussion started by: omega666
5 Replies

4. Programming

Problem with pipes

problem solved. (1 Reply)
Discussion started by: superfons
1 Replies

5. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

6. UNIX for Dummies Questions & Answers

pipes and shells

Hi How do I direct the output from ls to for example grep a in C. I am not asking for the whole shell implementation. If I write ls|grep myfile in the shell. How is the output sent from ls to the input grep. whit int pipe(pipe); We create the pipe. I and I guess we use dup2(old... (4 Replies)
Discussion started by: isato
4 Replies

7. Shell Programming and Scripting

named pipes

How to have a conversation between 2 processes using named pipes? (5 Replies)
Discussion started by: kanchan_agr
5 Replies

8. UNIX for Dummies Questions & Answers

Two types of pipes?

What is the difference between: cd /tmp tar -cf - *.txt |gzip > tmp_txt.tar.gz and cd /tmp mknod pipe p gzip < pipe > /tmp/tmp_txt1.tar.gz & tar -cf pipe *.txt Apart from the fact that we have to create the pipe file manually, is there any difference in the performance of the two?... (5 Replies)
Discussion started by: blowtorch
5 Replies

9. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question