0

when I want to get the size of sdb size on remote machine - master

I get that:

#  ssh master "lsblk | grep \"^sdb\" | awk '{print \$4}' "
100G

but

when we want to set the size value in variable and then to print it then we get that:

# VAR=` ssh master "lsblk | grep \"^sdb\" | awk '{print \$4}' " `
# echo $VAR
sdb 8:16 0 100G 0 disk

why the last test not give only the 100G size ?

what is the diff here

1

Don't run stuff remotely that you could run locally.

var=$( ssh master lsblk | awk '/^sdb/ { print $4 }' )

For when the value sdb is in a variable (from comments below):

var=$( ssh master lsblk | awk -v disk="$variable" '$0 ~ disk { print $4 }' )

or, to force a match at the start of the line,

var=$( ssh master lsblk | awk -v disk="$variable" '$0 ~ "^" disk { print $4 }' )
  • what in case we want to set sdb in variable ? – yael Dec 11 '18 at 12:15
  • we try that , but not works var=$( ssh master02 lsblk | awk -v Disk=sdb '/^Disk/ { print $4 }' ) – yael Dec 11 '18 at 12:15
  • 1
    @yael See update – Kusalananda Dec 11 '18 at 12:24
0

need to add another back slash as:

VAR=` ssh master "lsblk | grep \"^sdb\" | awk '{print \\$4}' " `
echo $VAR
100G
  • You can also use $(...) instead of the old-style backquote form for command substitution. It changes the meaning of "\" before "$", "`", or "\". – fra-san Dec 11 '18 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.