LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I grep variable? (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-grep-variable-4175538067/)

nine4409 03-27-2015 10:12 PM

How can I grep variable?
 
I want to And search grep shell

but It's hard to grep variable


---------------------------------------------------------------
#!/bin/bash


if [ $# -eq 0 ]
then
echo "Ussage: phone searchfor [...searchfor]"
echo "(You didn't tell me what you want to search for )"

else

pass=0
find=""

for idx in $*
do
if [ -n "$idx" ]
then
if [ $pass -eq 0 ]
then
find=$(egrep "$idx" mydata)
pass=1

else

find=$("$find" | grep "$idx")

echo $find
fi
fi

done

if [ -z "$find" ]
then
echo "There is no such thing"
else

echo $find | awk -f display.awk

fi

fi

-----------------------------------------------------

there is one error : command not found

in find=$("$find" | grep "$idx")

how can I grep variable and store it into variable?

grail 03-28-2015 02:57 AM

Unless $find is a command I am not how you think the pipe will work? Maybe try calling a command on the variable, say echo, then there is some standard input for grep and the pipe to work with.

Also, what would be the point? Is it likely that $find will have multiple lines in it? If not, why use grep?

sharky 03-30-2015 01:11 AM

Not sure what you want but maybe you could just run env and then grep.

Code:

env | grep <whatever>
or if you want to list just the environment variables without the values;

Code:

env | awk -F"=" '{print $1}' | grep <whatever>


All times are GMT -5. The time now is 03:44 AM.