0

mongo我想创建一个类似于, node, redis-cli,mysql等编程解释器的 bash 脚本。

我希望能够使用类似的命令test,它的行为类似于上面的示例。

thomas@workstation:~$ test
> 

我如何做出这样的命令?这个叫什么?

我希望能够获取内容并将其转换为变量。

thomas@workstation:~$ test
> hello world
hello world 
thomas@workstation:~$

一旦我希望能够处理代码中的字符串“hello world”,例如回显它,我只想在按下回车键后获取一个“条目”。

这个叫什么?如何使用 BASH 制作一个?

4

5 回答 5

2

我认为“阅读”是您正在寻找的,不是吗?

这是一些示例的链接:http: //bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard

所以你可以做这样的事情:

read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"
于 2012-12-14T20:47:29.673 回答
1

很抱歉,这并没有直接回答您,但考虑使用功能更强大的编程语言(如 Python、Ruby 或 Perl)来完成这样的任务可能是值得的。在 Python 中,您可以使用 raw_input() 函数。

user_command = raw_input('> ')

会产生你的提示。

于 2012-12-14T20:44:58.033 回答
1

首先,不要命名您的脚本test。这会产生太多的混乱。不管你怎么称呼它,你可以做很多事情:

#!/bin/sh
printf '> '
read line
echo "$line"

如果您的外壳支持它:

#!/bin/sh
read -p '> ' line
echo "$line"

或者

#!/bin/sh
printf '> '
sed 1q    # This will print the input.  To store in in a variable: a=$( sed 1q )
于 2012-12-14T20:45:35.837 回答
0
[spatel@tux ~]$ read a
Hello World!!!!!
[spatel@tux ~]$ echo $a
Hello World!!!!!
于 2012-12-14T20:55:55.197 回答
0

Key word that might be useful here is REPL (Read–eval–print loop) used primarily for programming languages or coding environments. Your browsers console is a great example of a REPL.

Node allows you use their REPL to build interactive apps.

于 2016-04-08T01:41:09.163 回答