read variable from file show empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read variable from file show empty
# 1  
Old 12-02-2010
read variable from file show empty

Hi Folks,

I am trying to read input from single line from a file and pass the read variable to one of the commands. However when I run the script it keeps the variable to be empty. I can however echo the variable ( but why it is empty when it goes to the command).

Any help will be appreciated.

Thanks,
Bhadu

::Script::
cat "file" | read line
echo $line
echo "bhadu"

=====
-> sh -x locfileinput
+ cat file
+ read line
+ echo


+ echo

+ echo bhadu
bhadu
# 2  
Old 12-02-2010
Code:
read line <<< `cat file`

or simply
Code:
line=`cat file`

# 3  
Old 12-02-2010
What should I do if I want to read the file line by line ?

It seems
read line <<< `cat file` will copy entire file into line ? how do i then parse it line by line ?
# 4  
Old 12-02-2010
If you want to read file line by line, AWK is your first option.
# 5  
Old 12-02-2010
What is is you file? if ist a string per line:
Code:
 for line in $(cat file);do echo " Line: " $line;done

# 6  
Old 12-02-2010
Code:
while read Line
do
   echo "$Line"
done <file

# 7  
Old 12-02-2010
Reading again you post...
If your file has only a single line, what don't you initialize your variable immediately then?
like
Code:
LINE=$(< file)


Last edited by vbe; 12-02-2010 at 11:03 AM.. Reason: typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

2. Shell Programming and Scripting

Read a tab separated file with empty column

Hi all, I'm trying to read a tab separated file and apply some functions on each column. I have an issue with empty column. Exemple: $ #cat with the sed to allow you to see my tab $ cat foo.txt| sed 's/\t/;/g' a;1;x b;;yI wanted to something like that: while read col1 col2 col3 do ... (4 Replies)
Discussion started by: maturix
4 Replies

3. Shell Programming and Scripting

Read variable name from file and use this?

I would like to read a variable name from file and I would like to use this... e.g. VARIABLE1=XXXXXX File.txt $VARIABLE1 Shell for STRING in `cat File.txt` echo $STRING done I would like the print XXXXXX. It's possible????:confused: Thanks :) (7 Replies)
Discussion started by: pinguc
7 Replies

4. Solaris

Empty ZFS SAN file system with high read I/O using MPXIO

I am researching the cause of an issue. The SAN file system /export/pools/zd-xxxxxxxxxxx is having a high amount of read traffic even though it is empty. It is ZFS with MPXIO. Any ideas? It's really strange considering the file system is empty, and I don't see any errors. cpu us sy... (2 Replies)
Discussion started by: christr
2 Replies

5. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

6. Red Hat

Read only file system : File show as ? ? ? ?

I have a test system, user which I have my home directory. /home/hansini The files under this directory show as ?--------- ? ? ? ? ? sam.sh ?--------- ? ? ? ? ? DDD I know on the test system OS is corrupt and it is read only file system. If I go to some other... (2 Replies)
Discussion started by: hansini
2 Replies

7. Shell Programming and Scripting

want to read variable for the file

Hi, i have one file which has list of data like this xemp 42 yeet 87 wax 223 dyne 442 i want to read each of in for loop from the script can you give me syntax Use code tags, ty. (2 Replies)
Discussion started by: mail2sant
2 Replies

8. UNIX for Dummies Questions & Answers

How to read a file into a variable?

Hello, I have a ini-file containing comma-separated e-mail addresses, an bash-script sending a mail. Mail-addresses and the mail-script are separated, so I need not to change the important mail script. But how can I read out the file into a variable? It is possible to handover the mail... (7 Replies)
Discussion started by: ABE2202
7 Replies

9. UNIX for Dummies Questions & Answers

How can I read variable values from file?

Hi, I want to pratmeterze my scripts like, my confRsync file contains varibale values for 1. host 2. Destination and 3. source like this, I want to read this values from this and assing to my makeRsyn.sh file's varibales. how to do this? (1 Reply)
Discussion started by: redlotus72
1 Replies

10. Shell Programming and Scripting

Read data from a file into a variable

I am a FORTRAN guy and not a UNIX expert by any means so sorry if this sounds dumb, but all I want to do is have a UNIX script which reads data from a file (say 1000 lines worth, each row is a file name) and store it in an array to perform an operation on later. As maddeningly simple as this... (2 Replies)
Discussion started by: yorkdg
2 Replies
Login or Register to Ask a Question