21

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
7
  • How do you call this script? Feb 5, 2015 at 15:20
  • possible duplicate of Ambigous redirect done >$2 Feb 5, 2015 at 15:21
  • @HaukeLaging, no it is not. The question seems to be about something close, but not the same Feb 5, 2015 at 15:31
  • @RomeoNinov Would you mind explaining the relevant difference? Feb 5, 2015 at 15:59
  • @HaukeLaging the script is saved in a .sh file. I just type ./myscript.sh Feb 5, 2015 at 16:03

3 Answers 3

25

1>> and 2>> 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 like

if [ -z "$1" -o -z "$2" ]; then
    echo "Log files for standard and error messages must be specified"
    echo "${0} msgfile errfile"
    exit 1
fi

at the start of the script.

0
2

In your case 1>> append the information from current stdout handler to the file ${jobout} 2>> append the information from current stderr handler to the file ${joberr}

jobout and joberr are files, defined as first and second parameter of script

1

As there are no parameters in calling the script the parameters $1 and $2 are empty and so are $jobout and $joberr.

You must call the script like this:

./myscript.sh file1 file2
2
  • Parameter numbers have nothing to do with the numbers before >> redirection operators.
    – Barmar
    Feb 11, 2015 at 19:29
  • Never mind, I see you're answering the part about the ambiguous redirect, not the question in the title. You should probably make your answer clearer about this.
    – Barmar
    Feb 11, 2015 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.