Increasing the array index inside while loop
Hi
There must be a simple solution but I am finding it elusive, below is the code: Code:
declare -a arr; How do I do this? |
Hi!
It would be better if you kept this in the original thread for context: http://www.linuxquestions.org/questi...4/#post5560608 But, you are on the right track! First of all, this is not correct syntax: Code:
cc=$arr[1] Code:
cc=${arr[1]} Best regards, HMW |
You can also increase your counter inside the call to the array, ie. ++ will work inside []
I am also curious why the previous question was marked as solved but there does not appear to be any solution posted?? Or were you not going to share your new found knowledge? |
Quote:
Quote:
Code:
declare -a arr; |
Okay I made it work following what HMW suggested,
Code:
declare -a arr; |
Quote:
Here are a few suggestions: You don't need this variable at all since you don't use it: Code:
cc=${arr[1]}; Code:
while read -r line; do Best regards, HMW |
You could also do away with the counter by using the positional args, "$@", directly:
Code:
while read -r q |
Quote:
Quote:
Great tip. Thanks!! |
Quote:
they get an idea from your solution on how their own might be solved. Quote:
So the working option would have been: Code:
sed -i "s/${arr[count++]}/$q/g" test |
All times are GMT -5. The time now is 10:22 AM. |