Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2005, 9:38 AM   #1
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
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

Last edited by trufla; Apr 5th, 2005 at 9:41 AM.
trufla is offline   Reply With Quote
Old Apr 5th, 2005, 11:19 AM   #2
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
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 is offline   Reply With Quote
Old Apr 5th, 2005, 11:29 AM   #3
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
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

?
trufla is offline   Reply With Quote
Old Apr 5th, 2005, 1:16 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
execution:

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

saving command to execute later:

pipe="who | grep myuser"
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 6th, 2005, 3:19 AM   #5
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
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?
trufla is offline   Reply With Quote
Old Apr 6th, 2005, 9:08 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."

Last edited by Infinite Recursion; Apr 6th, 2005 at 9:17 AM.
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:37 AM.

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