Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello, Can some one tell me what was wrong with my script?
I have file called list
cat list
s001 v
s010 h
s999 s
==================
here is my script:
for i in `cat list`
do
store=`echo $i |awk '{print $1}'`;
ver=`echo $i |awk '{print $2}'`;
echo $store;
echo $ver;
echo $store;
echo $ver;
#some other command
echo "tar.test.backup."$store;
echo "tar.test.backup."$ver;
done
==============================
when I excute this script
the result I got like this:
s001
s001
tar.test.backup.s001
tar.test.backup.
v
v
tar.test.backup.v
tar.test.backup.
s010
s010
tar.test.backup.s010
tar.test.backup.
s
s
tar.test.backup.s
tar.test.backup.
s999
s999
tar.test.backup.s999
tar.test.backup.
h
h
tar.test.backup.h
tar.test.backup.
=========================
what I am expecting is:
s001
v
s001
v
tar.test.backup.s001
tar.test.backup.v
----------------
s010
h
s010
h
tar.test.backup.s010
tar.test.backup.h
-------------
s999
s
s999
s
tar.test.backup.s999
tar.test.backup.s
=====================
what went wrong eith my script?
Please help
Thanks in advance

Don't use a for loop to read a file line-by-line. Use the while loop. Since your list file is two fields do this:
while read store ver
do
echo "$store"
echo "$ver"
done < listThe for loop parses on white space which is why you are having the problem. Also, look at this link on UUOC:

Thanks Nails.
the script is working with regular unix command, but when I try to call another script inside the loop , it is not working well again:
Here is what I am trying to do:
while read store version
docopyto $store log_file tar.test.backup.$version >> log_file 2>&1 # this one doesn't work
rcp tar.test.backup.$version.Z user@$store:/dir/ #this one is working
echo tar.test.backup.$version;
done <list
what copyto is a simple script which take 3 parameters ($store, log_file, tar_file by $version)basicly just rcp the tar_file to store server, uncompress it, and untar it
without copyto line, I got: ( -- correct)
s001
tar.test.backup.v
s002
tar.test.backup.v
s013
tar.test.backup.s
s006
tar.test.backup.s
s957
tar.test.backup.h
s973
tar.test.backup.h
==================
with copyto line
I got:
s001
tar.test.backup.v
and the exit the loop. the file is not copied overCan you tell me how can I make this work?
Thank you so much

I have no idea. First, I'd verify that the rcp command actually works from the command line.
Second, please publish the copyto script, and I might be able to give you an opinion.

Sorry Nails - not provided enough info.
Here are the all info:
Main script:--test
while read store version
do
echo begin
sendto $store log.$store tar.test.backup.$version
echo end
done <list
=========================
cat list
s001 v
s002 h
s003 s
========================
cat copyto
store=$1
outfile=$2
while [ "$3" ]
do
rcp -p $3.Z user@$store:/tmp >>$outfile 2>>$outfile
rsh $store -l user "uncompress -f /tmp/$3" >>$outfile 2>>$outfile
rsh $store -l user "tar -xvpf /tmp/$3" >>$outfile 2>>$outfile
rsh $store -l user "rm /tmp/$3" >>$outfile 2>>$outfile
shift
done
=========================
when I issue command:
sh -x test
I got the following log info:
+ read store version
+ echo begin
begin
+ sendto s001 log.s001 tar.test.backup.v
+ echo end
end
+ read store version
===================
The file did get copied to server s001 and then exit out -- not processing for the rest serverBUT if I using rcp it will working fine:
Main script-- test1:
while read store version
do
echo begin
#sendto $store jms.$store tar.test.backup.$version
rcp tar.test.backup.$version.Z user@$store:/tmp/
echo end
done <list
================
I got this:
+ read store version
+ echo begin
begin
+ rcp tar.test.backup.v.Z user@s001:/tmp/
+ echo end
end
+ read store version
+ echo begin
begin
+ rcp tar.test.backup.h.Z user@s002:/tmp/
+ echo end
end
+ read store version
+ echo begin
begin
+ rcp tar.test.backup.s.Z user@s003:/tmp/
+ echo end
end
+ read store version
========================
Hope I have made it clear this timeThanks in advance

It's still hard to say. you appear to be mixing up "sendto" and "copyto"???If any of your arguments, have spaces, you'll have trouble. Try surrounding your arguments with double quotes. Instead of:
sendto $store log.$store tar.test.backup.$version
try:
sendto "$store" "log.$store" "tar.test.backup.$version"
also, maybe the shell is having trouble with the copyto while loop:
while [ "$3" ]
..
..
shift
end whileallows you to send more than one 3 arguments. If there are more than 3 arguments, the shift command will shift arg 4 to arg 3 and your loop executes again. Maybe some extra garbage is on the command line and it trys to interpret it and fails.
If the problem continues why don't you take your sendto/copyto and embed the rcp/rsh in the original script and don't call the second script?

the copy is just a copy of sendto-- i renamed it for my testing.
there is no space in arguments.I guess I'll need take your advice to embed the copyto in my main script.
Thanks very much

Hi Nails,
I still can't make it work :(
It seems my while loop is having issue
I have embed my copyto into one script
======================================
Here is my main -- test
#!/usr/bin/sh
while read store version
do
echo begin
echo $store $version;
rcp -p tar.test.backup.$version.Z user@$store:/tmp >> log.$store 2>&1
rsh $store -l user "uncompress -f /tmp/tar.test.backup.$version.Z" >> log.$store 2>&1
rsh $store -l user "tar -xvpf /tmp/tar.test.backup.$version" >> log.$store 2>&1
rsh $store -l user "rm /tmp/tar.test.backup.$version" >> log.$store 2>&1
echo end
done<list
========================================
cat list
s001 v
s002 s
s003 h
========================================
when I excute it
I got this:
>sh -x test+ read store version
+ echo begin
begin
+ echo s001 v
s001 v
+ rcp -p tar.test.backup.v.Z user@s001:/tmp
+ rsh s001 -l user uncompress -f /tmp/tar.test.backup.v.Z
+ rsh s001 -l user tar -xvpf /tmp/tar.test.backup.v
+ rsh s001 -l user rm /tmp/tar.test.backup.v
+ echo end
end
+ read store version
-------------------------
It works for s001 server only but not for s002,s003
but if I comment out the rest command to only keep one. The loop will continue as I expected
-------------------------
sh -x test
+ read store version
+ echo begin
begin
+ echo s001 v
s001 v
+ rcp -p tar.test.backup.v.Z user@s001:/tmp
+ echo end
end
+ read store version
+ echo begin
begin
+ echo s002 s
s002 s
+ rcp -p tar.test.backup.s.Z user@s002:/tmp
+ echo end
end
+ read store version
+ echo begin
begin
+ echo s003 h
s003 h
+ rcp -p tar.test.backup.h.Z user@s003:/tmp
+ echo end
end
+ read store version
==========================
It is something to with rsh.
I figured that if there is no rsh then it works fine but after rsh the while loop lost the control.
Please help.Thx

Is anything in the error files?
Sounds to me that that your rsh command isn't completing properly. How could that happen?
If you don't have read/write permissions on this file:
rsh s001 -l user rm /tmp/tar.test.backup.v
the shell will ask you this question:
rm: tar.test.backup.v: override protection 440 (yes/no)?
Try the -f override flag on the rm command:
rsh s001 -l user rm -f /tmp/tar.test.backup.v
You also might try sleeping for a few seconds after the rsh command.

Hi Nails,
I got my issue resoved by doing work around
for i in `cat list`
do
Store=`echo $i|cut -c1-4`
version=`echo $i |cut -c5-5`
copyto...
done
cat list
s001v
s002s
s003hIt works fine this way.
to answer your questions:
Is anything in the error files? no
If you don't have read/write permissions on this file: -- permission is not issue here
Try the -f override flag on the rm command:
tried and still not work
You also might try sleeping for a few seconds after the rsh command. -- sleep not fix the issue.To me, in my enviroment, for loop having space issue; while loop can't work well with rsh; so I removed the space in list file and using for loop fix the problem.
Thank you very much for your help

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |