bash 使用脚本将字符串传递给 linux cli 交互式程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27671801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:04:49  来源:igfitidea点击:

Pass string to a linux cli interactive program with a script

linuxbashtelegram

提问by Jacquelyn.Marquardt

I'm using a library for accessing telegram via command line in bash linux ubuntu. It is telegram-cli. REFERENCE https://github.com/vysheng/tg

我正在使用一个库通过命令行在 bash linux ubuntu 中访问电报。它是电报-cli。参考https://github.com/vysheng/tg

It is an interactive program, which can be launched by

它是一个交互式程序,可以通过

./bin/telegram-cli -k tg-server.pub

It outputs these lines:

它输出这些行:

Telegram-cli version 1.1.1, Copyright (C) 2013-2014 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
I: config dir=[/home/utonto/.telegram-cli]
>

With the ">" prompt. To send a msg, at this prompt you have to type

带有“>”提示。要发送消息,在此提示下您必须键入

msg USERNAME This is a test message

I would like to make a script running on my pc, telling me the room temperature when i'm at work. How can I do that? I searched SO and found a similar question. It was advised to use the "expect" command, I made so but it didn't work. Maybe -Surely- cos i'm a beginner. I also tried

我想在我的电脑上运行一个脚本,告诉我工作时的室温。我怎样才能做到这一点?我搜索了 SO,发现了一个类似的问题。建议使用“expect”命令,我这样做了,但没有用。也许 - 当然 - 因为我是初学者。我也试过

echo "msg USERNAME this is a message |
telegram-cli -k ......"

but it didn't work. For your knowledge,on my android i have a telephone number which i use for telegram, but i registered to telegram-cli using another phone. Can someone help me?

但它没有用。据您所知,在我的 android 上,我有一个用于电报的电话号码,但我使用另一部手机注册了电报 cli。有人能帮我吗?

回答by eliafino

See here: remote control with Telegram

请参阅此处:使用 Telegram 进行远程控制

To intercept a new incoming message we create a file action.lua

为了拦截新的传入消息,我们创建了一个文件 action.lua

"Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping."

“Lua 是一种强大、快速、轻量级、可嵌入的脚本语言。

Lua 将简单的过程语法与基于关联数组和可扩展语义的强大数据描述结构相结合。Lua 是动态类型的,通过为基于寄存器的虚拟机解释字节码来运行,并具有自动内存管理和增量垃圾收集功能,使其成为配置、脚本编写和快速原型设计的理想选择。”

From http://www.lua.org.

来自http://www.lua.org

sudo nano /home/pi/tg/action.lua

with this content

有了这个内容

function on_msg_receive (msg)
    if msg.out then
        return
    end
    if (msg.text=='ping') then
        send_msg (msg.from.print_name, 'pong', ok_cb, false)
    end
end

function on_our_id (id)
end

function on_secret_chat_created (peer)
end

function on_user_update (user)
end

function on_chat_update (user)
end

function on_get_difference_end ()
end

function on_binlog_replay_end ()
end

Save and exit, when incoming text message is "ping", Telegram answers us with a text message containing "pong".

保存并退出,当收到的短信是“ping”时,Telegram 会回复我们一条包含“pong”的短信。

move in tg

cd /home/pi/tg

Then type

然后输入

bin/telegram-cli -k tg-server.pub -W -s action.lua

回答by jim mcnamara

Try a here document:

试试这里的文档:

#!/bin/bash
./bin/telegram-cli -k tg-server.pub <<-EOF
msg USERNAME This is a test message
exit
EOF

I added exit - do not know if it is needed. NOTE: the EOF thing can be any bunch of letters that the shell cannot interpret as a command. The - in front of the first EOF is there to tell the here doc that the next EOF is not in the leftmost column. If you omit it, then the last EOF has to be as far left as you can go. Column #1.

我添加了 exit - 不知道是否需要。注意:EOF 可以是 shell 无法解释为命令的任何一组字母。第一个 EOF 前面的 - 用于告诉此处的文档下一个 EOF 不在最左边的列中。如果省略它,则最后一个 EOF 必须尽可能地靠左。第 1 列。

With regard to getting the temperature, I have no idea how you would do that without special hardware.

关于获得温度,我不知道如果没有特殊硬件,您将如何做到这一点。

回答by David C. Rankin

If you have some way of getting the temperature to your computer, then to have bash send it to you is fairly trivial. The proper way to handle a recurring task is with cron. You would create a crontabentry launching your script (or directly sending the temperature itself to telegram). The format for a cron entryis:

如果您有某种方法可以将温度发送到您的计算机,那么让 bash 将其发送给您是相当简单的。处理重复性任务的正确方法是使用cron. 您将创建一个crontab启动脚本的条目(或直接将温度本身发送到电报)。a 的格式为cron entry

* * * * * command_to_execute
| | | | |
| | | | +- day of week  (0-6) (Sunday = 0)
| | | +--- month        (1-12)
| | +----- day of month (1-31)
| +------- hour         (0-23)
+--------- minute       (0-59)

In your case you can set the crontabby using crontab -e(to edit your crontab entries). To send the temperature every 10 minues it would be:

在您的情况下,您可以crontab使用crontab -e(编辑您的 crontab 条目)来设置。要每 10 分钟发送一次温度,它将是:

*/10 * * * * ./script/to/call

or you could try invoking telegram directly in your crontabwith something similar to:

或者您可以尝试crontab使用类似于以下内容的内容直接在您的电报中调用电报:

*/10 * * * * echo "msg $USER The current temp is: $temperature" | ./bin/telegram-cli -k tg-server.pub

Lastly, check the options closely in telegram-cli, there may very well be one that takes a message as an argument and forwards it avoiding interactive mode altogether (I haven't checked).

最后,仔细检查 中的选项telegram-cli,很可能有一个将消息作为参数并转发它,完全避免交互模式(我还没有检查过)。