简体   繁体   中英

Shell variable value substitution

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

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM