限制函数重新执行的方法

huangapple go评论57阅读模式
英文:

How to limit re-execution of function

问题

Here's the translated content of the script you provided:

我已经设置了一个键盘快捷方式来调用一个bash脚本,但事实证明,如果我频繁使用这个快捷方式,从脚本内启动的程序会崩溃。

以下是脚本的精简版本:

#!/bin/bash

# 配置
monitor="eDP-1"
wallpaper_dir="$HOME/Pictures/bg"

# 获取壁纸文件路径列表
file_listing=($wallpaper_dir/*)

function hyprpaper_set() {

    # 获取一个随机文件路径
    file_random="${file_listing[RANDOM % ${#file_listing[@]}]}"

    # 使用hyprctl设置壁纸
    hyprctl hyprpaper wallpaper "$monitor,$file_random"

}

hyprpaper_set

我想要实现的目标是限制hyprpaper_set函数的调用频率,例如每秒调用一次,以避免不必要的结果。

我尝试在不同的代码部分使用date +"%S"来比较日期。

我尝试使用SECONDS函数并比较经过的时间。

我尝试使用export函数使变量在全局范围内可用,以便一旦脚本重新执行,比较值可以保持不变。

我无法解决这个问题,所以非常感谢任何帮助。谢谢!

编辑:

锁定解决方案似乎很有效。修改后的脚本如下:

function hyprpaper_set() {

    # 设置锁
    lockdir=/tmp/myscript.lock

    if mkdir -- "$lockdir"; then

        # 获取一个随机文件路径
        file_random="${file_listing[RANDOM % ${#file_listing[@]}]}"

        # 使用hyprctl设置壁纸
        hyprctl hyprpaper wallpaper "$monitor,$file_random"

        # 当脚本完成或接收到信号时删除lockdir
        trap 'rm -rf -- "$lockdir"' 0    # 在脚本完成时删除目录

    else

        printf >&2 '无法获取锁定,放弃%s\n' "$lockdir"
        exit 0

    fi

}

hyprpaper_set

请注意,这是脚本的中文翻译,不包括任何额外内容。

英文:

I have set a keyboard shortcut to call a bash script, but it turns out if I spam the shortcut, the program launched from within the script crashes.

Here's a reduced version of the script:

#!/bin/bash

# Config
monitor="eDP-1"
wallpaper_dir="$HOME/Pictures/bg"

# Get list of wallpaper file paths
file_listing=($wallpaper_dir/*)

function hyprpaper_set() {

    # Get one random file path
    file_random=("${file_listing[RANDOM % ${#file_listing[@]}]}")

    # Set wallpaper with hyprctl
    hyprctl hyprpaper wallpaper "$monitor,$file_random"

}

hyprpaper_set

What I would like to achieve is a limit to how often the hyprpaper_set function can be called, for example once per second, to avoid undesirable results.

I have tried comparing dates at different code sections with date +"%S"

I have tried using the SECONDS function and comparing how much time has elapsed

I have tried the export function to make variables available globally so that once the script is re-executed, comparative values can remain.

I can't figure this one, so any help is greatly appreciated. Thanks!

Edit:

A lock solution seems to work well. Modified script:

function hyprpaper_set() {

    # Set lock
    lockdir=/tmp/myscript.lock

    if mkdir -- "$lockdir"; then

        # Get one random file path
        file_random=("${file_listing[RANDOM % ${#file_listing[@]}]}")
       
        # Set wallpaper with hyprctl
        hyprctl hyprpaper wallpaper "$monitor,$file_random"

        # Remove lockdir when the script finishes, or when it receives a signal
        trap 'rm -rf -- "$lockdir"' 0    # remove directory when script finishes

    else

        printf >&2 'cannot acquire lock, giving up on %s\n' "$lockdir"
        exit 0

    fi

}

hyprpaper_set

答案1

得分: 1

以下是翻译好的代码部分:

您可以通过将上次脚本运行的时间戳存储在文件中,然后在每次脚本运行时将当前时间戳与保存的时间戳进行比较来实现此操作。

这是已经修改以添加此功能的脚本:

#!/bin/bash

# 配置
monitor="eDP-1"
wallpaper_dir="$HOME/Pictures/bg"
time_stamp_file="$HOME/.last_hyprpaper_run"

# 获取壁纸文件路径列表
file_listing=($wallpaper_dir/*)

function hyprpaper_set() {
    # 获取一个随机文件路径
    file_random="${file_listing[RANDOM % ${#file_listing[@]}]}"

    # 使用 hyprctl 设置壁纸
    hyprctl hyprpaper wallpaper "$monitor,$file_random"
}

# 检查脚本是否在不到一秒前运行
if [ -f "$time_stamp_file" ]; then
    last_run=$(cat "$time_stamp_file")
    current_time=$(date +%s)
    time_diff=$((current_time - last_run))

    if [ "$time_diff" -lt "1" ]; then
        echo "请等待一秒钟后再次运行脚本。"
        exit 1
    fi
fi

# 保存当前时间戳
date +%s > "$time_stamp_file"

# 调用函数
hyprpaper_set
英文:

You could achieve this by storing the timestamp of the last script run in a file and then comparing the current timestamp with the saved one each time the script runs.

Here's your script modified to add this functionality:

#!/bin/bash

# Config
monitor="eDP-1"
wallpaper_dir="$HOME/Pictures/bg"
time_stamp_file="$HOME/.last_hyprpaper_run"

# Get list of wallpaper file paths
file_listing=($wallpaper_dir/*)

function hyprpaper_set() {
    # Get one random file path
    file_random=("${file_listing[RANDOM % ${#file_listing[@]}]}")

    # Set wallpaper with hyprctl
    hyprctl hyprpaper wallpaper "$monitor,$file_random"
}

# Check if the script was run less than a second ago
if [ -f "$time_stamp_file" ]; then
    last_run=$(cat "$time_stamp_file")
    current_time=$(date +%s)
    time_diff=$((current_time - last_run))

    if [ "$time_diff" -lt "1" ]; then
        echo "Please wait a second before running the script again."
        exit 1
    fi
fi

# Save the current timestamp
date +%s > "$time_stamp_file"

# Call the function
hyprpaper_set

huangapple
  • 本文由 发表于 2023年5月22日 08:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定