I have the following bash script, from what I understand >> is used to append the output of a command to an existing file instead of overwrite, but what is it doing in this case? This script is calling some exe files to convert from one format to another. There are many years for each file, so it loops through each file by looking at the filename. Also when I run this script I get "ambiguous redirect"
#!/bin/bash
source $HOME/.bashrc
jobout=${1}
joberr=${2}
# Set some paths and prefixes
yr_bgn=2000
yr_end=2000
yr=${yr_bgn}
pth_data='/mnt/'
pth_rst='/mnt/'
while [ ${yr} -le ${yr_end} ]
do
./executable1 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
./executable2 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
./executable3 ${pth_data}file${yr}-${yr}.nc ${yr} ${pth_rst} 1>> ${jobout} 2>> ${joberr}
let yr=${yr}+1
done
Best Answer
1>>
and2>>
are redirections for specific file-descriptors, in this case the standard output (file descriptor 1) and standard error (file descriptor 2).So the script is redirecting all "standard" messages to
${jobout}
and all error messages to${joberr}
. Using>>
in both cases means all messages are appended to the respective files.Note that
${jobout}
and${joberr}
take their values from the two command-line parameters to the script (${1}
and${2}
), so you need to specify the files you want to use to store the messages. If the parameters aren't given the script will produce the "ambiguous redirection" error message you've seen; the script should really check whether the parameters have been provided and produce an appropriate error message otherwise, something likeat the start of the script.