Visit The New, Modern Unix Linux Community


Reading a file with while into variable and using in another process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a file with while into variable and using in another process
# 1  
Reading a file with while into variable and using in another process

I would appreciate some help, please.

I have a file cutshellb.txt, which contains four report names (but it could be any number from 1 to x. The report suffix is not necessarily consecutive:

report3785
report3786
report3787
report3788

I have a shell script which attempts to read the names and use them as a parameter in another script to run a mail package:

Code:
#!/bin/ksh
for line in `/usr/bin/cat /u1/cutshellb.txt`; do
 /u1/sendreportmail.sh MYPRT=$(`echo "$line"`) 1>sendmail.log 2>&1
done

The script contains:

Code:
cd /u1/mail/bin/
./mailsend -i -vvvv   -tany_person@mailaddress.uk -s "Mail report " 
 -a/u1/$MYPRT.pdf "  "

I would appreciate some pointers - even if it is just that this is not possible.

I found this format MYPRT=$(`echo "$line"`) on a website which suggested that there are difficulties with passing variables between parent and child processes with pipes especially within ifs and whiles.

Last edited by pludi; 11-06-2009 at 01:43 PM.. Reason: code tags, please...
# 2  
how about somethign like this:
Code:
#!/bin/ksh
for line in `/usr/bin/cat /u1/cutshellb.txt`; do
 /u1/sendreportmail.sh "$line" 1>sendmail.log 2>&1
done

and in the script have:
Code:
cd /u1/mail/bin/
./mailsend -i -vvvv   -tany_person@mailaddress.uk -s "Mail report " 
 -a/u1/$1.pdf "  "

But why not simply incorporate the mailsend command into the orriginal script ?
like:
Code:
#!/bin/ksh
for line in `/usr/bin/cat /u1/cutshellb.txt`; do
 /u1/mail/bin/mailsend -i -vvvv   -tany_person@mailaddress.uk -s "Mail report " 
 -a/u1/$line.pdf "  " 1>sendmail.log 2>&1

# 3  
Quote:
Originally Posted by redhead
how about somethign like this:
Code:
#!/bin/ksh
for line in `/usr/bin/cat /u1/cutshellb.txt`; do
 /u1/sendreportmail.sh "$line" 1>sendmail.log 2>&1
done

and in the script have:
Code:
cd /u1/mail/bin/
./mailsend -i -vvvv   -tany_person@mailaddress.uk -s "Mail report " 
 -a/u1/$1.pdf "  "

But why not simply incorporate the mailsend command into the orriginal script ?
like:
Code:
#!/bin/ksh
for line in `/usr/bin/cat /u1/cutshellb.txt`; do


Code:
bash: /usr/bin/cat: No such file or directory

More accurate would be:

Code:
for word in `/usr/bin/cat /u1/cutshellb.txt`; do

You are reading a word, not a line, into the variable.


The correct way to read a file line by line is:

Code:
while IFS= read -r line
do
  : do whatever
done < /u1/cutshellb.txt

In bash4, you can read a file into an array with mapfile:

Code:
mapfile -t array < /u1/cutshellb.txt

# 4  
Just a quick thank-you both for your help.
Have now incorporated into the one process and am using 'for word in...' and the alternative 'while IFS' successfully

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #400
Difficulty: Medium
The first release for HP 9000 Series 500 HP-UX for Series 500 was HP-UX 1.0 (1982).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading foreach variable from a text file

Hello, I have a text file named "foreach.txt" which reads like --- foreach cal ( 1 2 3 4 5 ) I am using a simple script which looks like --- #!/bin/tcsh foreach cal (1 2 3 4 5) echo "$cal" end Is it possible to modify the script in such a way that instead of writing foreach cal (1... (6 Replies)
Discussion started by: Indra2011
6 Replies

2. Shell Programming and Scripting

Reading from a File and Using as an Input variable

I need to know how the the string constant from Input File should be read and provide as input data for the script . INPUT FILE CONST VARIABLE myname=/root/dir/syslog/myname1 myname=/root/dir/syslog/myname2 myname=/root/dir/syslog/myname3 urname=/root/dir/syslog/urname1... (6 Replies)
Discussion started by: baraghun
6 Replies

3. Shell Programming and Scripting

Reading from a file and storing it in a variable

Hi folks, I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it. My file is 1.txt and its contents are VERSION=5.6 UPDATE=4 I would like to read "5.6" and "4" and store it in a variable in shell... (6 Replies)
Discussion started by: scriptfriend
6 Replies

4. Shell Programming and Scripting

Reading a variable from parameter file

HI all, I have a parameter file with entries like $$Name =Abhinav $$CUTOFF_DATE = 11/11/2209 I am reading a variable from this file using a awk command like : var2=`awk -F"" "/CUTOFF_DATE/{f=$1;}{print f;}" InputFIleName` but facing an error like awk: cmd. line:1:... (3 Replies)
Discussion started by: abhinav192
3 Replies

5. Shell Programming and Scripting

reading variable value from a file

Hello, I need to read a variable value from script. Below is the scenario I am reading a value from an external file say a.txt a.txt: Jan Feb Mar I need the corresponding value of the months in in numerics such as Jan -->1, Feb-->2 etc. I have this mapping in another file... (1 Reply)
Discussion started by: aixjadoo
1 Replies

6. UNIX for Dummies Questions & Answers

Reading a variable from file

Hi, I have a situation where I need to read a variable from another file. But the problem is that the variable in the other file is starting with $. E.g. file1: $var1=out temp_ss.sh: . file1 echo "Print : $var1" It works fine if the file1 is having var1=out (note that it is... (6 Replies)
Discussion started by: shash
6 Replies

7. Shell Programming and Scripting

reading the whole line from a file into a variable

Hi, I am doing : while read line do printf "%s\n" ${line} done <datafile.txt but I am not getting each single line from the data file assigned to the variable line (but only tokens/fields at a time). I also tried while IFS= read -r lineI want the whole line assigned or read into the... (2 Replies)
Discussion started by: shri_nath
2 Replies

8. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

9. Shell Programming and Scripting

Reading a file that is already open by another process

hi, I'm using HP unix tru64 & Win XP. i'm download a file from winxp to unix using ftp. how to know whether a remote file is currently being used by the process or not? (my loaction unix server) (7 Replies)
Discussion started by: Tlg13team
7 Replies

10. Shell Programming and Scripting

Reading file and assigning that to Variable

I am missing something here, I have a file which contains only one line and that is either a number or character string. I am trying to read the file and assign that value to a variable and here it seems I am missing something and not getting the expected results... Here is the code : #!/bin/ksh... (2 Replies)
Discussion started by: Vaddadi
2 Replies

Featured Tech Videos