I tried generating variables dynamically and assigning values to it but I encountered two issues with the code:
- I am not able to convert the value of "count" to numeric value so it can be used in for loop
- The values of variables are lost after for loop is completed.
Code
filename=$1
count= sed -n 1p $1 | tr ',' '\n' | wc -l
while read line
do
if [ $i -ne 1 ]; then
check_Keyword=`echo $line |grep -Eo '\b,Error\b'`
if [ "$check_Keyword" = ",Error" ]; then
if [ $post_counter -eq 0 ]; then
post_counter=$(expr $post_counter + 1)
else
write_values
fi
temp_text=""
for j in $(eval echo {1..count} )
do
Column[$j]=`echo $line | cut -d ',' -f$j`
done
temp_text=`echo $line | cut -d ',' -f3`
else
temp_text=$line
fi
msg_identifier=$msg_identifier$temp_text
fi
i=$(expr $i + 1)
done < $file_name
forloop, e.g.for ((j = 1; j < count; j++)); do .. done. Also just useif [[ $line =~ ,Error ]]; then ...instead of the awkwardcheck_Keyword=andif [ "$check_Keyword" = ",Error" ]. Avoid 'echo ... | cut ...`, bash provides parameter expansions to do just that without spawning multiple additional subshells.bash -n scriptnameand shellcheck.net to analyse your script before questions like this. There are many things that won't work here, and it would be lovely if the question was more along the lines of "why does this generate unexpected output" rather than "please fix and clean my code".