Reading from standard input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from standard input
# 1  
Old 07-22-2011
Reading from standard input

So, I am new to shell scripting and have a few problems.
I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program goes on.

I tried using test with regular expressions after taking an input like this.

Code:
read input

while test $input != q || test -z $input
do
        if test $input = n
        then
 ...

I also want my program to move on if the input just presses enter, but I get two error messages on the while statement and the if statement saying unary operator expected.
# 2  
Old 07-22-2011
You need to check the length of a string, then.

There's really no reason to use test, at all, ever these days.

Code:
STR="zzz"

while [ "$#STR" -ne 1 ]
do
        printf "Enter one letter:   "
        read STR
done

# 3  
Old 07-22-2011
May I ask the reason to not use test? I recognize the square brace as an alternative method, I'm just curious as I'm just starting to learn.

And # is?
# 4  
Old 07-22-2011
Each time you run test you're creating a new process and waiting for it to quit. [ ] are usually shell built-ins these days, so no new process is needed -- meaning, hundreds of times faster.

# is length.
# 5  
Old 07-22-2011
Corona,

I tried testing your code, and it says on the loop with the case statement "integer expression expected"
# 6  
Old 07-22-2011
Corona probably meant
Code:
${#STR}

You could also do without testing for the length, just take the first character like this:
Code:
while [ ${STR:0:1} != q ] ; then ...

# 7  
Old 07-22-2011
I'm sorry, but could you explain what the colons mean?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

standard input and cron

I have a program that requires the user to enter input values while it is being run for example in bash ... ... .. echo "Enter your input" read input echo $input ... ... ...I need to schedule this program with crontab, hence a problem, cronjobs run in the background, any ideas on how to... (10 Replies)
Discussion started by: walforum
10 Replies

3. Shell Programming and Scripting

Reading Standard Input

Hello, I am new to scripting. How do I read multiple lines from the command line? I know read reads one line, but if I have to read multiple lines, how should I do? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

4. UNIX for Dummies Questions & Answers

Reading from Screen/Standard Output

Is it possible to read from the screen or standard output? If so, may I know how I can do this? For example, I have an application running which prints out the following on the screen: Starting tools from .image-tools... imagecontrol_1: SECS/GEM-capable version is running done cindy@pgunix... (2 Replies)
Discussion started by: sippingsoda
2 Replies

5. Shell Programming and Scripting

Reading from standard input with awk

Hello, Could somebody please give me an awk example on how to read from the standard input. It means as the "read" function in Korn shell. Thx in advance ... (3 Replies)
Discussion started by: rany1
3 Replies

6. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

7. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

8. Shell Programming and Scripting

change standard input ?

Dear... I have a scrpit that contains multiple read command.... when I run the script I have to enter 3 variables so that I can get the output.. but, I dont want to put those 3 inputs manually every time... I want to make a shell that reads the 3 inputs from a file. the script name is... (4 Replies)
Discussion started by: yahyaaa
4 Replies

9. Shell Programming and Scripting

How to copy from standard input

I tried copy the output files from find command into a directory. Example, find / -name core 2>/dev/null | xargs cp???? I have known that we can use xargs to execute command lines from standard input but how to use it in this case. Or I can do something besides xargs. (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies
Login or Register to Ask a Question