0

I am trying to make this interactive script take inputs in a non-interactive way by declaring $username and $password variables in bash env and pass them as inputs to the script. By running ./input.sh <<< "$username"

#!bin/bash
read username
read password
echo "The Current User Name is $username"
echo " The  Password is $password"  

is there a way to pass both the variables as input? Because with what I have tried it only takes one input this way.

2
  • But if $username and $password are in bash env, then you can access them from your script, can't you? That's the whole point of env variables. Commented Oct 4, 2022 at 9:38
  • @chrslg yes they can be accessed , but I want to know If what i am trying to do can be done? Commented Oct 4, 2022 at 9:41

2 Answers 2

1

So, staying as close as possible as your initial try (but I doubt that is the best solution for any real problem), what you are asking is "how I can pass 2 lines with here-string".

A possible answer would be

./input.sh <<< "$username"$'\n'"$password"

here-strings are the construct you are using when using <<<. When you type ./input.sh <<< astring it is, sort-of, the same as if you were typing echo astring | ./input.sh: it use the string as standard input of ./input.sh. Since your reads read lines, you need 2 lines as standard input to achieve what you want. You could have done this that way: (echo "$username" ; echo "$password") | ./input.sh. Or anyway that produces 2 lines, one with $username one with $password and redirecting those 2 lines as standard input of ./input.sh

But with here-string, you can't just split in lines... Unless you introduce explicitly a carriage return (\n in c notation) in your input string. Which I do here using $'...' notations, that allows c escaping.

Edit. For fun, I include here the other solutions I wrote in comments, since you are not specially requiring here-strings.

(echo "$username" ; echo "$password") | ./input.sh
{echo "$username" ; echo "$password" ; } | ./input.sh
printf "%s\n" "$username" "$password" | ./input.sh
./input.sh < <(echo "$username" "$password")
./input.sh < <(printf "%s\n" "$username" "$password")

Plus of course solutions that changes ./input.sh

#!bin/bash
username="$1"
password="$2"
echo "The Current User Name is $username"
echo " The  Password is $password"

called with ./input "$username" "$password"

Or

#!bin/bash
echo "The Current User Name is $username"
echo " The  Password is $password"

called with username="$username" password="$password" ./input.sh

Sign up to request clarification or add additional context in comments.

6 Comments

not specifically here-string i just used them as an example , i tried echoing the two variables as input echo"$username" "$password " | ./input.sh I was doing it wrong ! Thanks for the explanation. Your solutions worked !! But are there any other ways to do it aswell?
Well, you could have passed username and password as an argument. ./input.sh "$username" "$password" and then drop the reads in input.sh and username and password are $1 and $2.
You could have passed them as env value (even just locally), like this: username="$username" password="$password" ./input.sh. Which creates an environment just for this call of input.sh. And then, again, in input.sh, drop the reads, and the variables are called $username and $password
Or, if needed not to change at all input.sh, that is to keep the read, and then, to find way to pass 2 lines to input.sh, well, we covered the not too covoluted way, I would say: (echo name ; echo pass) | ./input.sh, or ./input.sh <<< name$'\n'pass. One might add printf "%s\n" "$username" "$password" | ./input.sh. That is just a variation around the pairs of echo that doesn't require a subshell. In bash, you can also compound a pair of echo without subshell (the (...) starts a subshell), with {echo "$username" ; echo "$password" ; } | ./input.sh.
Beware, the second comma is compulsory
|
0

Easiest way would be something like that:

#!/bin/bash
echo "The Current User Name is $1"
echo "The Password is $2"

$1 represents the first given argument and $2 the second.

[user@vm ~]$ input.sh "user" "password"

Inside the quotation marks ("") put the argument you want to pass.

For more professional/robust solution check this out: Redhat: Bash Script Options/Arguments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.