【发布时间】:2011-10-02 16:13:28
【问题描述】:
小问题。
#!/bin/bash
if test -z "$1"
then
echo "No args!"
exit
fi
for newname in $(cat $1); do
echo $newname
done
我想用 array population 代码替换循环内的 echo。 然后,循环结束后,我想再次读取数组并回显内容。 谢谢。
【问题讨论】:
小问题。
#!/bin/bash
if test -z "$1"
then
echo "No args!"
exit
fi
for newname in $(cat $1); do
echo $newname
done
我想用 array population 代码替换循环内的 echo。 然后,循环结束后,我想再次读取数组并回显内容。 谢谢。
【问题讨论】:
#!/bin/bash
files=( )
for f in $(cat $1); do
files[${#files[*]}]=$f
done
for f in ${files[@]}; do
echo "file = $f"
done
【讨论】:
ls -1 而不是cat $1 完成了它,它正在工作......也许与bash版本有关!?
for f in "${files[@]}"
files+=($f)
如果文件,如您的代码所示,有一组文件,每个文件在一行中,您可以将值分配给数组,如下所示:
array=(`cat $1`)
之后,要处理每个元素,您可以执行以下操作:
for i in ${array[@]} ; do echo "file = $i" ; done
【讨论】:
declare -a files
while IFS= read -r
do
files+=("$REPLY") # Array append
done < "$1"
echo "${files[*]}" # Print entire array separated by spaces
【讨论】: