b1onic
b1onic

Reputation: 239

Use a FOR loop which ends when the condition is true

here is my script :

 dir="/path/to/a/folder"


 for d in /Users/*
 do
 if [ -e $d/Desktop/*file ]
then

 $dir/./script.sh

 exit 

 else 

 /usr/bin/osascript <<-EOF

tell application "System Events"
    activate
    display dialog "Some text"
end tell

EOF
killall MyProgram
fi
done

Currently, the script looks an unique file in an user's desktop, then executes another script if the file exists, otherwise it kills MyProgram, and this for each user.

What I would like to do is that the script executes the commands in the else only when the file does not exist in ANY users' desktop, otherwise it stops the loop as soon as the file is found by the script and the script is run.

Thanks for your help !

Upvotes: 0

Views: 85

Answers (1)

Mat
Mat

Reputation: 206689

Use another variable:

script_found="false"

for d in /Users/*
do
  if [ -e $d/Desktop/*file ]
  then
    script_found="true"
    $dir/./script.sh
    exit
  fi
done

if [ $script_found eq "false" ]
then
  # do other stuff
fi

Edit: actually that's stupid. You don't need the variable at all if you're going to exit after the first script is run:

for d in /Users/*
do
  if [ -e $d/Desktop/*file ]
  then
    $dir/./script.sh
    exit # this terminates the whole script
  fi
done

# do other stuff

Upvotes: 1

Related Questions