3

I want to store the output from an netcat function into a variable. I tried a lot of different ways, but it doesn't work to me. Can someone help me? The whole scripting thing is whole new for me!

#! /bin/sh

while true;do
        var = "$(echo "RDTEMP1" | netcat -q2 sanderpi 5033)"
        echo &(var)
        echo "$(date +%Y-%m-%d%t%H:%M:%S)"
done

2 Answers 2

6

In shell, setting variables would be done with:

var1=toto
var2="$(echo toto | othercommand)"

You can't have spaces between your variable name, the equal character and the value you're assigning your variable with.

Then, to echo a variable, you would do:

echo $var
echo "$var"
echo "${var}"

The & character, in bash/sh, is used for "job control", which is yet another topic, ...

Start by using the following instead, tell us how it goes:

#! /bin/sh

while true;do
    var="$(echo "RDTEMP1" | netcat -q2 sanderpi 5033)"
    echo "$var"
    echo "$(date +%Y-%m-%d%t%H:%M:%S)"
done
2

Variable assignments in shells require no spaces around the assigner (i.e. =).

Correct:

var=$(frobnify xyz)

Wrong:

var = $(frobnify xyz)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .