Redirect output of command line to for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect output of command line to for loop
# 1  
Old 01-08-2013
Redirect output of command line to for loop

I would like to redirect output of command line in for loop as $line.

Output should be processed as line but instead it throw whole output.

Could somebody help me on how to redirect output of command line and process it line by line without sending output to any file.

below is my code
Code:
result=`${HOME}/sqlplus << EOF
command
select * from xyz;
exit
EOF`
###############################
for line in ${result}
do
echo $line
echo ${result} | grep xxx

Now
Code:
${HOME}/sqlplus << EOF
command
select * from xyz;
exit

produce output like below .
Code:
xxx   yyyy 11111   22222
xxx   yyyy 33333   44444
aaa  bbbb  5555    66666
aaa  bbbb  7777    99999

# 2  
Old 01-08-2013
Code:
$ sqlplus -S <<! | while read; do printf 'line: %s\n' "$REPLY"; done
> / as sysdba
> select sysdate from dual;
> !
line:
line: SYSDATE
line: ---------
line: 08-JAN-13
line:

Or:
Code:
$ sqlplus -S <<! |
> / as sysdba
> select sysdate from dual;
> !
> while read; do
>   printf 'line: %s\n' "$REPLY"
> done
line:
line: SYSDATE
line: ---------
line: 08-JAN-13
line:

In your case it would be something like this:
Code:
"$HOME"/sqlplus << EOF | 
command
select * from xyz;
exit
EOF 
while IFS= read -r; do
  printf '%s\n' "$REPLY" | grep whatever
done

P.S. Drop the -r option of read if your shell doesn't support it.

Last edited by radoulov; 01-08-2013 at 11:12 AM..
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How To Redirect The Output When We Are Usinf EOF In The Same Line?

need to login 4 systems to check whether the systems are accessible or not using rlogin command,after each login i will exit.After issuing the command rlogin abc if it is not responding i will get a message of "connection timed out" . So i want to automate this task .so i tried like below: EX: ... (3 Replies)
Discussion started by: kankuro9x
3 Replies

2. Shell Programming and Scripting

How to redirect the output when we are usinf EOF in the same line?

need to login 4 systems to check whether the systems are accessible or not using rlogin command,after each login i will exit.After issuing the command rlogin abc if it is not responding i will get a message of "connection timed out" . So i want to automate this task .so i tried like below : EX:... (1 Reply)
Discussion started by: charanarjun
1 Replies

3. Shell Programming and Scripting

How to redirect output of a command to a variable during ftp mode?

Hi, How can I redirect the output of a following command to a variable. ftp myservername ftp> user (username) guptaji 331 Password required for guptaji. Password: ftp> size /home/salil/abc.dat ftp> a='size /home/salil/abc.dat' ?Invalid command I want to redirect value of size to... (1 Reply)
Discussion started by: Salil Gupta
1 Replies

4. AIX

Not able to redirect output of command

Hi All,. We are using AIX as the OS to host the Oracle ERP. We have a command FNDLOAD which is used to load setups. When this command is run, it outputs names of log files and any errors to the screen. I am trying to redirect this output to a file because we have large number of these... (4 Replies)
Discussion started by: mansmaan
4 Replies

5. Shell Programming and Scripting

Redirect output directly with DOS end of line

Hi guys, I have some echo scripts and awk scripts like these: echo "some text" > output1 . . awk '{....}{print}' input > output2Both output1 and output2 are saved with unix END Of Line, then, is there an option to include within echo command and awk command to save the output as DOS END Of... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

6. Solaris

Script redirect command output failed, why?

Hi, I put a for loop in a script to eject backup tapes from the robot. The command echo' output goes to the log file without problem, but command vmchange's output does not go to the log file although it's working fine. It still displays on the screen. I've tried '2>&1 1>$log', but nothing changed.... (5 Replies)
Discussion started by: aixlover
5 Replies

7. Shell Programming and Scripting

How to redirect the output of a cvs command to a file as well as the console.

Hi can anyone tell me how to redirect the ouput of a cvs command to a file as well as the console? i tried using cvs add <filename> | tee logFile cvs add <filename> 2>logFile 2>&1 All i could get is only on console or on file. Please help Thanks (2 Replies)
Discussion started by: ankitag2010
2 Replies

8. UNIX and Linux Applications

How to redirect grep command output to same file

Hi Everyone, Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command. $grep pattern file1 > file1 Kind Regards, Eswar (5 Replies)
Discussion started by: picheswa
5 Replies

9. Shell Programming and Scripting

redirect output of dos2unix command

hi I want to suppress the output of dos2unix command in my shell script. I'm using follwing command in my script dos2unix somefile >/dev/null But it's still showing output while executing the script.Please help me to sort this out Thanks (4 Replies)
Discussion started by: nrbhole
4 Replies

10. UNIX for Dummies Questions & Answers

redirect command output to variable

Hi, I am looking for a way to redirect the result from a command into a variable. This is the scenario. Using the find command I will be getting multiple records/lines back. Here is the command I am using: find /”path”/ -name nohup.out -print This now is giving me the paths and file... (1 Reply)
Discussion started by: hugow
1 Replies
Login or Register to Ask a Question