There are a couple of problems here.
The first is that you're trying to execute as a command the contents of the variable $ouptut1, which is just the dig output; it's not a command. You probably meant echo $output1.
Then you are trying to execute the contents of variable $grepg as a pipeline. But parsing a line into the components of a compound command (a pipeline is a compound command) happens before parameter expansion. So if you execute something like this:
foo="echo hello | grep hi"
$foo
This first gets broken apart into a single simple command, then the command is expanded, then it is parsed into arguments. So this is equivalent to running:
echo hello "|" grep hi
In general, it's not a good idea to store strings of text that you want to have evaluated in variables. There are a few very special cases in which you may want to do it, but in almost every case (and probably your case) there's a better way to do it. I'm going to warn you against it, but if you really want to evaluate a string as a command, you can use eval, for example:
go=$(echo $output1 | eval $grepg)
What you seem to want is to define a shell function. If you want to reuse that pipeline several times, running several different values through it, just define a shell function. That function is a single command that acts as the equivalent of the contained commands:
function grepg() {
grep PTR | cut -d ' ' -f 2 | grep google | cut -f 5
}
go=$(echo $output1 | grepg)
To simplify your code further, you can avoid all of the intermediate variables. If you're just going to pipe the results of the dig command into grepg, you don't need to save it in a variable before doing so. And if you're just going to echo the results to stdout, again you don't need to store it in a variable, you can just let the output go to stdout:
#!/bin/bash
IP=$1
function grepg() {
grep PTR | cut -d ' ' -f 2 | grep google | cut -f 5
}
dig -x $IP | grepg
I'm assuming here that you wanted to factor out the function so you could use it several times in your script; if not, if you're just going to use it once, you could simplify further to:
#!/bin/bash
IP=$1
dig -x $IP | grep PTR | cut -d ' ' -f 2 | grep google | cut -f 5
Note that if you want to store a pipeline like this in a variable so that you could use some other code to decide between two functions, and apply one or the other depending on some condition, I'd recommend defining the function like above, and then just storing the single function name in the variable. That will work the way you expect:
function grepg() {
grep PTR | cut -d ' ' -f 2 | grep google | cut -f 5
}
function grepf() {
# do something else, I'm not creative enough to come up with a good example
}
if [ "$2" = "foo" ]
then
func=grepg
else
func=grepf
fi
dig -x $IP | $func
cutmight break easier than a regex, it might not - you might have well-defined fields but no idea what was in them. And both shell scripting and Python would let you use both anyway, in multiple different flavours. Like I say, it's all kind of subjective, and that was kind of where this discussion started. I think we both started taking it too seriously somewhere along the line, for which I apologise.