Read -s -n not working

Hi All,

Using bash.
I am trying to accept one char input for my code.
The user is suppose to enter d(disburse) or g(garner).

I am stuck with read command. It works for me on the command line, but when i put it in a shell script!

$  read -s -n 1 varb
$ echo $varb
g
$ read -s -n 1 varb
$ echo $varb
d
$ cat xxxxx.sh
#!/bin/bash
read -s -n 1 varb
echo $varb
## executing as below
$
$ sh  script14.sh
read: Illegal option -s

I am not able to figure out why is this not working ! :frowning:

Regards
Nss280

Try

bash script14.sh

sh is not the same shell as bash

Or make script14.sh executable and execute it like:

./script14.sh

Thankyou Scrutinize. It worked, I have one question here. I am going to call this script from a cron. As of now in my cron, I have been using

sh /..... 

to execute it. So this time should I used

bash /..... 

?

Regards,
Nss280

Indeed sh is not the right shell to use, unless the script is only using sh syntax, which is not the case here, since your script sample is using bash syntax.

Got that. Also while I was trying the read command it works well for almost all characters including ?. But it didn't work for *. It showed the list of files in the current directory. Can you please help me understand that?

Yes, try:

echo "$varb"

or better yet:

printf "%s\n" "$varb"

If you do not use those double quotes, then if the variable contains an asterisk, then this will be expanded by the shell to all files in the current directory..

From which user do you expect input when you run the script from a crontab?

Hi RudiC,
During the UT phase, I have a manual run on the script. Once success, I will be passing parameters obtained from another script to call this one. I am going to use the case statement in this one.
So this script will be called indirectly from another script that in turn will be called from a cron.
So now as Scrutinizer mentioned I will now call the main script through bash / rather then sh /
Regards,
Nss280