the code is:
echo -n "Enter the number you want to search for"
read number1
for i in `seq 1 $N`
do
temp= $number1
if [ temp -eq ${array[$index]} ]
then
temp= $index
fi
done
echo " The position is" $temp
i donot get an output for this
just the position is and no number.
What am i doing wrong?
There are a few big problems here.
Whitespace in assignment. One problem is that these lines:
need to be like this instead:
This is because in Bash, something like this:
runs the command
commandwith the environment variablevarnameset tovalue. In your case, this:tries to run the command
$number1with the environment variabletempset to the empty string.Reuse of temporary variable. Another problem is that this line:
is run on every pass through the loop; so even if
temphad previously been set to an appropriate array index, the above will discard that value and replace it with the number that the user had entered. Really, you should just remove this line, and use$number1directly when you need it.Variable-name mismatch. Another problem is that this line:
uses
ifor the loop variable, but these lines:use
index. Needless to say, these need to match.Unexpanded variable. This line:
was surely meant to be this:
(expanding the variable
$tempinstead of using the string'temp'); but in light of the above, it should now be:Array indices. Array indices start at zero; so if N is the number of elements in the array, then you need to iterate from 0 to N-1. So, this:
needs to be this:
Though I actually think you should get rid of
Nentirely, and use${#array[@]}(which is a Bash notation meaning “the number of elements inarray) instead: