2

I am currently attempting to create a bash script that will check inside of each users /Library/Mail folder to see if a folder named V2 exists. The script should create an array with each item in the array being a user and then iterate through each of these users checking their home folder for the above captioned contents. This is what I have so far:

#!/bin/bash

cd /Users

array=($(ls))

for i in ${array[@]}
do

if [ -d /$i/Library/Mail/V2 ]

then
    echo "$i mail has been upgraded."
else 
    echo "$i FAIL"

fi

done
1
  • In the future, describe why and how what you're doing doesn't work, or otherwise why you're asking about it here. Commented May 16, 2012 at 20:30

1 Answer 1

5

Populating your array from the output of ls is going to make for serious problems when you have a username with spaces. Use a glob expression instead. Also, using [ -d $i/... ] will similarly break on names with spaces -- either use [[ -d $i/... ]] (the [[ ]] construct has its own syntax rules and doesn't require quoting) or [ -d "$i/..." ] (with the quotes).

Similarly, you need to double-quote "${array[@]}" to avoid string-splitting from splitting names with spaces in two, as follows:

cd /Users
array=(*)
for i in "${array[@]}"; do
  if [[ -d $i/Library/Mail/V2 ]]; then
    echo "$i mail has been upgraded."
  else 
    echo "$i FAIL"
  fi
done

That said, you don't really need an array here at all:

for i in *; do
  ...check for $i/Library/Mail/V2...
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.