Running in a loop in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running in a loop in UNIX
# 1  
Old 03-04-2018
Running in a loop in UNIX

I have a table in oracle which has 2 columns
Code:
table_name table_name1
U1 T1
U2 T2

I have to take these as a variable in unix and then go to /home/bin and execute a unix command using these variables.
Considering that there is only one row in the table I was able to write the below but need help how to run for multiple rows in a loop

Code:
#usr/bin/kah
t='sqplus-s test/test<<EOF
set pagesize 0 feedback off verify off heading off echo off;
select table_name from table;
exit;
EOF'
echo $table_name

l='sqlplus test/test<<EOF
set pagesize 0 feedback off verify off heading off echo off;
select table_name1 from table;
exit;
EOF'

echo $table $table_namel

cd home/bin
run $table.sql $table $table_name1


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by Ashcalin; 03-04-2018 at 04:21 PM..
# 2  
Old 03-06-2018
Code:
#! /usr/bin/zsh

query="set pagesize 0 feedback off verify off heading off echo off;
select table_name, table_name1 from table;"

while read line
do
    table_name=$(echo $line | awk '{print $1}')
    table_name1=$(echo $line | awk '{print $2}')
    echo $table_name, $table_name1
done <<< $(echo $query | sqlplus -S test/test)

You may try to optimise further.
# 3  
Old 03-06-2018
There is some confusion over the variable names, for instance sometimes you use a l and sometimes a1 I have assumed you want a number one rather tha a lower case L

For ksh, you might need something like:-
Code:
#!/bin/ksh
# Whatever else you need first
:
:
:
sqlplus -s <<EOF > /path/to/some/file       # Capture all the output into a file
   test/test
   set pagesize 0 feedback off verify off heading off echo off;
   select table_name, table_name1 from table;
   exit;
EOF

while read table table_name1
do
   echo "My \$table is \"$table\""
   echo "My \$table_name1 is \"$table_name1\""
done < /path/to/some/file

Does this get you closer? What I'm suggesting is to get the output from the whole table and the parse it, line at a time, into the two variables. It's been a while since I had a true ksh hence the use of a temporary file. That may cost a lot of time and space depending on your table size. If it is bigger, it would be better to pipe it into the while read statement but there are differences between ksh and bash (which is what I have) in this respect. I cant comment for zsh and I don't know if that is available to you anyway.

What OS and version are you running? It might give us other tools to do this more neatly.



I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop is running only for the first iteration

Hello, I've written a script to automate encoding of all the MP4 files in a directory (incl. subdirectories). But unfortunately it's running for the first MP4 file only. My machine details: root@Ubuntu16:~# uname -a Linux Ubuntu16 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48... (2 Replies)
Discussion started by: prvnrk
2 Replies

2. Shell Programming and Scripting

While Loop not running

echo "The recreate was successfully completed for device file $LCL_CLN_FILE." >> $LOG_FILE remaining=$(symclone -sid $LCL_SID -f $ETC/$LCL_CLN_FILE query | grep MB | awk '{print $2}') if then while do echo "$remaining MB's to be copied........." >> $LOG_FILE ... (10 Replies)
Discussion started by: rajsan
10 Replies

3. Shell Programming and Scripting

While loop running after reaching limit

Hi frnds i am newbie in linux and trying to write a simple script for adding users.the problem is i am running a while loop under function and loop is running for 3 time more than limit.Here is my Script and output pls help me out : # CREATE N NO. OF USERS WITH PASSWORD IN SYSTEM #... (4 Replies)
Discussion started by: Vaibhav.T
4 Replies

4. Shell Programming and Scripting

Help Running Loop Script

Hi I have a file called lastlogin_1 that contains a list of usernames. I would like to put together a quick loop script that will use a variable (the contents of lastlogin_1 file) and grep all directories and sub directories in the current directory (/in/the/current/directory) for each username... (3 Replies)
Discussion started by: jamba1
3 Replies

5. Shell Programming and Scripting

Running for loop on remote server

Hi, I'm having a problem performing for loop on remote server, i know this can be done with one liner but i'm not sure how it works if using logical operator such as for ifs and case or while for server in sterverA serverB serverC ; do ssh -v $server "cd ~/MyDocuments/; bag=`find... (6 Replies)
Discussion started by: sexyTrojan
6 Replies

6. Shell Programming and Scripting

For loop and running 2 commands at once?

HI. I am trying to run 2 commands, using the "for x in a b c d" loop but i am having a hard time coding it... Here is what i have so far: for SERVER in SERVERA SERVERB SERVERC SERVERD SERVERE do ############################################################################### #... (5 Replies)
Discussion started by: Stephan
5 Replies

7. Shell Programming and Scripting

loop when process running

Hi Gurus, Could you please help me to create a shell script that will be started by a cron job once every night at 24.00 h (that should bee easy:)) The shell script should control every 30 seconds the name of a process, and when the process doesn't run anymore it should execute a few further... (12 Replies)
Discussion started by: blackwire
12 Replies

8. Shell Programming and Scripting

running a batch file in loop

i have a batch file which should run every five min...for an hour can any one help me out (1 Reply)
Discussion started by: aemunathan
1 Replies

9. Shell Programming and Scripting

running command in while loop

I have a script that users a while read, whithin this I want to execute the cp -i command on some of the results, however because it is in a loop it skips the -i part of cp. How do I get the loop to stop each time cp -i is run and wait for the user to type the response. (2 Replies)
Discussion started by: gefa
2 Replies

10. Shell Programming and Scripting

Nested loop not running using cronjob

Hi I have created a shell script which uses three nested loops. When the cronjob is running the script there is a for loop which call a function which has two nested for loops. It is going into the function but the for loop in the function is not running. Can someone pleasae give me a solution... (9 Replies)
Discussion started by: bihani4u
9 Replies
Login or Register to Ask a Question