Hi folks,
im writing a bash script that hdparms some block devices. I want to use a variable like CMD="lsblk /dev/sda | grep -v sda" to only get the partitions of the devices. But bash always ignores the pipe symbol and so lsblk complains about unkown option -v. Any idea how I can go about this?
Code:
#!/bin/bash
#set -x
# Get list of device
CMD="lsblk -e "11,1" -n -l -i -d -o NAME"
read -n 1 -p "Gather all block device data (y/n): " line
echo ""
if [ "$xline" == "xy" ]; then
echo > ./LAST
else
x=0
for i in $($CMD); do
x=$((x+1))
echo "$x) $i"
done
read -n 1 -p "Gather data of partition from ($(seq -s '/' 1 $x)): " line
echo ""
DEV=$($CMD | awk "NR==$line { print $1 }")
#CMD="lsblk -n -l -i -o NAME /dev/$DEV \| grep -v $DEV$"
CMD='lsblk -n -l -i -o NAME /dev/'
CMD+="$DEV "
CMD+='| grep -v "'
CMD+="$DEV"
CMD+='$"'
fi
set -x
echo $CMD
for i in $($CMD); do
echo some
echo $i
done
set +x
exit 0
What I can see from the debug output is that | is escaped with '' when the command should be run. So how can I convince the shell to interpret the pipe symbol. I also tried to escape it with \ but to no avail.
Code:
Gather all block device data (y/n): n
1) sda
2) sdb
3) sdc
Gather data of partition from (1/2/3): 1
+ echo lsblk -n -l -i -o NAME /dev/sda '|' grep -v '"sda$"'
lsblk -n -l -i -o NAME /dev/sda | grep -v "sda$"
++ lsblk -n -l -i -o NAME /dev/sda '|' grep -v '"sda$"'
lsblk: invalid option -- 'v'
Best Regards