KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Consider the following script: #!/bin/bash num=0 cat file | while read line; do echo "$line" lines[$num]="$line" ((num++)) echo "num = $num" done echo "end num = $num" i=0 while [ $i -lt $num ]; do echo "${lines[$i]}" ((i++)) done Normally, it should read the file line by line, store the result in an array, then go through the array and print it line by line. The problem is that the variable $num resets somehow after the first loop exits. The output of this script for me is the following (using a file with some random garbage in it): dsfkljhhsdfsdfshdjkfgd num = 1 fdfgdfgdfg num = 2 dfgdfgdfgdfg num = 3 dfgdfgdfgdfgdfg num = 4 dfgdfgdfgdfgdfgd num = 5 fgdfgdfgdfg num = 6 dfgdfgdfgdfg num = 7 dfgdfgdfgdfgdfg num = 8 dfgdfgdfgdfg num = 9 dfgdfgdgdgdg num = 10 dfgdffgdgdgdg num = 11 end num = 0 Why is this? How do I achieve to remember the variable? I am using bash 3.1.17 on SUSE Linux 10.
Tags (comma-separated)
Save Edits
Cancel