gpt4 book ai didi

每次保存文件时执行的 linux(和 OSX)shell 命令

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:19 33 4
gpt4 key购买 nike

我试图让 jsdoc 在我保存我的 javascript 文件时自动生成。我有一个工作脚本,它存储文件的最后更新时间(当前是硬编码的)并与该文件的当前时间戳进行比较。我在 while 循环中运行它,一直运行到按下 CTRL-C,然后插入 0.1 秒的 sleep 以停止消耗处理器。

这是工作脚本:

while :
do

if [ $(( lastTime )) -ne `stat -f %m -t %s javascript.js` ]
then
lastTime=`stat -f %m -t %s javascript.js`
# custom jsdoc generating script
jsdoc javascript.js
echo +++ Run: `date` +++
fi

# stops while loop from consuming a lot of resources
# and making my fan whirr like he wants the computer to take off
sleep .1

done

我知道有更好的方法 - 只是不是那种方法。任何帮助表示赞赏。

编辑:安装了 inotify-tools 的 linux 机器的更新应该可以工作

#!/bin/bash

# with inotify-tools installed...

# only watches first parameter for modification
while inotifywait -e modify $1; do
echo
echo +++ Building JSDocs +++
jsdoc $@
echo +++ Last run: `date` +++
done

但是,我希望它能在 Linux 和 OSX shell 上运行,所以我可以在两种环境中使用

最佳答案

有一个名为 INotify 的 linux 内核功能可以监视文件系统的任何更改。它作为许多系统 API 公开。

对于脚本,有一个名为 inotify-tools 的包这使脚本可以访问通知系统。

关于每次保存文件时执行的 linux(和 OSX)shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6962913/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com