Multiple FOR;Singe DO statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple FOR;Singe DO statement
# 1  
Old 09-15-2015
Multiple FOR;Singe DO statement

Hi guys,

I'm trying to send variable commands from 2 different files but I'm not figuring out how to put 2 "i" variables into the command, can anyone help?

This is what is done when I read only 1 file:
Code:
for i in $(file1.txt);do echo "$i "

I'm trying to echo 2 variables, matching the lines from each of them. Both txt files have the same number of lines.

Code:
for i in $(file1.txt);for e in $(file2.txt);do echo "$i and $e"

Thanks!
# 2  
Old 09-15-2015
for doesn't read lines. You don't want to use it that way.

read can be used to read lines reliably.

More than one stream makes it more complicated than reading from stdin, but you can open both files, read from them separately each loop, and close afterwards.
Try

Code:
# Open these two files into file descriptors 5 and 6
# 0 is stdin, 1 is stdout, 2 is stderr, higher numbers generally kept free for us
exec 5<file1
exec 6<file2

while IFS="" read -r LINE1 <&5 && IFS="" read -r LINE2 <&6
do
        echo "Line 1 is $LINE1 and line2 is $LINE2"
done

# Close files
exec 5<&-
exec 6<&-

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-15-2015
You can also make the redirection local to the while loop :

Code:
while IFS= read -r line1 && IFS= read -r line2<&3
do 
  printf "%s\n" "\$line1: $line1" "\$line2: $line2" 
done <file1 3<file2

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to apply the update statement in multiple servers on multiple dbs at a time .?

Hi , Can any please help the below requirement on all multiple servers and multiple dbs. update configuration set value='yes' ;1) the above statement apply on 31 Databases at a time on different Ip address eg : 10.104.1.12 (unix ip address ) the above ip box contains 4 db's eg : db... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

4. Shell Programming and Scripting

How to use logical operators in multiple if statement

Hi I want to send a status mail if daily or weekly or monthly batch completed or aborted. Here is the code. if && && || Else if && && || Else if && && || then mailx –s “Status Report” sumone@sumthing.com else print ”try again” Plz suggest the changes. (3 Replies)
Discussion started by: Avi
3 Replies

5. Shell Programming and Scripting

Multiple not statement in AWK

Hi I have a logfile that do grow large rapid, so I like to create a new one with needed info. For some reason the tail -d do not like to redirects its output to a file. tail -f /var/log/daemon.log | grep -vE '(snmpd|ntpd)' gives me real time log of all lines without "snmpd" and "ntpd" This... (4 Replies)
Discussion started by: Jotne
4 Replies

6. Shell Programming and Scripting

multiple if statement

#! /bin/csh set umr=UMR foreach i ( `ls`) set file_nm=$i set bh_nm=`echo $file_nm | cut -d"_" -f2` if($bh_nm !=$umr) then { set bh_ext=`echo $file_nm | cut -d"_" -f4` set bh_num_nm="$bh_nm $bh_ext a .txt" mv $file_nm $bh_num_nm } ... (3 Replies)
Discussion started by: jdsignature88
3 Replies

7. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

8. UNIX for Dummies Questions & Answers

Multiple Condition If statement

Hi, I would like to create an IF statement where if a variable is equal to at least one of 2 (or more) values then the script proceeds. For example: TEST_VAR=2 if ; then echo success! else echo failure fi I understand that the above syntax is wrong but I feel it must be close. Any... (1 Reply)
Discussion started by: msb65
1 Replies

9. What is on Your Mind?

When Unix will have a singe version, platform independent OS

I wonder when UNIX will be a OS, will have a single version. (1 Reply)
Discussion started by: abhishek0216
1 Replies

10. UNIX for Dummies Questions & Answers

multiple Logical statement

hi I have following if condition line_by_line="0000000000000tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" if then echo "Exclusion criteria" else echo "Not exclusion criteria" fi above condition works perfectley but if i add one more logical condition... (3 Replies)
Discussion started by: mahabunta
3 Replies
Login or Register to Ask a Question