Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Piping in a script (http://www.programmingforums.org/showthread.php?t=3123)

trufla Apr 5th, 2005 9:38 AM

Piping in a script
 
Hello all!

I was writing a simple script to search text files for data using grep and I decided to have a go at writing a script that piped unix commands to grep too. I am having problems though.

I think the problem is in the storing of user input in a variable.

For example:- If I wanted to store

who|grep ^l

in a variable called $pipe

is the spacing of the command an issue when stored in variables or am I completely off the point?

This is my code:-

:


#!/bin/bash
#Filename: piping : Author: L.Pearce

function pipefunction
{
 $pipe
}

echo
echo
echo "Type command in full:"
echo "1. Type unix command you want to pipe to grep."
echo "2. Type the 'pipe' symbol and name of tool you are using."
echo "3. Type the metachars and criteria to search with here:"
        read pipe

#:::How can the command stored in that variable be run:::
#------------------------------------------------------------------------
#By calling the function "pipefunction"

            pipefunction
echo
echo
echo "The entries above match your search"
echo
echo


I get this error:-

:

./piping: line 8: who|grep: command not found

Cheers,


Trufla

trufla Apr 5th, 2005 11:19 AM

aah, when saving a string with spaces you have to save it in quotes:-

:

x="Have a nice day"
y=$y


But a command has spaces and isn't a string? Cannot save it in double quotes.

How can a user enter a commmand to be saved to a variable so that it will be run when a function is called.

My original little script worked, but there were only single words in stored in the variables:- the user would type 'grep Jones Myfile', and each word is stored in a separate variable and recalled in order of the command

:


#Filename: user_retrieval : Author: L.Pearce

function grepfunction
{

$gtool $parameter $filename

}

#:::How does a user type data to be stored in a variable?:::
#-------------------------------------------------------------
echo
echo
echo ":::Retrieval of Information using 'Grep':::"
echo
echo "enter the tool you are using here:"
      read gtool
echo "$gtool was the tool you typed"
echo
echo
echo
echo
#:::Similarly, a user can specify a parameter to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "enter the parameter you wish to search by:"
      read parameter
echo "$parameter was the parameter you typed"
echo
echo
echo
echo

#:::And, a user can specify a filename to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "Enter file name to be searched"
      read filename
          if [ -r "$filename" -a -f "$filename" ]
            then
              clear

#:::How can the data stored in that variable be used to search a file?:::
#------------------------------------------------------------------------
#By calling the function "grep function"

            grepfunction
            else
      echo "Could not search!"
          fi
echo
echo
echo "The entries above match your search"
echo
echo
echo "It is as easy as:-"
echo "1. (Type tool) grep"
echo "2. (Type parameter) a name"
echo "3. (Type the name of the file) Myfile"
echo
echo "All you have to do is type the command at the $ prompt!"


Any ideas?

trufla Apr 5th, 2005 11:29 AM

I'm alittle confused because when I echo what is stored in $pipe it is

who|grep ^l

when I called the function that holds $pipe I get:-

:

./piping: line 6: who|grep: command not found

?

Infinite Recursion Apr 5th, 2005 1:16 PM

execution:

#!/bin/sh
pipe=`who | grep myuser`
echo $pipe

saving command to execute later:

pipe="who | grep myuser"

trufla Apr 6th, 2005 3:19 AM

I still get problems.

When I do this:-
:

#!/bin/bash
#Filename: piping : Author: L.Pearce
echo
echo
echo "Type command in full:"
echo "1. Type unix command you want to pipe to grep."
echo "2. Type the 'pipe' symbol and name of tool you are using."
echo "3. Type the metachars and criteria to search with here:"
        read pipe
echo "$pipe is what you typed."


I get this:-
:

who|grep myuser is what you typed

This means the user entered info is stored in the variable $pipe?

when I do this:-
:

#Filename: piping : Author: L.Pearce

function pipefunction
{
 $pipe
}

echo
echo
echo "Type command in full:"
echo "1. Type unix command you want to pipe to grep."
echo "2. Type the 'pipe' symbol and name of tool you are using."
echo "3. Type the metachars and criteria to search with here:"
        read pipe
#echo "$pipe is what you typed."

          pipefunction
echo
echo


I get this:-
:

./piping: line 6: who|grep: command not found

It is like the first half of the data with no spaces is stored but the last crucial part isn't? However I know that it all is stored because of the first test.

What happens to the 'myuser' bit?

What am I doing wrong?

Infinite Recursion Apr 6th, 2005 9:08 AM

Try this:

:

#Filename: user_retrieval : Author: L.Pearce
 
function grepfunction
{
 
cat $filename | $gtool $parameter
 
}
 
#:::How does a user type data to be stored in a variable?:::
#-------------------------------------------------------------
echo
echo
echo ":::Retrieval of Information using 'Grep':::"
echo
echo "enter the tool you are using here:"
read gtool
echo "$gtool was the tool you typed"
echo
echo
echo
echo
#:::Similarly, a user can specify a parameter to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "enter the parameter you wish to search by:"
read parameter
echo "$parameter was the parameter you typed"
echo
echo
echo
echo
 
#:::And, a user can specify a filename to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "Enter file name to be searched"
read filename
if [ -r "$filename" -a -f "$filename" ]
then
clear
 
#:::How can the data stored in that variable be used to search a file?:::
#------------------------------------------------------------------------
#By calling the function "grep function"
 
grepfunction
else
echo "Could not search!"
[jpowers@adsl-065-005-212-144 test]$ more grepit.sh
#Filename: user_retrieval : Author: L.Pearce
 
function grepfunction
{
 
cat $filename | $gtool $parameter
 
}
 
#:::How does a user type data to be stored in a variable?:::
#-------------------------------------------------------------
echo
echo
echo ":::Retrieval of Information using 'Grep':::"
echo
echo "enter the tool you are using here:"
read gtool
echo "$gtool was the tool you typed"
echo
echo
echo
echo
#:::Similarly, a user can specify a parameter to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "enter the parameter you wish to search by:"
read parameter
echo "$parameter was the parameter you typed"
echo
echo
echo
echo
 
#:::And, a user can specify a filename to be stored in a variable:::
#-------------------------------------------------------------------------
echo
echo
echo "Enter file name to be searched"
read filename
if [ -r "$filename" -a -f "$filename" ]
then
clear
 
#:::How can the data stored in that variable be used to search a file?:::
#------------------------------------------------------------------------
#By calling the function "grep function"
 
grepfunction
else
echo "Could not search!"
fi
echo
echo
echo "The entries above match your search"
echo
echo
echo "It is as easy as:-"
echo "1. (Type tool) grep"
echo "2. (Type parameter) a name"
echo "3. (Type the name of the file) Myfile"
echo
echo "All you have to do is type the command at the $ prompt!"



Results...

[jpowers@thebeast test]$ ./grepit.sh

:::Retrieval of Information using 'Grep':::
enter the tool you are using here:
grep
grep was the tool you typed

enter the parameter you wish to search by:
jpowers
jpowers was the parameter you typed

Enter file name to be searched
dat.dat
jpowers
jpowers
jpowers

The entries above match your search


All times are GMT -5. The time now is 4:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC