i have this very simple script:
data=$(<data.txt)
counter=10
#just a dbquery using $data
result=(`psql -X -t -AF $'\t' -h $POSTGRES_HOST -d $POSTGRES_DATABASE -U $POSTGRES_USERNAME -w -c "select COUNT(field_value) from table where field_value in ($data)"`)
if [ $result == counter ]; then
echo -e "\e[92m Success \e[39m"
else
while read -r dataLine;
do
result=(`psql -X -t -AF $'\t' -h $POSTGRES_HOST -d $POSTGRES_DATABASE -U $POSTGRES_USERNAME -w -c "select field_value from fields where field_value = $dataLine" `)
if [ -z "$result" ]; then
echo "$dataLine failed"
fi
done < $data
fi
I am getting line 17: $data: ambiguous redirect
(where fi
is) i imagine the issue is something with the first line reading data.txt then later referencing $data on the read -r but im not exactly sure what is wrong.
Any ideas?
edit: error in line 17, not 21.
edit2: fixed "results" typo inside the loop.
edit3: data.txt
contains a list of UUIDs:
'5dce6dcc-5368-4dc2-b26e-01b92c3dd3aa',
'6dab9b13-1734-4766-93f5-a96d0e0afd38',
'c365e709-296b-4e8e-acf9-1d9e252325f6'
CodePudding user response:
The <
redirection takes a filename, but you are passing it string data.
To redirect from a string, use a <<<
here-string instead:
data=$(< data.txt)
cat <<< "$data"
Or if you are using sh
instead of bash
, use a short here-doc:
cat << end
$data
end
Though it would be better to just read directly from the file, since this allows you to stream arbitrary amounts of data without reading it into memory first:
data="data.txt"
cat < "$data"
While not applicable to this question, but for the benefit of future readers, you also get this error if you try to redirect from a file with spaces in the name:
$ file="my file.txt"
$ cat < $file
bash: $file: ambiguous redirect
In this case you simply have to quote the variable:
$ cat < "$file"
My file contents