I have a command I'm using to get the hostname.localdomain:
dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'
This nicely returns a result like:
michael.lawler.localdomain.com
I'd like to further use that result as a variable in a Bash script.
It seems I'm having trouble getting past the first pipe.
If I VAR="dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'"
...I get back the entire zone transfer. I've also tried many minor changes, adding $
before the dig
command, without quotes, but nothing seems to work. How can I fix this?
BASH command output to the variable. Different types of bash commands need to be run from the terminal based on the user’s requirements. When the user runs any command from the terminal then it shows the output if no error exists otherwise it shows the error message.
The following script stores the output of `pwd` command into the variable, $current_dir and the value of this variable is printed by using `echo` command. The option and argument are mandatory for some bash commands.
Quoting in bash scripting is a process to make variables or any other container for data to be expanded to the literal value inside any string, quoting is also used for other operations as well. There are several types of quoting techniques in a bash script.
To see the active environment variables in your Bash session, use this command: env | less. If you scroll through the list, you might find some that would be useful to reference in your scripts. How to Export Variables. When a script runs, it’s in its own process, and the variables it uses cannot be seen outside of that process.
VAR=$( dig axfr @dc1.localdomain.com localdomain.com |
grep -i Lawler |
awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }' )
Use backtics instead of quotes:
VAR=`dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'`
Backtics actually mean "run whatever is in here and return standard out as the expression's value", but quotes don't do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With