for loop iteration and shell programming startup

question :how can i iterate to next item in for loop with the loop
e.g

for i in `cat abc.txt`
do
  echo $i // this will display first line
  i=$i+1;  // this doesnt work for me. 
  echo $i; //this will display secound line
done

question: is my approach to manipulate text good?

I have one output available as

12
123
1234
12345
123
1234

I want to manipulate it using awk or sed so output should look like

Item1 apple:
a=12
b=123
Item2 banana:
a=1234
b=12345
Item3 soda:

I will use three for loops to display the result

for i in `cat first list`
do
  echo $i
  for j in `cat 2ndlist.txt` 
    for k in `cat 3rdlist.txt`
      echo $j $k $k
    done
  done
done

question: i want to become very good in shell scripting , can anyone guide me to a series of tutorials which focus on examples?

please use CODE tags around your logic.

i=$((i+1))

or

let i=i+1
1 Like

Hi,

Last question:
you may start with 'Linux Hands on Guide', you can find it on tldp.org; it's not specifically about shell scripting, but has useful exercises at the end of each chapter that may be useful for you.

see ya
fra

---------- Post updated at 14:55 ---------- Previous update was at 14:55 ----------

Answer to first question:
$i gets the value of the first word (that is, set of contiguous, non space-interleaved characters) in the file.
in order to loop you don't have to modify $i. $i in the specific example is not a counter.

Second question: it' not very clear to me what are the starting conditions (what are the input data or files).

1 Like

If we are working with lines, we do not want to expand the entire file "abc.txt" on the "for" line and then read it word by word.
We need to read each line one-by one with a method which preserves the record construct.

cat abc.txt | while read line
do
          echo "${line}"          # Display line
done

To make the extra read in the while loop:

cat abc.txt | while read line
do
          echo "${line}"          # Display first line and every odd-numbered line
          read line                  # Read even numbered lines
          echo "${line}"           # Display 2nd line and every even-numbered line
done

Don't understand your second question. Not clear which file is which or what the process is.

1 Like