Searching for a member of an arrray Bash 3.2 with a @..

Creating the array.

while read line
	do
	    array+=("$line")
	done < "$temporary_file"	

This is the basic idea, but not working at the moment.
It just seems to be returning "No such file or directory"
for each array member

 for (( i = 0 ; i < "${#array[@]}"; i++ )); do
    	if grep "@" "${array[$i]}"; then
   		echo "${array[$i]}"
 		fi
    done

Check out this info: Bash: check if an array contains a value - Stack Overflow

Sorry, I am not good with bash functions that do not return void. How would I re-write those so they were inline?

See if this is what you were looking for:

$ cat test.sh
FA=("one" "two" "@"  "four" "@" "six")

for i in ${!FA[@]}
do
  if [ '@' = "${FA[$i]}" ]; then
    echo "Found @ at index $i"
  fi
done

$ test.sh
Found @ at index 2
Found @ at index 4

Here is inline:

A=("one" "two" "mail@yourhost.com")
B=("one" "two" "three")

for((i=0;i<${#A};i++)) {
   if [[ ${A} = *@* ]]
   then
       echo "A contains @"
   fi
}

for((i=0;i<${#B};i++)) {
   if [[ ${B} = *@* ]]
   then
       echo "B contains @"
   fi
}

And here is function version:

A=("one" "two" "mail@yourhost.com")
B=("one" "two" "three")

containsAT() {
    for((i=1;i<=$#;i++)) {
        [[ "${!i}" = *@* ]] && return 0
    }
    return 1
}

if containsAT "${A[@]}"
then
   echo "A contains @"
fi

if containsAT "${B[@]}"
then
   echo "B contains @"
fi
1 Like

@Spacebar
Not working still. Thanks for you input.

Taking this data

First Last
123 some road
some unit
citySTATEZIP
(111) 222-1111
(111) 222-1112
Fax:(111) 222-1113
user@domain.com
#:123456

stored in

"$temporary_file"

then

while read line
	do
	    array+=("$line")
	done < "$temporary_file"	

and for debugging I print...

    echo " array 0 = ${array[0]}"
    echo " array 1 = ${array[1]}"
    echo " array 2 = ${array[2]}"
    echo " array 3 = ${array[3]}"
    echo " array 4 = ${array[4]}"
    echo " array 5 = ${array[5]}"
    echo " array 6 = ${array[6]}"
    echo " array 7 = ${array[7]}"
    echo " array 8 = ${array[8]}"

and the array has populated successfully.

with this output..

 array 0 = First Last
 array 1 = 123 some road
 array 2 = some unit
 array 3 = citySTATEZIP
 array 4 = (111) 222-1111
 array 5 = (111) 222-1112
 array 6 = Fax:(111) 222-1113
 array 7 = user@domain.com
 array 8 = #:123456

The next problem is this data set (members 5-6) may or may not appear.
So algorithmically speaking and where I am getting stuck...

Next step:

  1. search array for email address (using the @ symbol here to keep things simple)

Then I am going to use conditional / control flow statements using the position of the email address to further parse the data.
Not sure if this helps, but wanted to add some context.

Thanks in advance, for any help.

The code that you have is looking for lines containing @ in the files named by lines in the file named by the expansion of the shell variable temporary_file . Presumably you're getting error messages because the lines in your file are not names of files to be searched.

If what you want to do is print lines in the file named by $temporary_file that contain an @ , that would much more simply be done by:

grep '@' "$temporary_file"

If you want a way to determine if the expansion of a shell variable ( $x in this example) contains an @ using only features that are available in all standards conforming shells, you could try something like:

if [ "${x#*@}" = "$x" ]
then    echo 'no @ found'
else    echo '@ found'
fi

This solved it.

	for((i=0;i<${#array};i++)) {
   if [[ ${array} = *@* ]]
   then
       echo "${array} contains @"
   fi
}

Thanks Chubler_XL