[英]Shell variable value substitution
Heres the description of my issue, I have a while loop that takes values from a file这是我的问题的描述,我有一个从文件中获取值的 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
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将值解析为 2 个变量 所以在回显 $schema 之后,我希望 MCLM 回显 $tabname 我希望 OPPP
But I will get empty string但我会得到空字符串
I'm using kornshell and I think its the older version我正在使用 kornshell,我认为它是旧版本
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:您可以像这样使用
read
更有效地编写循环,而无需使用像cut
这样的外部命令来提取每个字段:
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 _
. read
的第三个参数_
是为了安全——如果输入在一行上有多个点,所有额外的值都将被_
捕获。 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 :当你读入变量的值时尝试去掉双引号,并在 $table 变量中使用双引号,例如:
schema=$(echo "$table" | cut -d'.' -f1)
tabname=$(echo "$table" | cut -d'.' -f2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.