Convert shell script for looping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert shell script for looping
# 1  
Old 06-06-2007
Convert shell script for looping

Situation: I have a working shell script on our file server (OSXS Tiger) to connect to a workstation, which is using a portable home directory (phd), and rsync a user's MirrorAgent.log. I'm not that strong of a scripter (obviously), but I would like to add other workstations to this script as they are converted to phd's so they can be checked to see if the phd is working as it should (another script down the line). I reason that I need to convert this script to loop through the workstation names. I'm looking for any helpful suggestions for how to do this:
Code:
#!/bin/bash 
# 
# purpose: to use rsync to synchronize the MirrorAgent.logs of PHD users 
# 
# create variables 
RSYNC="/usr/bin/rsync" 
ROpts="-az -e ssh" 
ADMIN="alatorre@cshs.org" 
SRC="cardenasv@awm2212a.csmc.edu:~/Library/Logs/MirrorAgent.log" 
DST="/Volumes/Data2/MA_logs/cardenasv/" 
theLog="/Library/Logs/mySync.log" 
# 
# sync MirrorAgent.log 
time $RSYNC $ROpts $SRC $DST >> $theLog 
# test to see if sync completed successfully 
if [ ! $? = 0 ]; then 
        echo "MA_log SYNC NOT COMPLETED!!!" >> $theLog 
        echo "MirrorAgent.log not synced!" | mail -s "MA log sync problem" $ADMIN 
fi 
exit 0 
# end script

Thanks in advance.
# 2  
Old 06-06-2007
This should help...
Code:
#!/bin/sh
#

inputstuff=`ls -1 /Users/`

for names in $inputstuff
do
	if [ $names = "Shared" ]; then
		continue
	else
		echo "put your stuff here"
		echo
	fi
	
done

# 3  
Old 06-07-2007
Yes, it does. This is exactly what I was looking for. Thanks, much.
# 4  
Old 06-07-2007
Done

With the help provided by the kind forum member, I was able to convert the script for looping:
Code:
#!/bin/bash
#
# purpose: to use rsync to synchronize the MirrorAgent.logs of PHD users
#
# create variables
PHDUSERS=`cat /Library/Scripts/shell/phd_users.txt`
RSYNC="/usr/bin/rsync"
ROpts="-az -e ssh"
ADMIN="alatorre@cshs.org"
#SRC="cardenasv@awm2212a.csmc.edu:~/Library/Logs/MirrorAgent.log"
theLog="/Library/Logs/mySync.log"
#
# sync MirrorAgent.logs
echo "*** MirrorAgent Log Sync ***" >> $theLog
echo "===> sync'd the following user MirrorAgent logs:" >> $theLog
for names in $PHDUSERS
do
        theUser=`echo $names | awk -F@ '{print $1}'`
        SRC="${names}:~/Library/Logs/MirrorAgent.log"
        DST="/Volumes/Data2/MA_logs/${theUser}/"
        $RSYNC $ROpts $SRC $DST
        # test to see if sync completed successfully
        if [ ! $? = 0 ]; then
                eRR="${theUser} SYNC NOT COMPLETED!!!"
                echo $eRR >> $theLog
                echo $eRR | mail -s "MirrorAgent.log sync problem" $ADMIN
        else
                echo $theUser >> $theLog
        fi
done
date >> $theLog
echo "---" >> $theLog
exit 0
# end script

This forum is awesome. Thanks, again.
# 5  
Old 06-07-2007
I can't claim credit, really.

I got it from
http://www.tldp.org/LDP/abs/html/

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert my shell script to C programming HELP!!

I had try to create a basic shell script. So now im trying to convert in to C-programming language can some one guide/help me out with it?(BTW IM USING A LINUX/UNIX/ORACLE SYSTEM) CODE BELOW !/bin/bash for i in `ls -1 /cslab/home/JAZEL/` do cp -uv $i /cslab/home/JAZEL/cs295/$i.`date... (2 Replies)
Discussion started by: Nygenesis
2 Replies

2. Shell Programming and Scripting

Looping not completing in shell script

Hi, Iam using below code to login to servers to get cpu utilisation. but output is coming for only one server. code is below root@blr-svr-oclan-01 # more SSSC_CPU_UTIL1.sh #!/bin/sh echo "CPU UTILIZATION" while read line; do IDLE=`/usr/local/bin/sshpass -p 'xxx' ssh xxx@$line 'sar 2 2' |... (1 Reply)
Discussion started by: surender reddy
1 Replies

3. Shell Programming and Scripting

Looping in the shell script with help of script timer.

Hello Experts- We are facing some issues in the while loop script when we use the script time to decide whether to exist from the loop or continue. Below is the script SrcExitLoop="FALSE" Src_InitialStartTime=`date +%s` Src_StartTime=`date +%s` Src_NUM_ALERTS=0 TOTAL_ALERTS=`expr <SOME... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

4. Shell Programming and Scripting

convert to shell script

how to convert these code to shell script #include<stdio.h> #include<conio.h> main() { int i,j,a=0,b=0,c=0,f,t,al,ta; int a1, max, n, n1,p,k=0; printf(“\n enter no.of resources”); scanf(“%d”,n1); printf(“\nenter the max no .of resources for each type”); for(i=0;i<n1;i++)... (4 Replies)
Discussion started by: syah
4 Replies

5. Shell Programming and Scripting

C Shell Script: While function not fully looping

I am new to scripting and this is probably the 4th or 5th simple script I have written. I am working with a HUGE number of data that need to be organized into folders and named a certain way. I wrote the naming script using a while function to go through the 1000-some folders and rename the files... (0 Replies)
Discussion started by: notluckyhannah
0 Replies

6. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

7. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

8. Shell Programming and Scripting

please convert the below program into shell script

if ( ( grep -i "Exception : " /home/dklog* )) then echo " improper combination" elsif ( ( grep -i "invalid" /home/dklog*)) then echo " wrong process " fi fi in the above case i am facing the the syntx error please help in this case... (3 Replies)
Discussion started by: mail2sant
3 Replies

9. Shell Programming and Scripting

looping through a variable in a shell script

hi, my first question is :- i would like to know how do i loop through the output of a variable. for ex:- if i have a variable called x and echo $x gives the output like feb 19 07 feb 20 07 feb 21 07 i would like to know how do i loop through this since it is separated and i... (1 Reply)
Discussion started by: ramachandranrr
1 Replies

10. Shell Programming and Scripting

Looping a perl script in a shell script

I am trying to get the follow script to run in the background on the 'fly'. I can launch it via cron and it will run in the background. BUT when I launch it from the command line it will run in the foreground. I figure it has to do with the while loop I have, but I have no clue how I can run the... (8 Replies)
Discussion started by: edkung
8 Replies
Login or Register to Ask a Question