Ivan Rosa
Ivan Rosa

Reputation: 85

Shell script, how to put inputs from user without user intervention

So my idea is to make a shell script that creates a user. For example, after the command: flask fab create-admin

is called it will ask for some inputs: username (User types username, click enter) user first name (User types first name, click enter) password (User types first name, click enter)

And I want to define these inputs in the file, so when the command is called the inputs will be provided automaticly without user intervention.

How can I achieve this in shell script? Thanks!

Upvotes: 1

Views: 480

Answers (2)

akostadinov
akostadinov

Reputation: 18634

expect available in most linux distros has exactly this purpose.

Upvotes: 0

Bayou
Bayou

Reputation: 3461

I don't know flask, but if the input is required in the order of username, first name, password; you could use heredocs to automate it.

# /bin/bash

source password.txt # a file that has the content of:
                    # username=user
                    # firstname=Ivan
                    # password=123


flask fab create-admin <<-EOF
${username}
${firstname}
${password}
EOF

exit 0

However, it's typically a bad idea to store passwords in plain text. You could use read to ask for user input once and reuse it multiple times as long as the script is running.

read -sp "Password? " password
echo $password

Upvotes: 1

Related Questions