Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output a print command to a file.

Status
Not open for further replies.

tijerina

Vendor
Mar 4, 2003
132
US
Can anyone show me how to output my print command to a file in this ksh script. I tried to output this to a file called "testout" but it is not working. Can it be done with the print command?

#!/bin/ksh

DATE=`date`
SERVER=`uname -n`

FILELIST=/DJ/PUB/testfile

while read -r FNAME
do
if [ ! -f ${FNAME} ]
then
{ print -u2 "${DATE}-File Missing:${dir} ${FNAME}-On Machine:${SERVER}" ;
} > testout
fi
done < ${FILELIST}

 
Try this...
[tt]
#!/bin/ksh

DATE=`date`
SERVER=`uname -n`

FILELIST=/DJ/PUB/testfile
OUTFILE=/DJ/PUB/testout

# Open file descriptor 3
exec 3> ${OUTFILE}


while read -r FNAME
do
if [ ! -f ${FNAME} ]
then
print -u3 &quot;${DATE}-File Missing:${dir} ${FNAME}-On Machine:${SERVER}&quot;
fi
done < ${FILELIST}

# Close file descriptor 3
exec 3<&-

[/tt]
That should do it. You need to make sure [tt]dir[/tt] and [tt]SERVER[/tt] are defined. They aren't defined in the snipped you listed here.

Hope this helps.

 
Or...
[tt]
#!/bin/ksh

DATE=`date`
SERVER=`uname -n`

FILELIST=/DJ/PUB/testfile
OUTFILE=/DJ/PUB/testout

while read -r FNAME
do
if [ ! -f ${FNAME} ]
then
print &quot;${DATE}-File Missing:${dir} ${FNAME}-On Machine:${SERVER}&quot;
fi
done < ${FILELIST} > ${OUTFILE}
[/tt]
...or...
[tt]
#!/bin/ksh

DATE=`date`
SERVER=`uname -n`

FILELIST=/DJ/PUB/testfile

while read -r FNAME
do
if [ ! -f ${FNAME} ]
then
print &quot;${DATE}-File Missing:${dir} ${FNAME}-On Machine:${SERVER}&quot; > testout
fi
done < ${FILELIST}
[/tt]
The problem with your first attempt is that the &quot;[tt]-u2[/tt] is telling [tt]print[/tt] that the output is going to [tt]stderr[/tt], BUT, your redirect (the &quot;[tt]>[/tt]&quot;) is only redirecting [tt]stdout[/tt] to the file [tt]testout[/tt]. So, there are several ways to correct it.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top