我是shell脚本的新手,但我尝试了以下内容:
#!/bin/sh
declare -a s
read string1
s[0] = string1
while ((a<=2))
do
read string2
for i in "${s[@]}"
do
if $i=string2
then
a=3
exit 1
fi
done
s[@+1]=string2
done
它不起作用,我甚至不确定我是否需要使用数组。任何帮助都会很棒。
答案 0 :(得分:2)
只是一些错误。更正如下
declare -a s
read -p "enter a value: " string1
s[0]=$string1 # must not have spaces around `=`
# must use `$` to get the *value* of a variable
while true # infinite loop
do
read -p "enter a value: " string2
for i in "${s[@]}"
do
if [[ "$i" = "$string2" ]] # how to test string equality
then
break # break the loop
fi
done
s+=("$string2") # append a value to the array
done