I am trying to implement a line of code that prevents the user from wiping the host flashdrive (anything under 110 GB actually)
#!/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
If I attempt to wipe sda which is 8.7 GB I expect it to throw the error i created. Instead it says expected operand error then continues to wipe the test flashdrive anyway.
Your variable size
is always equal to zero (actually it's unset for almost the whole script). I assume you meant to have a backtick immediately after the equal sign in that assignment and another one at the end of the line. But don't use backticks at all. This is an example of why. They're easily missed or misread as single quotes and they're very difficult to nest, etc.
The line as it stands says to "set the value of size
to null and pass that only into the environment of sudo
".
Since size
is otherwise unset, you're getting an "operand expected" error for the if
statement and the printf
is outputting "0" also.
Change that line to:
size=$(sudo fdisk -l | awk -F " " -v drive=$drive 'BEGIN {IGNORECASE = 1} $0 ~ "/Disk /dev/" drive {print $3}')
Note that I also moved the single quotes to their proper position in the AWK command and moved the grep
function into the AWK command.
Here are a couple of other corrected lines:
dt=$(date '+%m/%d/%Y_%H:%M:%S')
printf '%.0f\n' "$size"
You can omit the dollar sign inside double parentheses:
if (( size <= 110 ))