有没有办法搜索参数内的整个数组?

时间:2011-04-05 23:20:26

标签: arrays shell unix

在下面发布我的代码,想知道我是否可以在一个数组中搜索匹配...或者如果我可以搜索参数内的unix文件。

#!/bin/bash

# store words in file
cat $1 | ispell -l > file
# move words in file into array
array=($(< file))
# remove temp file
rm file
# move already checked words into array
checked=($(< .spelled))

# print out words & ask for corrections
for ((i=0; i<${#array[@]}; i++ ))
do
if [[ ! ${array[i]} = ${checked[@]} ]]; then
    read -p "' ${array[i]} ' is mispelled. Press "Enter" to keep
this spelling, or type a correction here: " input
if [[ ! $input = "" ]]; then
correction[i]=$input
else
echo ${array[i]} >> .spelled
fi
fi
done

echo "MISPELLED:              CORRECTIONS:"
for ((i=0; i<${#correction[@]}; i++ ))
do
echo ${array[i]}                ${correction[i]}
done

否则,我需要编写一个for循环来检查每个数组indice,然后以某种方式做出决策声明是否要通过循环并打印/接受输入

1 个答案:

答案 0 :(得分:0)

使用shell的咒语是:

cat $1 | ispell -l |while read -r ln
do
  read -p "$ln is misspelled. Enter correction" corrected
  if [ ! x$corrected = x ] ; then
    ln=$corrected
  fi
  echo $ln
done >correctedwords.txt

while; do; done有点像函数,你可以将数据输入和输出。

P.S。我没有测试上面的代码,所以可能存在语法错误