$1”: ambiguous redirect

New to the site, please let me know I'm not meeting the post guidelines.

I'm creating a bash script to generate a report with output from a grep command. The goal is to direct
the output to a different log file by using a 'logger file'. But I get this error during the run: $1�: ambiguous redirect.
I'd appreciate if someone can pointout what I'm missing. Thanks.

#!/bin/bash

#setup logging # applogger.sh
LOGFILE="/users/ronaldbland/desktop/ctestlog.log"

exec 2>>${LOGFILE}

# override this in sourcing script
LOGENTITY="default"

function log() {
	echo �$1�
	echo �[$LOGENTITY] $(date "+%Y.%m.%d %H:%M:%S") -> $1� >> $LOGFILE 2>&1
}
#!/bin/bash

#Script Name: Ctest.sh
#Main Script
# Author:Ron
# July 2019

#setup logging 

source /users/ronaldbland/desktop/applog.sh
LOGENTITY="ctest.sh"

# Serial Number
SN=`system_profiler SPHardwareDataType | grep "Serial Number"`
log "Serial Number (system): $SN"
echo "[$LOGENTITY] $(date "+%Y.%m.%d %H:%M:%S") -> $1" >> $LOGFILE
1 Like

That's because you are using the unicode U+201C / U+201D (Left / Right Double Quotation Mark) in lieu of the required ascii " (0x22, \o042). The "greater than" sign > thus is unquoted, and interpreted as a redirect operator by the shell.

Is it OK to presume you created the script with a word processor? Try a simple text editor instead.

1 Like

Thanks guys for the assist. I'v e banging my head on the wall.
Once I updated the "" format to ascii the script worked as needed. Also I noticed after this fix the script works with or without this ... 2>&1. But I will definitely keep this in mind as go onto new tasks.

A further problem is the double use of $LOGFILE, as rdrtx1 already mentioned.

exec 2>>${LOGFILE}
echo "[$LOGENTITY] $(date "+%Y.%m.%d %H:%M:%S") -> $1" >> $LOGFILE

The second write opens an already opened file. It might get lost when the writing to the stream continues.
Correct is to write to the already opened stream.

exec 2>>${LOGFILE}
echo "[$LOGENTITY] $(date "+%Y.%m.%d %H:%M:%S") -> $1" 1>&2
2 Likes

I didn't think of that. Thanks for bringing that up. I will be writing several other variables to the stream.