简体   繁体   English

我正在编写一个 shell 脚本,需要在“if then”语句中组合“find”和“grep”

[英]Im writing a shell script and need to combine "find", and "grep" inside of a "if then" statement

I have been working on a Script that automatically changes my Macbooks wallpaper based on the currently connected SSID.我一直在开发一个脚本,它可以根据当前连接的 SSID 自动更改我的 Macbooks 墙纸。 and it will select a new wallpaper every 30min它会每 30 分钟选择一个新壁纸

Long story short it's working fine, but im trying to create the illusion of "randomness" by not allowing the script to repeat the same wallpapers too often.长话短说它工作正常,但我试图通过不允许脚本过于频繁地重复相同的壁纸来创造“随机性”的错觉。

The command to pull a wallaper currently looks like this;拉墙纸的命令目前看起来像这样;

find -E -L ~/WallAware/A -type f -regex ".*\.(jpg|gif|png|jpeg)" \( -atime +5 -o -mtime -1 \) | ~/WallAware/gshuf -n 1

Which I mistakenly thought would work.我错误地认为这会起作用。

So what I need to do is:所以我需要做的是:

  1. use find to get a random file from a folder with an access time of 5+ days (This will prevent wallpapers being repeated too often)使用 find 从访问时间为 5 天以上的文件夹中获取随机文件(这将防止壁纸过于频繁地重复)
  2. If there is no file with an access time of 5+ days, then get a file with a modified time of -1 (This will prioritize new wallpapers if all other have been seen in the last 5 days)如果没有访问时间超过 5 天的文件,则获取修改时间为 -1 的文件(如果在过去 5 天内看到所有其他壁纸,这将优先考虑新壁纸)
  3. If it cant do either of those then just pick a random file.如果它不能执行其中任何一个,则只需选择一个随机文件。
  4. put the path to the selected wallpaper in the variable "$B"将所选壁纸的路径放在变量“$B”中

I am very much still learning, I am documenting my progress over at Github for fun.我仍然在学习,我正在Github 上记录我的进步以供娱乐。

I would do the following:我会做以下事情:

#!/bin/sh

my_find() {
    find -type f '(' -false $(for ext in png jpg gif jpeg; do echo "-o -name *.$ext"; done) ')' "$@"
}

if my_find -atime +5 -print -quit | grep .; then
    B=$(my_find -atime +5 | shuf -n 1)
elif my_find -mtime -1 -print -quit | grep .; then
    B=$(my_find -mtime -1 | shuf -n 1)
else
    B=$(my_find | shuf -n 1)
fi

# then do your stuff with B

Feel free to replace shuf by gshuf if you are on Mac OS X. I am not sure find on Mac OS X will have all these options though.如果您使用的是 Mac OS X,请随意将shuf替换为gshuf 。不过,我不确定在 Mac OS X 上find所有这些选项。 -quit is optional. -quit是可选的。 Also you could put extensions in a variable ( $exts ).您也可以将扩展名放入变量 ( $exts ) 中。

Edit: from this SuperUser post :编辑:从这个超级用户帖子

The difference?区别? Spotlight doesn't index /tmp, but it does index ~. Spotlight 不索引 /tmp,但它索引 ~。 I'm pretty sure that what you're seeing here is spotlight reading the file to index it after you change the atime - which then sets the atime back to now.我很确定您在这里看到的是在更改 atime 后聚光灯读取文件以对其进行索引 - 然后将 atime 设置回现在。

I believe this could be your atime issue.我相信这可能是您的时间问题。 Otherwise atime should work just fine on Darwin.否则 atime 在达尔文上应该可以正常工作。 You could either add your path to exceptions, or use a path not indexed by Spotlight ( /wallpapers could work).您可以将路径添加到异常中,也可以使用 Spotlight 未编入索引的路径( /wallpapers可以工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM