Shell变量值替换

问题描述 投票:0回答:2

下面是我的问题的描述,我有一个while循环,从文件中获取值

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

这是一个值的示例

MCLM.OPPP

将值解析为2个变量所以在回显出$ schema之后我会期望MCLM回显$ tabname我会期待OPPP

但我会得到空字符串

我正在使用korn shell,我认为它是旧版本

shell ksh
2个回答
1
投票

读取变量值时尝试删除双引号,并在$ table变量中使用双引号,例如:

schema=$(echo "$table" | cut -d'.' -f1)
tabname=$(echo "$table" | cut -d'.' -f2)

1
投票

您可以使用read更有效地编写循环,而无需为每个要提取的字段使用cut等外部命令:

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

有关:

© www.soinside.com 2019 - 2024. All rights reserved.