0

I want to create a bash script that is simular to a programming interpreter like mongo, node, redis-cli, mysql, etc.

I want to be able to use a command like test and it behave like the examples above.

thomas@workstation:~$ test
> 

How do I make a command that behaves like this? What is this called?

I want to be able to take the content and turn it into a variable.

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

I only want to take one "entry" after enter is pressed once I want to be able to process the string "hello world" in the code, like echo it.

What is this called? How do I make one using BASH?

2
  • http://en.wikipedia.org/wiki/Parsing Commented Dec 14, 2012 at 20:44
  • Why not just implement your desired commands as scripts (or shell functions in a single script)? Bash is already a very powerful environment & does a great job of, you know, being a shell. Commented Dec 14, 2012 at 21:25

5 Answers 5

2

I think "read" is what you are looking for, isn't it?

here is a link with some examples: http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard

so you can do stuff like this:

read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"
Sign up to request clarification or add additional context in comments.

1 Comment

I understand that commands can take flags and arguments I wanted to understand how the alternative works.
1

I'm sorry this doesn't answer you directly, but it might be worth it to look into using a more fully capable programming language such as Python, Ruby, or Perl for a task like this. In Python you can use the raw_input() function.

user_command = raw_input('> ')

would yield your prompt.

1 Comment

"Prompt" is a great word thanks for that. I see that bash also might be insufficient.
1

First, do not name your script test. That generates too much confusion. Whatever you call it, you can do many things:

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

If your shell supports it:

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

or

#!/bin/sh
printf '> '
sed 1q    # This will print the input.  To store in in a variable: a=$( sed 1q )

1 Comment

I knew I was going to get flac for naming that example test, thanks for your submission. Perfect.
0
[spatel@tux ~]$ read a
Hello World!!!!!
[spatel@tux ~]$ echo $a
Hello World!!!!!

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.