Find the answer to your Linux question:
Results 1 to 2 of 2
I've been learning bash scripting, and trying some basic stuffs. Just came across a situ where I have to execute a command, where attributes should be from each line of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! fydcode's Avatar
    Join Date
    Dec 2010
    Posts
    1

    Post using two variables i and j together in 'for' - bash


    I've been learning bash scripting, and trying some basic stuffs. Just came across a situ where I have to execute a command, where attributes should be from each line of 2 files. file1 and file2.

    I guess it can be done with for and while loops. Is there a way to use multiple variables in a single line like this (&& is not working here)

    for i in `cat file1` && j in `cat fil2` ; do command $i $j ; done

    I am trying to pick i and j simultaneously and execute a command using their values. Curious about how two variables can be used with a single for loop. Any help will be appreciated. Thanks!

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52

    Is this what you want?

    Code:
    exec 3< "$file1"
    exec 4< "$file2"
    
    while read a <&3; read b <&4
    do
      ## do something here with $a and $b, e.g.:
      printf "%-20s %s\n" "$a" "$b"
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •