LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script variable NULL instead of value of expression (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-variable-null-instead-of-value-of-expression-844756/)

bdrockwell 11-17-2010 12:27 AM

Bash script variable NULL instead of value of expression
 
Quote:

#!/bin/sh

for i in {1..10}
do
for j in {1..50}
do
echo `host 129.74.$i.$j` | awk '{print $5}'

set domain = `host 129.74.$i.$j` | awk '{print $5}'

echo $domain
done
done
The first echo generates something like: abc.de.fgh
The second echo generates:

Any help would be highly appreciated.

Kenny_Strawn 11-17-2010 12:34 AM

You just define the variable without set, like so:

Code:

#!/bin/sh

for i in {1..10}
do
for j in {1..50}
do
echo `host 129.74.$i.$j` | awk '{print $5}'

domain = `host 129.74.$i.$j` | awk '{print $5}'

echo $domain
done
done


druuna 11-17-2010 01:41 AM

Hi,

@bdrockwell: If you want to fill a variable with the output of a command:

domain="`host 129.74.$i.$j | awk '{print $5}'`"

Or

domain=$(host 129.74.$i.$j | awk '{print $5}')

@Kenny_Strawn: Removing the set part is not enough to fix the problem.......

Hope this helps

colucix 11-17-2010 01:42 AM

In assigments you cannot have spaces around the equal sign:
Code:

domain=`host 129.74.$i.$j | awk '{print $5}'`
moreover the command substitution operator (backticks) must include the entire command line (included the pipe and the awk command).


All times are GMT -5. The time now is 01:12 PM.