0

How to store output command in a variable and use this variable again for next command after pipe?

lsblk | awk {'print $1'} | grep ^sd | parted $input mklable gpt mkpart primary "1 -1" -s

$input is output lsblk | awk {'print $1'} | grep ^sd command and use $input again in parted command.

1
dev=$(lsblk | awk '$1 ~ /^sd/ {print $1; exit}')
if [ -n "$dev" ]; then
  dev=/dev/$dev
  parted "$dev" mklabel gpt mkpart primary "1 -1" -s
fi

Now, if you want the first sd? disk, it may be easier to do:

shopt -s nullglob; set -- /dev/sd[a-z]
if (($# > 0 )); then
  parted "$1" mklabel gpt mkpart primary "1 -1" -s
fi

Or to do it for every disk:

shopt -s nullglob
for dev in /dev/sd[a-z]; do
  (set -x; parted "$dev" mklabel gpt mkpart primary "1 -1" -s)
done
  • How to use this command in a terminal line? – pyramid13 Jul 26 '18 at 10:45
0

Just save to a variable and process it.

input=`lsblk | awk {'print $1'} | grep ^sd` ; parted $input mklable gpt mkpart primary "1 -1" -s

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.