【发布时间】:2018-10-11 23:25:32
【问题描述】:
这是一个简单的脚本,用于连接文件名以创建要创建的表列表。
tabnames.bash
#!/bin/bash
ADDITIONALTABLES="FXRATES ANVIL"
ls /abc/static/rtce_reports/static/*.csv | while read staticFile
do
staticTable=`basename $staticFile`
echo $staticTable
ADDITIONALTABLES=$ADDITIONALTABLES" "${staticTable%.csv}
echo $ADDITIONALTABLES
done
echo $ADDITIONALTABLES
文件是:
$ ls /abc/static/rtce_reports/static/*.csv
/abc/static/authority.csv
/abc/static/creditRating.csv
/abc/static/creditdept.csv
/abc/static/currency.csv
/abc/static/organiationType.csv
/abc/static/sector.csv
下面是输出:
$ ./tabnames.bash
authority.csv
FXRATES ANVIL authority
creditRating.csv
FXRATES ANVIL authority creditRating
creditdept.csv
FXRATES ANVIL authority creditRating creditdept
currency.csv
FXRATES ANVIL authority creditRating creditdept currency
organiationType.csv
FXRATES ANVIL authority creditRating creditdept currency organiationType
sector.csv
FXRATES ANVIL authority creditRating creditdept currency organiationType sector
FXRATES ANVIL
一旦退出循环,ADDITIONALTABLES 的值就会重置为进入循环之前的值。
为什么?
【问题讨论】:
-
因为管道在子shell中运行。
-
其实这种情况下最好使用
for staticFile in /abc/static/rtce_reports/static/*.csv; do——这样不存在subshell问题,也避免了可能出现的ambiguity inls's output`。 -
好主意。谢谢。