The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reading variable value from a file aixjadoo Shell Programming and Scripting 1 4 Weeks Ago 05:50 AM
Reading a variable from file shash UNIX for Dummies Questions & Answers 6 08-06-2009 11:37 AM
Reading variable from file variable values sagii Shell Programming and Scripting 5 06-27-2009 02:06 PM
Reading a file that is already open by another process Tlg13team Shell Programming and Scripting 7 08-13-2008 09:57 PM
Reading file and assigning that to Variable Vaddadi Shell Programming and Scripting 2 04-03-2008 03:49 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
Dicloflex Dicloflex is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
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; 3 Weeks Ago at 01:43 PM.. Reason: code tags, please...
  #2 (permalink)  
Old 3 Weeks Ago
redhead's Avatar
redhead redhead is offline
Registered User
  
 

Join Date: Feb 2002
Location: Denmark
Posts: 68
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 (permalink)  
Old 3 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by redhead View Post
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 (permalink)  
Old 3 Weeks Ago
Dicloflex Dicloflex is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0