使用 FOR 循环,当条件为真时结束

发布于 2024-10-20 14:34:08 字数 494 浏览 0 评论 0原文

这是我的脚本:

 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

目前,该脚本在用户桌面中查找唯一的文件,然后如果该文件存在则执行另一个脚本,否则它会杀死 MyProgram,这对于每个用户都是如此。

我想做的是,只有当该文件不存在于任何用户的桌面中时,脚本才会执行 else 中的命令,否则一旦发现该文件,它就会停止循环脚本并运行该脚本。

感谢您的帮助 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-10-27 14:34:08

使用另一个变量:

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

编辑:实际上这很愚蠢。如果您要在第一个脚本运行后退出,则根本不需要该变量:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文