linux - DD 脚本和操作数预期错误
问题描述
我正在尝试实现一行代码,以防止用户擦除主机闪存驱动器(实际上是 110 GB 以下的任何内容)
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
END='\033[0m'
FLASH='\e[5m'
dt=`date '+%m/%d/%Y_%H:%M:%S'`
echo -e "STILL BE CAREFUL!! SIZE CHECK IS STILL NON-FUNCTIONAL"
echo "Inspect the drive for multiple boot/storage/recovery partitions. Check out the README.odt for reference photos."
sudo gnome-disks
echo "Showing list of drives: "
sudo fdisk -l | grep -i "Disk /"
echo "What drive are you attempting to wipe? Do not include /dev/"
read drive
size= sudo fdisk -l | grep -i "Disk /dev/$drive" | awk -F" " {'print $3'}
printf %.0f $size
if (( $size <= 110 ))
then
echo -e "$size"
echo -e "${RED}${FLASH}Error: You are trying to wipe a disk that is less than 110 GB. There's a high chance this is the host flashdrive. If you are sure this is the correct drive use\n ${END}${RED}sudo dd if=/dev/urandom of=/dev/<drive here> bs=1M status=progress${NC}"
else
echo -e "${RED}Now wiping drive /dev/$drive!${END}"
sudo dd if=/dev/urandom of=/dev/$drive bs=1M status=progress
echo -e "${RED}${FLASH}Wiping has completed at $dt !!!${END}"
echo "Drive in question should only have one main partition. See README.odt for reference."
sudo gnome-disks
fi
echo "Please enter Ctrl + C to exit!"
sleep 10000
如果我尝试擦除 8.7 GB 的 sda,我希望它会抛出我创建的错误。相反,它说预期的操作数错误然后继续擦除测试闪存驱动器。
解决方案
您的变量size
始终为零(实际上几乎整个脚本都未设置)。我假设您的意思是在该分配中的等号之后立即有一个反引号,而在该行的末尾有另一个。但根本不要使用反引号。这是为什么的一个例子。它们很容易被遗漏或误读为单引号,并且很难嵌套等。
就目前而言,该行表示“将值设置size
为 null 并将其仅传递到”的环境中sudo
。
由于size
未设置,因此您将收到if
语句的“预期操作数”错误,并且printf
也输出“0”。
将该行更改为:
size=$(sudo fdisk -l | awk -F " " -v drive=$drive 'BEGIN {IGNORECASE = 1} $0 ~ "/Disk /dev/" drive {print $3}')
请注意,我还将单引号移到了 AWK 命令中的正确位置,并将grep
函数移到了 AWK 命令中。
以下是其他一些更正的行:
dt=$(date '+%m/%d/%Y_%H:%M:%S')
printf '%.0f\n' "$size"
您可以省略双括号内的美元符号:
if (( size <= 110 ))
推荐阅读
- google-sheets - 使用 ArrayForumla 对列求和
- android-studio - 如何折叠所有语句(包括 if else、forloop)?
- php - 如何将 cmd 权限授予 apache?
- php - 如何通过 Laravel 中的外键检索数据?
- java - 调用 `ProcessHandle` 的 `parent().get()` 时出现“没有值”
- swift - 是否可以从动画控件驱动非视觉属性?
- python-2.7 - 如果存在则替换以字符串开头的文件中的行,如果不存在则添加
- php - 找到比“gethostbyname”更快的功能?
- javascript - 所有 javascript 数组和对象方法的大 O
- android - 如何在 UiAutomator 测试中获取材质编辑文本