Shell Script that reads a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script that reads a file
# 1  
Old 05-24-2016
Shell Script that reads a file

I have below script to read a file line by line. How can I ensure that the loop will stop after last line.

Code:
 #!/bin/bash
 while read -r mod ver tarball; do     
    echo $mod 
done < taskfile.txt


Last edited by Don Cragun; 05-24-2016 at 06:15 AM.. Reason: Add CODE tags.
# 2  
Old 05-24-2016
Why wouldn't it stop after the last line?

Do you think that whatever interpreter is running your script is going to add phantom lines to your input file when it hits end-of-file?

(Note that when #!interpreter does not start in the 1st character of the 1st line of your script, it has no effect on what interpreter will be invoked by your operating system to run your script and is just treated as a [misleading] comment.)
# 3  
Old 05-24-2016
while executes commands as long as a test succeeds.

read will not succeed at EOF from taskfile.txt and the loop will stop.

if the last line is not in fact a line (it doesn't end with \n), then you will reach EOF before read reaches the delimiter. An example:

Code:
mute@zbox:~$ printf '1\n2\n3' > file
mute@zbox:~$ while read -r a b c; do echo "$a"; done < file
1
2
mute@zbox:~$

Text files by definition should end with a newline, but if this is the problem you need to work around you can try entering your loop again with a test like this:

Code:
mute@zbox:~$ while read -r a b c || [[ $a ]]; do echo "$a"; done < file
1
2
3

# 4  
Old 05-24-2016
Thanks for the reply.

Will it not be an endless loop if I will not put a counter to it? Or an end of file flag?
# 5  
Old 05-24-2016
Quote:
Originally Posted by aderamos12
Thanks for the reply.

Will it not be an endless loop if I will not put a counter to it? Or an end of file flag?
No! After the last line in your file is read, the next read will report that it found end-of-file. No special flags are needed. When you read from a regular file, and no more data is available, the read() system call will return 0 (no data available). When you read from a pipe and no more data is available at that moment, the read() system call will hang until more data is available (in which case it will return the data available at that time or the number of bytes requested, whichever is smaller) or all file descriptors open for writing to that pipe have been closed (in which case it will return 0).

The operating system knows how big a file is and won't return more data to the user than what exists in the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. UNIX for Dummies Questions & Answers

Sudo reads password from a .cfg file

cleanwork /saswork removes sas orphanded processes in the saswork directory. Subdirectories under sasem are sas94, sas92 and sasworks . I am getting the following error messages: 1. '/usr/bin/sudo -S apt-get update <~/opt/SiM/pos/ps/db_auth.cfg... (4 Replies)
Discussion started by: dellanicholson
4 Replies

3. Shell Programming and Scripting

Help with script that reads and writes java console Minecraft

Hi I am looking for an easy way to lock game mode (0) for everyone included op on a Minecraft server. It can be a script that every time a player changes game to 1 the script changes back to 0. What the player writes is visible in the java console. I am not good at script programming and my... (0 Replies)
Discussion started by: MyMorris
0 Replies

4. UNIX for Dummies Questions & Answers

Difference between buffered disk reads and cached reads?

I was analyzing the Disk read using hdparm utility. This is what i got as a result. # hdparm -t /dev/sda /dev/sda: Timing buffered disk reads: 108 MB in 3.04 seconds = 35.51 MB/sec # hdparm -T /dev/sda /dev/sda: Timing cached reads: 3496 MB in 1.99 seconds = 1756.56 MB/sec... (1 Reply)
Discussion started by: pinga123
1 Replies

5. Shell Programming and Scripting

script that reads a value and send an email on 89th day

hi all, Currently in my system a user's password expires after 90 days and the last passsword change time is stored in an attribute pwdchange date. i need a linux script that read this attribute and add 89 days and send an email that the user password is expiring the next day. i can store... (1 Reply)
Discussion started by: manankapoor
1 Replies

6. Shell Programming and Scripting

script that reads all the scripts in the directory and run them within that script

Hi I am trying to write a shell script that is reading all the scripts in the current directory (currently 5) and is allowing me to run the scripts that is in the directory. I want that this scripts asks te user to execute 1 of the listed scripts. I have 4 sample scripts in the directory:... (8 Replies)
Discussion started by: I-1
8 Replies

7. Shell Programming and Scripting

how to read dbf file in shell script and to convert dbf file usinf shell script

Hi all, I am new to shell scripting. I have dbf file and I need to convert it into csv file. OR, can i read the fields from a .dbf file and OR seprate the records in dbf file and put into .csv or txt. Actually in the .dbf files I am getting , the numbers of fields may vary in very record and... (6 Replies)
Discussion started by: gauara
6 Replies

8. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

9. Shell Programming and Scripting

Shell script that reads from configuration file to get database

Hi intelligent beings, I am trying to write a script that 1. Accepts the following arguments: store_number div_number user_id div_code 2. Will read from a configuration file to get the Database 3. Use the div_code to determine which value to retrieve from the configuration file ... (1 Reply)
Discussion started by: dolo21taf
1 Replies

10. Shell Programming and Scripting

sh script that reads/writes based upon contents of a file

Hi everyone, Ive got a quick question about the feasibility and any suggestions for a shell script. I can use sh or ksh, doesnt matter. Basically, Ive got an output file from a db2 command that looks like so: SCHEMA NAME CARD LEAF ELEAF LVLS ISIZE NDEL KEYS F4 F5 ... (3 Replies)
Discussion started by: rdudejr
3 Replies
Login or Register to Ask a Question