[英]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:所以我需要做的是:
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.