Use while loop - output variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use while loop - output variable
# 1  
Old 01-21-2014
Use while loop - output variable

I'm very much a newbie and hence why this is going to be a stupid question.

I'm attempting to create a korn shell script that pulls zone file locations and includes the copy command in the output. What?

getzonedir.ksh
Code:
#!/bin/ksh
while read -r domain
do ls */*"$domain" > $dir1
echo "cp $dir1 $dir1-$2"
done < $1

$1 = user1domains.txt
Code:
hosangit.com
djzah.com
ourholychurch.com

$2 = usr1bak

RUN: ./getzonedir.ksh user1domains.txt usr1bak

Expected output...
first step would be the domain zone files are found, so stepping through the first it would find primary.dir/db.hosangit.com (this I want to become a variable so I can echo the command needed to backup this zone file for example "cp primary.dir/db.hosangit.com primary.dir/db.hosangit.com-usr1bak

Am I going about this all wrong?
# 2  
Old 01-21-2014
so you just want to spit out the zone files from named in a loop?

Code:
#!/bin/ksh
for ZONE in `find /path/to/file(s) -type f`
 do
   echo ${ZONE}
 done

I keep my zone files in /etc/named/views:

Code:
$ ksh getzones.ksh
/etc/named/view/mydomain.internal
/etc/named/view/150.168.192.in-addr.arpa
/etc/named/view/mydomain.external


I know this doesn't answer your question exactly, but I think it should help you get to where you are going. There are other ways to achieve the same thing without using 'find' as well.

Last edited by Blackacid; 01-21-2014 at 02:53 PM.. Reason: update to this
# 3  
Old 01-21-2014
Thanks for the feedback...

I use ls */*$domain <--which is fed from the .txt file for the domains I'm interested in and it works great so far but I want to output the results (which would be the full path and zone file name) into a variable. I thought you could output the results using a > to variable name but that doesn't seem to work. If I could get the entire path and file name into a variable then I could either print the variable with the commands I want like
Code:
print "cp $fullpathandfilenamevar $fullpathandfilenamevar-bak"

or something like that.
# 4  
Old 01-21-2014
Why would you want to print a shell statement -- why not actually run the shell statement?

> does not redirect into variables, > redirects into files. You may have created a bunch of funny-named files doing this, you'd probably better check Smilie
# 5  
Old 01-21-2014
I'm guessing he is just trying to output what would be happening, before he turns it loose. Just guessing, I tend to do the same when trying something new.


Why wouldn't this work for you?

Code:
$ cat mydomains.txt
domain1.com
domain2.com
domain3.com


$ cat getzone.ksh
#!/bin/ksh
#
for domain in `cat ${1}`
 do
   zone=`find . -type f -name \*${domain}\* -print`
   echo "cp ${zone} ${zone}-${2}"
 done

$ ksh getzone.ksh mydomains.txt usr1bak
cp ./dir1/db.domain1.com ./dir1/db.domain1.com-usr1bak
cp ./dir1/db.domain2.com ./dir1/db.domain2.com-usr1bak
cp ./dir2/db.domain3.com ./dir2/db.domain3.com-usr1bak

# 6  
Old 01-21-2014
Thank You for the input. Its just like Blackacid said, I like seeing what should happen before I let it loose. Its my lack of confidence during my learning how to script.
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 do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Print for loop variable in output too

Hi, This is my input file cat input chr1:100-200 chr1:220-300 chr1:300-400 Now, I would like to run a program that will take each of the input record for i in `cat input`; do program $i | wc -l;done the output will be something like 10 20 30 But, I would like to print the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

5. UNIX for Dummies Questions & Answers

Need to get a loop variable from output of a command

Hi, I am trying to get a loop counter i and set its value as the ouput of a command: i=`printmo TEST1 | grep -i TEST2 | wc -l` Then I want to use i as counter to run a loop i number of times. Like if i gets a value of 5 I'll have to run loop 5 times. But will i here be a numeric... (3 Replies)
Discussion started by: pat_pramod
3 Replies

6. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

7. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

8. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

9. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

10. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies
Login or Register to Ask a Question