LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2008, 04:15 AM   #1
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Rep: Reputation: 15
process interaction in shell script


hi. i want to use tar -uf file1.tgz file2.tgz in my script. but a problem exist. i have to copy file1 from another location, in previous line and file1 is very larg ( about 100 megabytes). so before coping completes tar command executes and following error occurs:

Code:
tar: file1.tgz file2.tgz : No Such file or directory
How can i configure a command in shell script to wait for result of previous execution?
thanks for any help
 
Old 11-09-2008, 04:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Could you post the relevant part of the script?

Normally a command needs to finish before the next command is executed, this is default behavior.

Code:
cp /x/y/z/fileA /a/b/c/fileA
tar cf example.tar /a/b/c/fileA
In the above example the tar command will start after fileA is copied, no matter how long the cp action takes.
 
Old 11-09-2008, 04:56 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
I agree with druuna. Anyway you can always check the exit status of the previous command and launch the tar command accordingly, that is "if the file has been successfully copied, do the tar archive":
Code:
#!/bin/bash
cp /x/y/z/fileA /a/b/c/fileA
if [ $? -eq 0 ]
then
  tar cf example.tar /a/b/c/fileA
fi
 
Old 11-09-2008, 05:16 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
or:

Code:
cp /x/y/z/fileA /a/b/c/fileA && tar cf example.tar /a/b/c/fileA
 
Old 11-09-2008, 06:03 AM   #5
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Original Poster
Rep: Reputation: 15
but when i run my script tar says that related file is not exist! i think it try to use a file that it is not copied yet.
tar command is in merg function and copy starts in another function named backup

Code:
function merg
{
	tar -uf "df-"$filename".tgz   export-"$filename".tgz"
}
they called in this order:
backup $1 // creating df-$filename.tgz and export-$filename.tgz
merg // adds export-$filename.tgz to df-$filename.tgz

after shell execution, i run tar -uf df-081109.tgz export-081109.tgz it works successfully. and i guess it is because of copy process completion!
please pay attention that tar and copy execute in separate functions.
i use KSH and Solaris.
thanks for all replies. :-*

Last edited by a.toraby; 11-09-2008 at 06:06 AM.
 
Old 11-09-2008, 06:44 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Easiest (quick and dirty) way of doing this, is to have the first function perform:

Code:
cp /x/y/z/export-${filename}.tgz /a/b/c/ && date > /tmp/FINISHED
And have the second function perform:
Code:
if [-f /tmp/FINISHED]
then
   tar -uf df-${filename}.tgz export-${filename}.tgz
   rm -f /tmp/FINISHED
fi
 
Old 11-09-2008, 07:21 AM   #7
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Disillusionist View Post
Easiest (quick and dirty) way of doing this, is to have the first function perform:

Code:
cp /x/y/z/export-${filename}.tgz /a/b/c/ && date > /tmp/FINISHED
And have the second function perform:
Code:
if [-f /tmp/FINISHED]
then
   tar -uf df-${filename}.tgz export-${filename}.tgz
   rm -f /tmp/FINISHED
fi
i tried this command. finished created and removed, but problem continues! and tar -uf failed.
 
Old 11-09-2008, 07:53 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Modify the tar command to create an error report:

Code:
tar -uf df-${filename}.tgz export-${filename}.tgz 2> /tmp/Error.txt
Please post the exact contents from /tmp/Error.txt
 
Old 11-09-2008, 09:21 AM   #9
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Original Poster
Rep: Reputation: 15
Code:
tar: df-081109.tgz   export-081109.tgz: No Such file or directory
but both files exist
 
Old 11-09-2008, 09:37 AM   #10
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

you didn't show the copy part of the script yet. There are some possible reasons for the error:

- the files are copied to another directory
- there is a cd somewhere in the script so the tar starts not in the directory containing the files
- the copy command runs in the background
- ...

Try pwd >>/tmp/Error.txt; ls -l >>/tmp/Error.txt before the tar starts.

Jan

Last edited by jan61; 11-09-2008 at 10:07 AM. Reason: Error.txt output should be appended
 
Old 11-09-2008, 10:02 AM   #11
permalac
Member
 
Registered: Jul 2007
Location: Barcelona
Posts: 115

Rep: Reputation: 16
also you can launch your script with

$> sh -x script.sh


to see some debug info.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
shell script - process directory keith2045 Programming 7 05-31-2007 07:26 PM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM
shell script interaction sudhasmyle Linux - Software 1 12-08-2004 03:34 PM
Interaction in shell script sudhasmyle Programming 1 12-08-2004 07:27 AM


All times are GMT -5. The time now is 01:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration