![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
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
echoI get this error:- ./piping: line 8: who|grep: command not found Cheers, Trufla Last edited by trufla; Apr 5th, 2005 at 9:41 AM. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
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 ? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
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
echoI 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? |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|