首页 > 解决方案 > 是否可以在 irc 中运行 bash 命令?

问题描述

在 ssh 中,我可以使用执行我的 bash 脚本

./myscript.sh -g House -y 2019 -u https://someurl.com Artist - Album

该脚本从包含各种艺术家的子文件夹的目录中读取,但是当我从 IRC 执行触发器时,它告诉我文件夹名称无效

irc 触发器是!myscript -g House -y 2019 -u https://someurl.com Artist - Album.

就目前而言,我使用此代码来触发 IRC 命令

proc dupe:myscript {nick host hand chan arg} {
    set _bin "/home/eggdrop/logfw/myscript.sh"

    if {[catch {exec $_bin "$arg" &} error]} {
        putnow "PRIVMSG $chan :Error.. $error"
    } else {
        putnow "PRIVMSG $chan :Running.. $arg"
    }
}

我得到的错误是找不到文件夹名称,因为它报告为 -g House -y 2019 -u https://someurl.com Artist - Album

所以我需要 irc 或 bash 来删除 optarg 部分,以便只显示 irc 中的文件夹名称。

我认为错误的出现是因为 tcl 正在发送带引号的字符串,但不确定如何解决该问题

标签: bashtcl

解决方案


问题是您$arg作为一个字符串而不是多个参数发送。修复可能是这样做的:

if {[catch {exec $_bin {*}$arg &} error]} {

(您的其余代码将是相同的。)


您可能需要采取一些额外的步骤来防御进行重定向和其他恶作剧的混蛋。这很容易:

proc dupe:myscript {nick host hand chan arg} {
    set _bin "/home/eggdrop/logfw/myscript.sh"

    # You might need this too; it ensures that we have a proper Tcl list going forward:
    set arglist [split $arg]

    # Check (aggressively!) for anything that might make exec do something weird
    if {[lsearch -glob $arglist {*[<|>]*}] >= 0} {
        # Found a potential naughty character! Tell the user to get lost…
        putnow "PRIVMSG $chan :Error.. bad character in '$arg'"
        return
    }

    if {[catch {exec $_bin {*}$arglist &} error]} {
        putnow "PRIVMSG $chan :Error.. $error"
    } else {
        putnow "PRIVMSG $chan :Running.. $arg"
    }
}

推荐阅读