ThomasReggi
ThomasReggi

Reputation: 59345

Create bash script that takes input

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?

Upvotes: 0

Views: 925

Answers (5)

ThomasReggi
ThomasReggi

Reputation: 59345

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.

Upvotes: 0

Satish
Satish

Reputation: 17397

[spatel@tux ~]$ read a
Hello World!!!!!
[spatel@tux ~]$ echo $a
Hello World!!!!!

Upvotes: 0

chsymann
chsymann

Reputation: 1670

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!"

Upvotes: 2

William Pursell
William Pursell

Reputation: 212198

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 )

Upvotes: 1

Dan Passaro
Dan Passaro

Reputation: 4387

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.

Upvotes: 1

Related Questions