[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please check my sudo bash script





On Sun, Aug 31, 2025 at 2:25 PM Tom Browder <tom.browder@gmail.com> wrote:
I just added three new SSD and want to prep them for use.

I plan to use a script to do that incrementally. Given the disks show
the following when running "lsblk -o ..." ('#  ' added):

# NAME     SIZE FSTYPE MOUNTPOINT
# sda      3.6T                   # <= new disk   sda
# sdb    465.8G
# |-sdb1   512M vfat   /boot/efi
# |-sdb2 464.3G ext4   /
# `-sdb3   976M swap   [SWAP]
# sdc    931.5G                   # <= new disk   sdc
# sdd    931.5G                   # <= new disk   sdd

Does this bash fragment look safe for using bash vars for the first disk:

SDX=sda
sudo parted /dev/${SDX} mklabel gpt
sudo parted -a opt /dev/${SDX} mkpart primary ext4=0% 100%
sudo mkfs.ext4 /dev/${SDX

You should probably provide the entire script, and not just pieces. Before you post it, run it through ShellCheck.

It is unusual to use sudo like that in a script. Typically you require the root user, and have a check like this at the beginning of your script:

    $ cat ~/ssd-prep.sh
    #!/usr/bin/env bash

    # Control our PATH
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
    export PATH

    if [[ "${UID}" -ne 0 ]]
    then
        echo "This command has to be run as the root user."
        exit 1
    fi

Then, you would invoke your script with `sudo ~/ssd-prep.sh sda`.

You should also check return values. Silent failures suck.

Jeff 

Reply to: