Computing.Net > Forums > Unix > need help with unix script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

need help with unix script

Reply to Message Icon

Name: jds
Date: September 18, 2008 at 11:20:28 Pacific
OS: xp
CPU/Ram: 2gb
Product: xp
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 18, 2008 at 11:49:31 Pacific
Reply:

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 < list

The for loop parses on white space which is why you are having the problem. Also, look at this link on UUOC:

http://partmaps.org/era/unix/award....


0

Response Number 2
Name: jds
Date: September 18, 2008 at 14:22:36 Pacific
Reply:

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
do

copyto $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 over

Can you tell me how can I make this work?
Thank you so much


0

Response Number 3
Name: nails
Date: September 19, 2008 at 06:48:30 Pacific
Reply:

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.


0

Response Number 4
Name: jds
Date: September 19, 2008 at 10:06:32 Pacific
Reply:

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 server

BUT 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 time

Thanks in advance


0

Response Number 5
Name: nails
Date: September 19, 2008 at 12:17:54 Pacific
Reply:


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 while

allows 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?


0

Related Posts

See More



Response Number 6
Name: jds
Date: September 19, 2008 at 12:29:13 Pacific
Reply:

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


0

Response Number 7
Name: jds
Date: September 22, 2008 at 10:11:44 Pacific
Reply:

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


0

Response Number 8
Name: nails
Date: September 22, 2008 at 11:51:01 Pacific
Reply:

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.


0

Response Number 9
Name: jds
Date: September 23, 2008 at 23:34:30 Pacific
Reply:

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
s003h

It 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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: need help with unix script

unix scripting help needed. www.computing.net/answers/unix/unix-scripting-help-needed/2473.html

i need help with unix shell script www.computing.net/answers/unix/i-need-help-with-unix-shell-script/8239.html

need help on Unix program www.computing.net/answers/unix/need-help-on-unix-program/4936.html