Matam Matam - 6 months ago 7
Bash Question

Trying to read an output from another shell failed

I have this code:

while IFS= read -r line; do
arr+=("$line")
done < <(isValidParameters < "$1")

printf "%s\n" "${arr[@]}"


And when I run this, I get no output at all.
What I run is:
./getApartments dos > output.txt


NOTE: dos is not a real file in my directory, those
isValidParameters
should print:
File is missing
.

Here is
isValidParameters
:

#!/bin/bash
PATH=$PATH:.

if(($# != 1 && $# != 3)); then
echo "Illegal or missing parameters"
exit 1
fi

if [[ !(-f $1) ]]; then echo "File is missing";
exit 1
fi

#find . $1 \*.flat -print

function search_file() {
#echo "$1"

for line in "$1"/*; do
if [[ $line == *.flat && -f "$line" ]]; then
echo "$line"
fi

if [[ $line == *.flat && -d "$line" ]]; then
search_file "$line"
fi
done
}

while read line; do
if [[ $line == *.flat && -f "$line" ]]; then
echo "$line"
fi

if [[ $line == *.flat && -d "$line" ]]; then
search_file "$line"
fi
done < "$1"

Answer

isValidParameters expects to get the filename as argument $1, but you're using shell input redirection instead. Since the file doesn't exist, the shell reports the error "dos: No such file or directory" and doesn't run isvalidParameters at all.

Change that line to:

done < <(isValidParameters "$1")

I think you can simplify that loop to:

IFS=$'\n' arr=($(isValidParameters "$1"))