从“ for循环”创建变量,然后将字符串关联到
it.New值显示为null。
预期产量:
colname1 = hash(id)as id
colname2 = hash(cust)作为客户
实际输出:
colname1 =''
colname2 =''
#!/bin/bash
hcd1="hash(" # string to be added
hcd2=")as " #string to be added
dmk=id,cust
IFS=" ,"
i=0
for val in ${dmk}
do
i=$((++i))
eval var${i}="${val}"
done
for ((j=1;j<=i;++j))
do
name="var${j}"
colname="col${j}"
echo ${colname} = "$hcd1${!name}$hcd2 ${!name}"
done
echo "$colname1 $colname2 "
答案 0 :(得分:0)
一些注意事项:
进行了一些修正
#!/bin/bash
hcd1="hash(" # string to be added
hcd2=")as " #string to be added
dmk="id cust"
#IFS=" ,"
i=0
for val in $dmk
do
i=$((++i))
eval var$i="$val"
#echo var$i="$val"
done
for ((j=1;j<=i;++j))
do
eval name='$'"var$j"
colname="col$j"
echo $colname = "$hcd1$name$hcd2 $name"
done
输出:
col1 = hash(id)as id
col2 = hash(cust)as cust