Shilpi Agrawal
Shilpi Agrawal

Reputation: 615

Store the output of shell command in a variable

I am trying to install zfs through shell script while installation I am getting some error so, to automate it fully, I want to get the version to be installed from error itself. For all other commands I am getting the error in one variable but for one command its not coming at all. I tried every solution possible.

I need output of this command

sploutput=$(sudo dkms install -m spl -v $version)

echo $sploutput

echo $sploutput # This is giving nothing.

I tried wrapping it around string also like "sploutput=$(sudo dkms install -m spl -v $version)"

echo "{sploutput}"

Nothing seems to work.

Upvotes: 0

Views: 58

Answers (2)

Nishu Tayal
Nishu Tayal

Reputation: 20820

As per ZMO's answer: try running sudo dkms install -m spl -v $version

See, what is it returning? : STDERR or STDOUT.

In case, if it fails, it won't show up anything in sploutput. It write only STDOUT to the variable.

Use 2>&1(to write Standard error to standard output). You can refer IO redirection

You can use following:

sploutput=$(sudo dkms install -m spl -v $version 2>&1)
echo $sploutput

Upvotes: 1

zmo
zmo

Reputation: 24812

sploutput=$(sudo dkms install -m spl -v $version)
echo $sploutput

it might be because the dkms install outputs on STDERR and not on STDOUT, and with the command you're using, you're only getting STDOUT output in the variable. To take both, you can try:

sploutput=$(sudo dkms install -m spl -v $version 2>&1)

to redirect STDERR into STDOUT.

Upvotes: 1

Related Questions