我不明白为什么这些行不起作用:
prim=$(lsblk -o MOUNTPOINT -nr /dev/$hdd);
echo $prim;
if [[ "$prim" == "/" ]]
then echo "ok";
fi
第一个echo
给出的/
是输出,所以它应该输入if
,但它不起作用。我遗漏了什么吗?
$hdd
是从 获得的read -p "blabla" hdd;
。
我已尝试修剪第一行结果,但因为它是一个目录,所以我无能为力。
也许查看整个脚本更容易,但问题似乎出在已经发布的部分。
#!/bin/bash
clear
lsblk --scsi
read -p "wich hdd you want to operate with? " hdd
echo "starting operations. . . (wait)"
#controllo esistenza disco
if [ ! $(lsblk /dev/$hdd) ] 2>/dev/null
then echo "select an existing hdd"
else
#estrapolo punto mount
prim=$(lsblk -o MOUNTPOINT -nr /dev/$hdd)
echo "$prim"
#controllo scelta se è disco primario
if [ "$prim" == "/" ]
then echo "error!! $hdd is the primary disk, can't operate on that disk"
else
#mostro opzioni
echo ""
echo "options : "
echo ""
echo "1)format"
echo "2)info "
echo "3)show content"
echo ""
read -p "choose option : " opt
if [ $opt = 1 ]
then echo "1"
elif [ $opt = 2 ]
#mostro info disco selezionato
then echo "2"
elif [ $opt = 3 ]
then echo "3"
else echo "insert a correct option"
fi
fi
fi
答案1
问题取决于分配给 的输入$hdd
。如果您输入类似sda1
(带分区号)的内容,脚本将正常工作。但似乎您希望输入类似sda
(不带分区号)的输入。在后一种情况下,$prim
包含每个分区的名称,以新行分隔,因此它永远不会与 完全匹配/
。您也不能使用=~
来搜索子字符串,因为除 [swap] 之外的每个分区都将包含/
。
您可以尝试拆分$prim
并检查每个部分:
quit=0
for part in $prim ; do
if [[ "$part" == "/" ]] ; then
quit=1
fi
done
if [[ $quit == '1' ]] ; then
echo "error!! $hdd is the primary disk, can't operate on that disk"
else
# ...
fi