read command with bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers read command with bash
# 1  
Old 10-18-2012
read command with bash

I'm trying to understand the following piece of code using bash:

Code:
while read myline; do
  .
  .
  .
done

My initial interpretation was that the user is prompted to enter data which will be stored in the myline variable. It will then enter the while loop. Maybe I'm completely wrong (I am a newbie).

However I am not prompted to enter anything nor does the program wait for me to enter anything. Also it does not enter the loop.
# 2  
Old 10-18-2012
Code:
 
#/bin/bash
 
print Enter line:
while read myline
do
   echo $myline
   print Enter line:
done

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-18-2012
No this doesn't work like this. Here you normally read a line by line from a file.

Code:
$ cat file
LINE1
LINE2
I_AM_SEPARATOR
LINE11
LINE22
I_AM_SEPARATOR

$ while read line; do  echo $line; done<file
LINE1
LINE2
I_AM_SEPARATOR
LINE11
LINE22

To get prompt, use this...


Code:
$ cat test.sh
echo "enter something"
read line
echo $line

$ sh test.sh
enter something
while read
while read

This User Gave Thanks to pamu For This Post:
# 4  
Old 10-18-2012
Quote:
Originally Posted by rdrtx1
Code:
 
#/bin/bash
 
print Enter line:
while read myline
do
   echo $myline
   print Enter line:
done

Thanks for replying but I need to understand my code as it is. My code does work (I know because it's used in work) but I don't understand how it works and can't get it to work.
# 5  
Old 10-18-2012
bash

Hi,

Here while loop will keep on reading the input from the user and print it to STDOUT. This is called infinite loop. One more thing, there is no
Code:
print function in bash. you have to use echo or printf instead of print.

Cheers,
Ranga Smilie
# 6  
Old 10-18-2012
Quote:
Originally Posted by pamu
No this doesn't work like this. Here you normally read a line by line from a file.

Code:
$ cat file
LINE1
LINE2
I_AM_SEPARATOR
LINE11
LINE22
I_AM_SEPARATOR
 
$ while read line; do  echo $line; done<file
LINE1
LINE2
I_AM_SEPARATOR
LINE11
LINE22

To get prompt, use this...


Code:
$ cat test.sh
echo "enter something"
read line
echo $line
 
$ sh test.sh
enter something
while read
while read

So is the purpose of the while loop to just keep reading each line within the while loop until something causes it to come out or stop e.g. an exit statement

---------- Post updated at 05:21 PM ---------- Previous update was at 05:20 PM ----------

Quote:
Originally Posted by rangarasan
Hi,

Here while loop will keep on reading the input from the user and print it to STDOUT. This is called infinite loop. One more thing, there is no
Code:
print function in bash. you have to use echo or printf instead of print.

Cheers,
Ranga Smilie
thanks.
# 7  
Old 10-18-2012
Quote:
Originally Posted by millsy5
So is the purpose of the while loop to just keep reading each line within the while loop until something causes it to come out or stop e.g. an exit statement
It is just one way of using while loop..

Code:
while Condition
do
# do things here
done

This is basic while loop. Till the condition is true it is in the while loop.Smilie
When condition gets fail it came out of loop.

Yes you can exit if you want putting some condition. In above case it will print all the lines of the file.

I hope this helpsSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read with -p option

I have this simple code to stop script execution. I did not invent it, and it works. It used to have fackEnterKey as option, but it works without it. My curiosity question is - the "function " terminates when "enter" key is pressed and only "enter" key. I did read the man and... (5 Replies)
Discussion started by: annacreek
5 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

4. Shell Programming and Scripting

Bash while read line

I have a script batch_vmdgenpqr.sh which has a problem: #!/bin/bash while read line do vmd.sh -dispdev text -e vmdgenpqr.tcl -args $line done<file The do line calls another program, VMD (called by vmd.sh) and it requires the values from $line, but this is not recognised by... (3 Replies)
Discussion started by: chrisjorg
3 Replies

5. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

6. UNIX for Dummies Questions & Answers

Read Bash Script

I am very new to all these programming languages and really love Linux but have only begun to dive into bash scripting... I am curious what's going on with this script... #!/bin/bash if ; then # xen grep control_d /proc/xen/capabilities >& /dev/null if ; then # domU -- do not run on... (2 Replies)
Discussion started by: BrianBlaze
2 Replies

7. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

8. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

9. UNIX for Dummies Questions & Answers

Cygwin bash script and read command

Hello everyone, I am struggling a bit with a batch script that I need to run in cygwin. I work in winXP and I had to write some awk scripts to do some file manipulation, and now I would like to automate the process by just running a batch file so that my colleagues can use it easily. Now, the... (2 Replies)
Discussion started by: Teroc
2 Replies

10. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies
Login or Register to Ask a Question