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

Re: Please check my sudo bash script



On Sun, 31 Aug 2025 at 14:56, 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}

Hi,

If you are interested in optimisation, I might mention that there's no need
to call parted multiple times.

Although it's 'man' page and 'info' page don't give any clear examples of
usage, the 'parted' command itself is capable of executing a script of
commands that is provided on its invocation line.

Here's a safe, tested, minimal executable shell script example of this:

-----
#!/bin/bash
# display the partition table using sectors, bytes, and human units
mydevice=/dev/sda
myscript="unit s print free unit B print free unit compact print"
parted "$mydevice" "$myscript"
-----

If you want to add clarity by using line breaks between the parted script
commands, the $myscript variable needs to be used unquoted as shown below:

-----
#!/bin/bash
# display the partition table using sectors, bytes, and human units
mydevice=/dev/sda
myscript="
unit s
print free
unit B
print free
unit compact
print
"
parted "$mydevice" $myscript
-----

Also, if you want to use parted commands that modify the drive, then
you will likely need to disable parted's interactive prompting, to do
that you will need to utilise its '-s' option as well.

WARNINGS:

When automating parted like this, there is a danger of destroying your
system, please take care to ensure that you never accidentally modify the
wrong drive.

The above scripts are minimal examples to demonstrate specific points,
please add safety features to ensure that your script only does exactly
what you want it to do.

These are not complete solutions, for brevity I have not repeated good
advice that others have given you.

Take care and have fun with it :)


Reply to: