2

If I use "read -s" it deletes the existing line and brings the next prompt to the same line as mentioned below. Please let me know any solution to this error. I need to use "-s" with read not to echo the password on the screen.

script:


$ cat a.sh
printf "Enter the db name : "
read -r sourcedb
printf "Enter the source db username: "
read -r sourceuser
printf "Enter the source database password: "
read -s sourcepwd;
printf "Enter the target database username: "
read -r targetuser

Current Output:


$ ./a.sh
Enter the db name : ora
Enter the db username: system
Enter the db password: Enter the target database username:

Desired Output:


$ ./a.sh
Enter the db name : ora
Enter the db username: system
Enter the db password: 
Enter the target database username:

I am using Linux.

4
  • What shell are you using? Your script has no shebang line, and you are running it as ./a.sh so it isn't clear what shell will be running it. What shell do you use?
    – terdon
    Commented Apr 19, 2023 at 9:10
  • sorry, Its a bash shell. #!/bin/bash.
    – dbadmin
    Commented Apr 19, 2023 at 10:00
  • The first sentence is partly incomprehensible. Can you fix it, please? For instance, do you mean "If I use "read -s", it deletes the existing line..."? Commented Apr 19, 2023 at 17:42
  • Sorry, i have correct it now. Thanks.
    – dbadmin
    Commented Apr 20, 2023 at 6:27

1 Answer 1

6

-s is not a standard option of the read utility. In ksh, -s is to save the input on the shell's history, in bash and zsh, it's to suppress the terminal echo, in most other shells, it's an invalid option, so you need to specify a she-bang and change the misleading .sh extension if you want to use it.

Suppressing the terminal echo means the CR and NL that are echoed when you press Enter will also be suppressed, so you need to send them by hand. Just print a newline before the next prompt (it will be changed to CR+NL by the terminal driver).

Prompts should also go to stderr rather than stdout which should be reserved for the output produced by your script.

#!/bin/bash -
printf>&2 "Enter the db name: "
read -r sourcedb
printf>&2 "Enter the source db username: "
read -r sourceuser
printf>&2 "Enter the source database password: "
IFS= read -rs sourcepwd
printf>&2 "\nEnter the target database username: "
read -r targetuser

The read builtin of both bash and zsh (the shells that support -s to suppress echo) support issuing the prompt by themselves (which they send to stderr). In zsh with:

read 'var?prompt'

Like in ksh, and in bash:

read -p prompt var

So instead of using printf, you could do:

#!/bin/bash -
read -rp 'Enter the db name: ' sourcedb
read -rp 'Enter the source db username: ' sourceuser
IFS= read -rsp 'Enter the source database password: ' sourcepwd
read -rp $'\nEnter the target database username: ' targetuser
3
  • Am using bash shell. #!/bin/bash. Thanks for the response. Am able to resolve it with the answer mentioned above using \n with printf. Somehow "read -s" is deleting the current line, I had to use \n\n for the printf 's next to "read -s". Anyhow it resolved my issue. Thanks a lot for the quick response and solution.
    – dbadmin
    Commented Apr 19, 2023 at 10:11
  • @dbadmin are you on Windows, perhaps, using WSL?
    – terdon
    Commented Apr 19, 2023 at 10:17
  • No, Am using RHEL Linux
    – dbadmin
    Commented Apr 20, 2023 at 6:25

You must log in to answer this question.

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