Set variables from file, then loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Set variables from file, then loop
# 1  
Old 07-28-2011
Set variables from file, then loop

Hello,

I am very, very new to shell scripting, but what I'm attempting to do is read in a list of user ID's to create on a database system from a CSV flat file, and for each entry run the "create user" script. I've gotten pretty far but I'm having trouble with the looping mechanism.... Any input would be greatly appreciated.

Code:
FILE=/path/to/the/infile
cat ${FILE} | sed 's/,/ /g' | while read ID
do
set${ID}

The rest of the code is a SQL script that uses ${ID} as the username. How do I wrap the SQL script into the loop so that it will execute the script for each entry in the file?

Things that work as expected are: passing the variable into the SQL script (was able to run some tests and get it working), and executing the SQL script from the .ksh against the target database system.

Thanks in advance.

*edit*
Edited to add, currently it sets the variable ${ID} as every item in the CSV file, without the commas and with a space inbetween, which of course causes a syntax error in the SQL.

Last edited by jkarren; 07-28-2011 at 04:27 PM..
# 2  
Old 07-28-2011
Can we see the entire script, please?
# 3  
Old 07-28-2011
Sorry. I thought it might be overkill. Script is below (edited out a few things like passwords).

Code:
FILE=/path/to/infile
cat ${FILE} | sed 's/,/ /g' | while read ID
do
set${ID}

bteq <<-EOF
.logon $LOGON_STRING_DEV;

CREATE USER ${ID} FROM PROD_JOB_USERS  AS
       PERM=0,                            
       PASSWORD = ********             
       ACCOUNT='********'            
       DEFAULT DATABASE=PROD_DB;       
GRANT  DROP DATABASE ON ${ID} TO ALL PROD_DBA;                        
GRANT  DROP USER ON ${ID} TO ALL PROD_DBA;                        
GRANT  ALL ON ${ID} TO DBC WITH GRANT OPTION;
GRANT BATCH_JOB_ROLE TO ${ID};
MODIFY USER ${ID} AS DEFAULT ROLE = ALL;             

.quit;
EOF
done

# 4  
Old 07-28-2011
Can you display a sample of your csv file?
# 5  
Old 07-28-2011
Sure, the entire contents of the CSV are below:

Code:
NB203A,NB203B,NB203C,NB203D,NB203E,NB203F,NB203G,NB203H,NB203I,NB203J,NB203K,NB203L,NB203M,NB203N,NB203O,NB203P,NB203Q,NB203R,NB203S,NB203T,NB203U,NB203V,NB203W,NB203X,NB203Y,NB203Z

# 6  
Old 07-28-2011
What does set${ID} do?

What you need to do is build up a file with the appropriate commands and then feed that to bteq afterwards.

Code:
CMD_FILE=/tmp/a

echo ".logon $LOGON_STRING_DEV;" > $CMD_FILE
cat ${FILE} | sed 's/,/ /g' | tr ' ' '\n' | while read ID
do

cat <<EOF >> $CMD_FILE
CREATE USER ${ID} FROM PROD_JOB_USERS  AS
       PERM=0,                            
       PASSWORD = ********             
       ACCOUNT='********'            
       DEFAULT DATABASE=PROD_DB;       
GRANT  DROP DATABASE ON ${ID} TO ALL PROD_DBA;                        
GRANT  DROP USER ON ${ID} TO ALL PROD_DBA;                        
GRANT  ALL ON ${ID} TO DBC WITH GRANT OPTION;
GRANT BATCH_JOB_ROLE TO ${ID};
MODIFY USER ${ID} AS DEFAULT ROLE = ALL;             
EOF

done

echo ".quit" >> $CMD_FILE

cat $CMD_FILE | bteq


Something like this. Notice how you need lines in order for the whileloop to work as you imagine, so I made each entry a new line instead of it being on the same line.
# 7  
Old 07-28-2011
You can loop this way:
Code:
for mEach in $(tr ',' '\n' < ${FILE}); do
  echo "Each <${mEach}>"
done

This User Gave Thanks to Shell_Life 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

HELP - loop a curl command with different variables from input file

Hi guys! Kind of new to bash scripting and now I'm stuck. I need to curl with these variables: "{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }" and my input_file.txt contains server001 10.10.10.01 serverpool1 80... (4 Replies)
Discussion started by: yort
4 Replies

2. Programming

[LUA] Set variables according to if file exists

Heya I'm using Awesome WM with the Vicious widget library. As i'm using multi boot, Win8, Fedora and Arch, i have my WM-Config shared accross my GNU/Linux installations. The regarding snippet: -- Functions -- Its just a workaround for an 'unstable' 'hwmon/hwmon' definition of Fedora21 -... (0 Replies)
Discussion started by: sea
0 Replies

3. Shell Programming and Scripting

How to set a variable name from another variables value?

Experts, I want to set value of variables like this in bash shell: i=5 ; L=100 I want variable d5 (that is d(i) ) to be assign the value of $L , d$i=$L ; echo $d5 Not working Thanks., (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

Need to SET Environment variables

Hi Could you please tell me how to set environment variables in Unix ksh. And how can acess those varibles in shell scripts ( Please give the code with an example) For my scenario. We have written number of shell scripts with hard coded username and password. But if we want to... (1 Reply)
Discussion started by: shyamu544
1 Replies

5. Shell Programming and Scripting

For loop using input file doesn't expand variables

Hi, I'm using a for loop reading from an input file that contains files, whose path includes a variable name. But the for loop doesn't expand the variable and therefore can't find the file. Here's an example: File BACKUPFILES /home/John/alpha /home/Sue/beta... (8 Replies)
Discussion started by: Hesiod
8 Replies

6. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Ksh script - Design ? - Search file and set variables

Hi - I'm trying to think of a clever way to write a shell script (trying to stay w/ ksh as that's what I know the best...) that will resolve the following problem: Problem - On a daily basis I have to email folks who are on-call to remind them. I was hoping to script this out so I could have... (9 Replies)
Discussion started by: littlefrog
9 Replies

10. UNIX for Dummies Questions & Answers

Using Variables to Set Other Variables

I have a script that I'm trying to shorten (below) by removing repetitive code. if ] then commodity_ndm_done=Y fi if ] then customer_ndm_done=Y fi if ] then department_ndm_done=Y fi if ] then division_ndm_done=Y fi (3 Replies)
Discussion started by: superdelic
3 Replies
Login or Register to Ask a Question