3

Trying to break a for loop if the variable $DEVP doesn't exist.

nme=(Y6T1 Y6-T1 Y6.T1 Yr6T1 Yr6-T1 Yr6.T1 Yr6Term1)
DEVP=(/dev/disk2 /dev/disk3 /dev/disk4 /dev/disk5 /dev/disk6 /dev/disk7 /dev/disk8)

for ((i = 0; i < 7; i++)) ; do 
    if [ ${nme[i]} ${DEVP[i]} = 0 ] ; then
        diskutil eraseDisk FAT32 ${nme[i]} ${DEVP[i]}
    else
        echo “Formatted USBs” ; break
    fi
done
Phil
  • 119
  • 1
  • 1
  • 8

3 Answers3

2

The break works fine

for ((i = 0; i < 7; i++)) ; do
    if [ $i -lt 3 ] ; then
       echo $i
    else            
        echo “Formatted USBs” ; break
    fi
done

Output is:

0
1
2
“Formatted USBs”

, however I'm not sure regarding your if statement, you should probably use -z.

 if [ -z ${DEVP[i]} ] ; then

Have a look here

Community
  • 1
  • 1
Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18
  • I tried -z but then realised that it should be -d instead. I have just run the if loop but it's returns false when I search for the directory. It doesn't seem to recognise directories that exist? – Phil Oct 29 '15 at 10:04
  • Are you sure it's a directory? You need -b for the block device – Roman Pustylnikov Oct 29 '15 at 10:16
  • I just ran ls and noticed it was a Block device. I didn't know you could use -b. Thank you, Roman. Legend! – Phil Oct 29 '15 at 10:33
1

Your comparison looks like you expect the two strings to contain the single number zero. They never will. If they are empty, they will contain the empty string. (But even then having two string arguments to the left of the comparison operator is a syntax error.)

Anyway, if the actual problem you are trying to solve is that you don't know how many elements these arrays contain, just ask.

for((i=0; i<${#DEVP[@]}; ++i)); do

Of course, if the arrays contain a different number of arguments, you could still end up in a situation where ${nme[i]} is undefined.

    ${nme[i]+:} break

This is rather obscure -- it will expand to the empty string before break (and thus break out of the loop) if the value is unset; if it's set, this is just a : noop.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

First repair the syntax: Put things in quotes in the if statement.

EDIT: I thought you needed to refer to the var i with $i, but @tripleee showed me, that this is optional in bash arrays. I removed the extra $ characters.

nme=(Y6T1 Y6-T1 Y6.T1 Yr6T1 Yr6-T1 Yr6.T1 Yr6Term1)
DEVP=(/dev/disk2 /dev/disk3 /dev/disk4 /dev/disk5 /dev/disk6 /dev/disk7 /dev/disk8)
for ((i = 0; i < 7; i++)) ; do
        if [ "${nme[i]} ${DEVP[i]}" = 0 ] ; then
                echo "diskutil eraseDisk FAT32 ${nme[i]} ${DEVP[i]}"
        else
                echo “Formatted USBs” ; break
        fi
done

If you want to check the vars being empty, introduce an error by looping until 8.
I have put an echo in front of the diskutil line, so you can test without doing something you do not want.

for ((i = 0; i < 8; i++)) ; do
        if [ -z "${nme[i]}" ]; then
           echo "loop $i: \${nme[i]} is empty";
           break;
        fi
        if [ -z "${DEVP[i]}" ]; then
           echo "loop $i: \${DEVP[i]} is empty";
           break;
        fi
        echo "diskutil eraseDisk FAT32 ${nme[i]} ${DEVP[i]}"
done
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • The `$` is optional in array subscripts in Bash. – tripleee Oct 29 '15 at 09:18
  • @tripleee Thanks, I usually try to avoid arrays in bash. Without the `$` the code looks cleaner! – Walter A Oct 29 '15 at 10:15
  • There is still no way for `"${nme[i]} ${DEVP[i]}"` to ever be literally the string zero, so that part of the logic would need another round of thinking. My guess is that the intent was to take the `if` branch if either (or both?) are unset but this is really just pure unadultered guesswork. The question simply isn't very clear. – tripleee Oct 29 '15 at 10:17
  • Sorry it should have been `if [ -b ${DEVP[i]} ] ; then`. ${nme[i]} shouldn't of been in there. @Roman got it. – Phil Oct 29 '15 at 11:32