-1

I want to write a sed script that reads user input, and if the variable matches a predefined string it should print welcome, otherwise it should print sorry.

For example, if user inputs "Hello" the system should print "Welcome", if not it should print "Sorry".

3
  • 2
    What have you tried so far? why do you want to use sed for this task, specifically? As it stands, this isn't really a question within the terms of this site - please take a look at How do I ask a good question? Jun 11, 2015 at 12:05
  • sed isn't the right tool, use grep Jun 11, 2015 at 12:07
  • Are you doing this to get a password?
    – Tim
    Jun 11, 2015 at 12:08

2 Answers 2

2
#!/bin/bash
read -p "Enter some input:" USERINPUT
(grep "$USERINPUT" -q inputfile.txt ) && echo "Welcome" || echo "Sorry"
1

If you are trying to get input of a password, that is certainly not how to do it.

First, the comparison password should be stored as a hash. Create that with md5sum for example:

echo hello | md5sum

gives

b1946ac92492d2347c6235b4d2611184  -

Now to get the input, we can use @Serg's answer, slightly modified. We use the -s to hide the input (keep it secret).

echo Please enter password:
read -s input

and compare it to the hash:

input=$(echo $input | md5sum)

if [ $input == "b1946ac92492d2347c6235b4d2611184  -" ]
then
    echo "Welcome, your password was correct"
else
    echo "Sorry, wrong password"
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .