Peter Sondag Peter Sondag - 2 months ago 14
Bash Question

bash ls output to file when ls finds no results

i'm relative new to scripting in bash, and made the following script:

# Set variables
logfile=/tmp/peter/logfile.log
indir=/mls/indir/IG/
tempdir=/mls/indir/IG/IGTEMP/
MLSready=/mls/indir/IG/MLSready

#Log current date/time
date >>$logfile

#Check whether processing files are present and one MLSready file exists. If not, exit the script.
if ls ${tempdir}R* 1> /dev/null 2>&1; then
mv $MLSready $tempdir
if [ $? -eq 0 ] ; then
echo MLSready move succeeded, copy processing files from $tempdir to $indir >>$logfile
ls -l ${tempdir}MLSready >>$logfile
find $tempdir -type f -name 'RPUO*' -mmin +1 -exec mv {} $indir \;
find $tempdir -type f -name 'RFRO*' -mmin +3 -exec mv {} $indir \;
else
echo MLSready does not exist, exit script >>$logfile
exit 1
fi
else
echo no processing files in $tempdir, exit script >>$logfile
exit 1
fi

echo set new MLSready file >>$logfile
# date >$MLSready
ls -l $MLSready 1>&2 >>$logfile

exit


At the end of the script, i don't make a new MLSready file.
When i execute the script, i expect to see the result of the latest 'ls -l' command in my logfile, however (because i didn't create a new MLSready file?) the output is redirected to my prompt instead of to my logfile:

DC3_igsy3ft3.sonic:/tmp/peter> ./move_bmp_files.sh
ls: 0653-341 The file /mls/indir/IG/MLSready does not exist.


How can i arrange that the result of the latest 'ls -l' command is always redirected to my logfile?

I also tried with:

ls -l $MLSready 2>&1 >>$logfile
ls -l $MLSready >>$logfile


Below my logfile:

do okt 6 09:22:39 2016
MLSready move succeeded, copy processing files from /mls/indir/IG/IGTEMP/ to /mls/indir/IG/
-rw-rw-rw- 1 oracle 505 0 okt 06 09:22 /mls/indir/IG/IGTEMP/MLSready
set new MLSready file


Thanks in advance!!

Answer

It's the order:

$ ls -l $MLSready >>$logfile 2>&1
$