Read -s -n not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read -s -n not working
# 1  
Old 07-06-2013
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!

Code:
$  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 ! Smilie

Regards
Nss280
# 2  
Old 07-06-2013
Try
Code:
bash script14.sh

sh is not the same shell as bash

Or make script14.sh executable and execute it like:
Code:
./script14.sh

# 3  
Old 07-06-2013
Quote:
Originally Posted by Scrutinizer
Try
Code:
bash script14.sh

sh is not the same shell as bash

Or make script14.sh executable and execute it like:
Code:
./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
Code:
sh /.....

to execute it. So this time should I used
Code:
bash /.....

?


Regards,
Nss280
# 4  
Old 07-06-2013
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.
# 5  
Old 07-06-2013
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?
# 6  
Old 07-06-2013
Yes, try:
Code:
echo "$varb"

or better yet:
Code:
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..

Last edited by Scrutinizer; 07-06-2013 at 04:18 PM..
# 7  
Old 07-07-2013
From which user do you expect input when you run the script from a crontab?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using read to assign value to bash variable not working

Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. # openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}' vcpus 92 # echo $? 0 # openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies

2. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

While read -a line not working in ksh

while read -a line; this is not working in ksh. what is the equivalent of this in ksh. read: -a: unknown option (2 Replies)
Discussion started by: archana25
2 Replies

4. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

5. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

6. Shell Programming and Scripting

Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below - #!/bin/bash file=$1 v=0 if then echo "$0 filename" exit 1 fi if then echo "$file not a file" exit 2 fi while read -n... (14 Replies)
Discussion started by: linux_learner
14 Replies

7. Shell Programming and Scripting

need to read lines in file and compare value in if not working

Hello, I have this file that sometime contains 0 lines and sometimes 1 or more. It's supposed to then put the result (could be 0 or 1 or 2 or more) into a variable. Then it's supposed to echo using an if else statement depending on the value of the variable. flagvar='wc -l $tempfile |... (1 Reply)
Discussion started by: script_op2a
1 Replies

8. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

9. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

10. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies
Login or Register to Ask a Question