Switching from a single variable to multiple


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switching from a single variable to multiple
# 1  
Old 08-16-2012
Switching from a single variable to multiple

I apologize in advance if someone has answered this question before, but I am not sure even what to search for here. I have this script for backing up DB2 databases and it works fantastically if there is only on database in the config. My problem is that I have a business intelligence server that has multiple DB's in the directory and the script just falls over. This is the code that performs the work:

Code:
while read inst ; do
        db=`su - $inst "-c db2 list db directory" | grep -p Indirect | grep -v HA | sed -n 's!Database alias *= \([A-Z]*\)!\1!p'`
        role=`su - $inst "-c db2 get db cfg for $db" | sed -n 's!.*= \(STANDBY\)!\1!p;s!.*= \(PRIMARY\)!\1!p;s!.*= \(STANDARD\)!\1!p;s!^\(SQL30082N\).*!REMOTE!p'`
        if [ "$role" == STANDARD ] || [ "$role" == PRIMARY ] ; then
                su - $inst "-c db2 backup database $db online use tsm" >> $result

My question is, how do I get this to do each DB in turn when going through the backup command.

I believe something like for
Code:
i in $db; do  su - $inst "-c db2 backup database $i online use tsm" >> $result

But it appears it is not this simple. Smilie

Any help will be appreciated.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.
# 2  
Old 08-16-2012
You haven't explained what anything in your loop does which makes it hard to reverse-engineer.

But I can tell you how a for-loop works:

Code:
db="a b c d e"
for X in $db
do
        echo $X
done

I think the -c needs to be out of the string, there.
Code:
su - user -c "whatever"

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-16-2012
Okay:
The first line:
Code:
db=`su - $inst "-c db2 list db directory" | grep -p Indirect | grep -v HA | sed -n 's!Database alias *= \([A-Z]*\)!\1!p'`

This lists all the databases and then greps and removes all extraneous information leaving me with only the database alias so instead of:
Quote:
Database 12 entry:

Database alias = Alias_Name
Database name = Alias_Name
Local database directory = /db2/some/directory
Database release level =
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
I get just the Alias_Name which is something I can work with.

The next line:
Code:
role=`su - $inst "-c db2 get db cfg for $db" | sed -n 's!.*= \(STANDBY\)!\1!p;s!.*= \(PRIMARY\)!\1!p;s!.*= \(STANDARD\)!\1!p;s!^\(SQL30082N\).*!REMOTE!p'`
if [ "$role" == STANDARD ] || [ "$role" == PRIMARY ] ; then

Lets the script know if the database is in a state that can be backed up.

After all that, the following line does the actual work:
Code:
su - $inst "-c db2 backup database $db online use tsm" >> $result

with $db being the listed database aliases.

Hope that helps.

---------- Post updated at 01:37 PM ---------- Previous update was at 11:04 AM ----------

Through quizzing my co-workers and a lot of testing, this is the solution we came up with:
Code:
while read inst ; do
        dbb=`su - $inst "-c db2 list db directory" | grep -p Indirect | grep -v HA | sed -n 's!Database alias *= \([A-Z]*\)!\1!p' | sed -e 's/^ //g'`;
        for db in `echo $dbb`;
                do
                        role=`su - $inst "-c db2 get db cfg for $d | sed -n 's!.*= \(STANDBY\)!\1!p;s!.*= \(PRIMARY\)!\1!p;s!.*= \(STANDARD\)!\1!p;s!^\(SQL30082N\).*!REMOTE!p'`;
                        if [ "$role" == STANDARD ] || [ "$role" == PRIMARY ];
                                then
                                        su - $inst "-c db2 backup database $db online use tsm" >> $result

Thanks!

Last edited by Clay Lovett; 08-16-2012 at 12:08 PM.. Reason: Sentences out of order and made no sense.
# 4  
Old 08-16-2012
You've got some useless use of backticks in there. All you actually need is for db in $dbb, like I showed you above.
This User Gave Thanks to Corona688 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

User switching without carrying over LC_CTYPE env variable

I am using Solaris8, userA's shell '/usr/ace/prog/sdshell', AppuserB's shell '/bin/ksh'. serverT:/home/userA>LC_CTYPE=iso_8859_1; export LC_CTYPE; vtemp='userA variable'; export vtemp serverT:/home/userA>echo "LC_CTYPE=$LC_CTYPE, vtemp=$vtemp"; LC_CTYPE=iso_8859_1, vtemp=userA... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

3. Shell Programming and Scripting

Using multiple values for single variable in a loop

Hello Guys, I have a small loop problem as below. I have 3 different values to be used while running the same script - va1="some-value1" va2="some-value2" va3="some-value3" Now I want to use these three variable values to be used for running the same command, like - while... (1 Reply)
Discussion started by: rockf1bull
1 Replies

4. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

5. IP Networking

Multiple wan connection on single PC

Hi All Is it possible to be connected to a corporate lan/wan through the on-baord nic, and also be connected to mobile wan through a tethered smartphone? Ideally I'd like to be able to use my own smartphone using a browser like chrome (private browsing) while still being able to use the... (3 Replies)
Discussion started by: huskie69
3 Replies

6. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

7. Shell Programming and Scripting

Multiple lines into a single line

Hi, I've some files with the following data and i need to convert the lines between the separator ---, into a single line. I've tried with the paste cmd but my main problem is that the number of lines between the separator is not fix, it can very between 1-4 lines. Input --- 2010-02-22... (4 Replies)
Discussion started by: RickyC9999
4 Replies

8. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

9. Shell Programming and Scripting

Reading multiple lines as single

Hi, Is it possible to read every 2 lines as single record, I have a file like below, ~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK Dec 1 2005 12:00AM~ 10.840000~ ~DKK ~DAENISCHE KRONE ~DAENEMARK Dec 2 2005 12:00AM~ ... (9 Replies)
Discussion started by: braindrain
9 Replies

10. UNIX for Dummies Questions & Answers

Switching to single-user mode

Hello everyone, I need to make a OS full backup. I am using the vdump command but first, I must to switch to the single-user mode. I am working on a Compaq Tru64 Unix V4.0G. Please, could somebody tell me which is/are the commands to do it? I appreciate your help Gastón (1 Reply)
Discussion started by: gmoyano
1 Replies
Login or Register to Ask a Question