Home > Software engineering >  Mounting devices to mountpoints stored in variables
Mounting devices to mountpoints stored in variables

Time:10-31

Im doing this script and I cant see to find a way to accordingly mount the devices stored in the "devs" variable into the mount points listed in "mntpnt".

Hope Im being clear explaining myself, heres the script so far:

#!/bin/bash

count=$(find /dev/sd{b..z}1 2>/dev/null | wc -l)
dirnames=$(printf "win%d " $(seq $count))

for i in $dirnames
do
        sudo mkdir -p /mnt/$i
done

devs=$(find /dev/sd{b..z}1 2>/dev/null)
mntpnt=$(find /mnt/win*)

CodePudding user response:

One approach without find.

#!/usr/bin/env bash

shopt -s nullglob

disks=(/dev/sd[b-z]1)
count=${#disks[*]}

printf '%s\n' "${disks[@]}"

for ((i=0;i<count;i  )); do
  printf 'mkdir -p %s && mount %s %s\n'  "/mnt/win$i" "${disks[$i]}" "/mnt/win$i"
done

Output using my /dev/sd

/dev/sdb1
/dev/sdc1
/dev/sdd1
mkdir -p /mnt/win0 && mount /dev/sdb1 /mnt/win0
mkdir -p /mnt/win1 && mount /dev/sdc1 /mnt/win1
mkdir -p /mnt/win2 && mount /dev/sdd1 /mnt/win2

Im doing this script and I cant see to find a way to accordingly mount the devices stored in the "devs" variable into the mount points listed in "mntpnt".


Use an array to store data.

mapfile -t devs < <(find /dev/sd{b..z}1 2>/dev/null)
mapfile -t mntpnt < <(find /mnt/win*)

for i in "${!devs[@]}"; do
  printf '%s %s\n' "${devs[$i]}" "${mntpnt[$i]}"
done

As mentioned in the comments by @innocent Bystander use

find /dev -name 'sd[b-z]1'

and

find /mnt -name 'win*'

Although I suggest to use:

find /mnt -name 'win[0-9]*'

  • I prefer to use globs like in my sample code, rather that find.
  • Related