In Bash, how can I output a new line after read -es? - linux

current code:
echo -n "password: "
read -es password
echo -n "ok"
behavoir in linux:
password: ok
Can we make it like:
password:
ok
thanks,
wxie
The following will work:
read -p "password: " -es password
echo
echo "ok"

Maybe:
read -p "password: " -es password
printf "\n ok \n"

If you want a newline before the OK, this should do it:
echo -e "\nok"
The -e enables interpretation of the backslash codes.

From echo's man page: -n do not output the trailing newline. Try it without the option.

We can use \n as a line separator in printf, there is an extra line for padding after "ok" which you can remove
printf "password: " && read -es password && printf "\n\nok\n"
If you are saving a password this way it would still be contained in the variable $password in plain text, the string below would print it openly.
printf "$password"
Your code sample uses echo and should be using printf, there are articles on echo's POSIX compliance, echo could work like Greg Tarsa points out, but not all versions are built with switch options like -e or -n.
I find it readable to use printf, also other "languages" have similar commands such as PRINT, print, println, print() and others.
If you need to use echo you could
echo -n "password: " && read -es password && echo -e "\nok\n"

Related

How to pass multiple variables as input in shell script in non-interactive way

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.
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
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

How to 'read -s' in shell?

I know that user input can be read silently using bash with read -s someVar and I was wondering if there is a /bin/sh equivalent that allows user input without displaying it on the command line?
Note: I am just curious if /bin/sh read supports this feature somehow.
Use the stty command to turn off echoing of typed characters.
get_entry () {
printf "Choose: "
stty -echo
IFS= read -r choice
stty echo
printf '\n'
}
get_entry
printf "You chose %s\n" "$choice"

read -p returns "read: no query process" using korn shell ksh

created a simple shell file that contains this:
read -p ThePrompt TheSomthing
echo $TheSomething
Run it, and it returns
-ksh[1]: read: no query process
I've tried single quotes, double quotes around ThePrompt and the man page specifically says "-p" is to use a prompt but it is not working for me. Can anyone tell me what I'm doing wrong? Thanks!
In Ksh you can use this format:
echo "ThePrompt\c"
read TheSomthing
echo $TheSomething
From the googled man page:
The -un and -p options cause input to be read from file descriptor n or the current co-process (see Co-Processes above for comments on this), respectively. If the -s option is used, input is saved to the history file.
To use a prompt, write this instead:
read TheSomething?'ThePrompt'
I found a word around:
echo -n 'prompt: '
read input1
echo -n 'prompt: '
read input2
.
.
.
I don't know why the -p doesn't work as described in the man page. If anyone out there has insights, please reply.
Thanks!
Sorry for reviving this question, but I do my shell scripts in KSH, so I was in the same predicament, until I came with this.
My solution to capture a single character:
$> echo -e "My prompt: \c" ; read -n 1 -s -r FOO ; echo -e "\b"
My prompt:
$> echo $FOO
d
$>
For a longer string remove the "-n 1" from the read command:
$> echo -e "My prompt: \c" ; read -s -r FOO ; echo -e "\b"
My prompt:
$> echo $FOO
this is my entry!!!
$>
I hope this is what you were looking for... Cheers!

How to display newline in ssh

I'm trying to do the following:
#!/bin/sh
ssh user#server "echo \"Test \n for newline\""
This displays:
test \n for newline
How do I get the shell to interpret \n as an actual newline?
Try using the -e option, e.g., echo -e "Test \n for newline".
If your echo doesn't have a -e option, then I'd use printf. It's widely available and it does not have nearly as many variations in it's implementations.
For greater portability, use printf instead of echo.
#!/bin/sh
ssh user#server 'printf "Test \n for newline"'
According to the POSIX standard, echo should process \n as a newline character. The bash built-in echo does not, unless you supply the -e option.
Just use one of
#!/bin/sh
ssh user#server "echo -e \"Test \n for newline\""
or
#!/bin/sh
ssh user#server 'echo -e "Test \n for newline"'
or
#!/bin/sh
ssh user#server "echo -e 'Test \n for newline'"
or even
#!/bin/sh
ssh user#server "echo 'Test
for newline'"
All of those will display
Test
for newline
(note the trailing space after the first line and the leading space before the second one - I just copied your code)
Before exectuning ssh command update the IFS environment variable with new line character.
IFS='
'
Store the ssh command output to a varaible
CMD_OUTPUT=$(ssh userName#127.0.0.1 'cat /proc/meminfo')
iterate the output per line
for s in $CMD_OUTPUT; do echo "$s"; done

echoing in shell -n doesn't get printed the right thing

I know that this is some kind of special character issue but I do not know how to solve it.
I type in console
echo "-n"
and nothing get printed :(
I also tried with
echo -e "-n"
to execute the special characters (the one escaped from sequence) but again nothing happend
how can I print "-n" ?
Try
printf "%s\n" -n
or
printf "%s\n" '-n'
Here is one way:
aix#aix:~$ echo -e '\x2dn'
-n
It escapes the - as \x2d.
A more verbose way is to print the two characters separately:
aix#aix:~$ echo -n -; echo n
-n
Here, the -n instructs the first echo to not print a newline; it is not related to the -n being printed. :)

Resources