Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 循环后重置可变值_Bash_Shell - Fatal编程技术网

Bash 循环后重置可变值

Bash 循环后重置可变值,bash,shell,Bash,Shell,下面是一个简单的脚本,用于指定要创建的表的列表的文件名 tabnames.bash 文件包括: $ 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.c

下面是一个简单的脚本,用于指定要创建的表的列表的文件名

tabnames.bash 文件包括:

$ 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的值就会重置为它在进入循环之前保持的值

为什么?

这样做:

#!bin/bash
#
ls *.csv > /tmp/CSVfiles
ADDITIONALTABLES="FXRATES ANVIL"
while read staticFile
do 
   staticTable=`basename $staticFile`
   echo $staticTable
   ADDITIONALTABLES="$ADDITIONALTABLES ${staticTable%.csv}"
   echo "in $ADDITIONALTABLES"
done < /tmp/CSVfiles
echo "out $ADDITIONALTABLES"
#!bin/bash
#
ls*.csv>/tmp/CSVfiles
附加表=“FXRATES ANVIL”
读取静态文件时
做
staticTable=`basename$staticFile`
echo$staticTable
ADDITIONALTABLES=“$ADDITIONALTABLES${staticTable%.csv}”
echo“在$ADDITIONALTABLES中”
完成

正如其他人在评论中指出的,管道在子shell中运行。但是,如果您像上面那样使用重定向,它会工作。

因为管道在子shell中运行。实际上,在这种情况下,最好对/abc/static/rtce_reports/static/*.csv中的staticFile使用
;do
——这没有子shell问题,也避免了可能的问题。好主意。谢谢
#!bin/bash
#
ls *.csv > /tmp/CSVfiles
ADDITIONALTABLES="FXRATES ANVIL"
while read staticFile
do 
   staticTable=`basename $staticFile`
   echo $staticTable
   ADDITIONALTABLES="$ADDITIONALTABLES ${staticTable%.csv}"
   echo "in $ADDITIONALTABLES"
done < /tmp/CSVfiles
echo "out $ADDITIONALTABLES"