2015-09-25 21 views
2

首先,我已經閱讀了很多文章(請參閱底部)if-clause在文件中搜索特定的字符串並遵循這些職位1to1,但是我不要設法讓我的腳本工作。我想在/etc/fstab一個條目,如果不存在的:在您的幫助提前Bash:添加磁盤到fstab的腳本(如果不存在)

#!/bin/bash 
fstab=/etc/fstab 

if grep -q "poky-disc" "$fstab" 
then 
    echo "#poky-disc" >> /etc/fstab 
    echo "/dev/sdb1 /media/poky ext4 defaults 0 2" >> /etc/fstab 
else 
    echo "Entry in fstab exists." 
fi 

感謝。這些都是類似的帖子,這沒有幫助我進一步:

+0

它看起來像你需要扭轉'then..else'內容 – arco444

+0

我試了一下反演也是如此。 'grep -q「poky-disc」「$ fstab」'和grep「poky-disc」「$ fstab」' – h0ch5tr4355

+0

究竟是什麼問題? – arco444

回答

1
#!/bin/bash 
fstab=/etc/fstab 

if [[ $(grep -q "poky-disc" "$fstab") ]] 
# forgiving me for being a bit of over-rigorous, you might want to change this matching word, as below, 'poky-disc' merely a comment, not exactly a config line, so 
then 
    echo "#poky-disc" >> /etc/fstab 
    echo "/dev/sdb1 /media/poky ext4 defaults 0 2" >> /etc/fstab 
else 
    echo "Entry in fstab exists." 
fi