0

Heres the description of my issue, I have a while loop that takes values from a file

while read table
do
    schema="$(echo $table | cut -d'.' -f1)";
    tabname="$(echo $table | cut -d'.' -f2)";
    echo "$schema";
    echo "$tabname";
    echo $folder/$table_space"_auto_ddl"/$tabname"_AUTO_"$schema".sql.tmp"
    echo $folder/$table_space"_auto_ddl"/${tabname}"_AUTO_"${schema}.sql
    print $schema.$tabname;
done < $folder/tables_ddl_list.log

This is an example of one value

MCLM.OPPP

Parses the values into 2 variables So After echoing out $schema I would expect MCLM echoing out $tabnameI would expect OPPP

But I will get empty string

I'm using kornshell and I think its the older version

Stephen P
  • 14,422
  • 2
  • 43
  • 67
d0dulk0
  • 112
  • 2
  • 8

2 Answers2

2

You can write your loop more efficiently like this, using read, without the need for using an external command like cut for each field to be extracted:

while IFS=. read -r schema table _; do
    # your logic
done < "$folder/tables_ddl_list.log"

The third argument to read, _ is for safety - if the input has more than one dot on a line, all the extra values would be captured by _. Optionally, you could add error checking based on whether _ gets set or not.

Related:

codeforester
  • 39,467
  • 16
  • 112
  • 140
1

Try removing the double quotes when you read in the values of the variables, and use double quotes in the $table variable, eg :

schema=$(echo "$table" | cut -d'.' -f1)
tabname=$(echo "$table" | cut -d'.' -f2)
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • 2
    One can have *both* the outer and inner quotes -- there's no conflict between them, as a command substitution has its own quoting context. That said, putting the quotes around `"$table"` was indeed necessary. – Charles Duffy Mar 08 '19 at 15:53
  • @CharlesDuffy why is quoting causing the issue here? `.` has no special meaning to word splitting, right? `var=a.b; echo $var | cut -f1 -d.` would yield `a`, right? – codeforester Mar 08 '19 at 17:48
  • @codeforester, necessary for the general case; didn't mean to speak to any specific data. That said, if `IFS=.`, then we'd lose it in the `echo $var`. – Charles Duffy Mar 08 '19 at 17:57