Results 1 to 9 of 9
Hi Linux Gurus:
I am newbie to shell script programming. I was trying to write one script which look for $1.txt file in two dir (say in and out), and ...
- 05-18-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 8
Shell script for file existence
Hi Linux Gurus:
I am newbie to shell script programming. I was trying to write one script which look for $1.txt file in two dir (say in and out), and if file found in either of the said dir, it should copy back to my current working dir. Hope I made my requirement clear to you all.
Thanking you all in advance...
Regards,
..Jay
- 05-18-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
You need to check if the file exists using 'test'.
Check the man page for the test command and look through a couple of scripting tutorials.
Go here for a tutorial:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
Check this page for the test command for files:
http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html
And this should be the next:
http://tldp.org/LDP/abs/html/
Does this make sense? If not, feel free to ask more questions!
Regards
- 05-18-2007 #3
The following site is a good quick reference for file testing:
Metalshell.com - Sh Source Code - File Testing
- 05-21-2007 #4Just Joined!
- Join Date
- May 2007
- Posts
- 8
Dear All:
Thanks for your advice. I think I have not explained the things clear. I am running a script which will create a file say abc.txt in (in or out dir of my home dir). I just want to create a script which look for this file and no soon abc.txt file created in either of the mention above dir, it should copy back to my current working dir.
Kinldy help...
Regards,
...Jay
- 05-21-2007 #5
Is this what you're looking for?
This will look for a specified file in a certain path every 30 seconds.
Code:echo -n "What file are you looking for? " read FILE echo -n "Where should I look? " read PATH while [ ! $RESULT ] do sleep 30 RESULT=`find $PATH -name $FILE -print` done echo "I found it" echo $RESULT #cp $RESULT ~/
- 05-21-2007 #6
- 05-22-2007 #7Just Joined!
- Join Date
- May 2007
- Posts
- 8
Thanks Mr. Johnson, but while running the mention above script, loops got terminated if $1.txt not found in either of these dir. Whereas, I am looking for a script which keeps running untill $1.txt not found in the said dir. Once the $1.txt found, it should move to my working dir.
Please advice...
Regards,
...Jay
- 05-23-2007 #8Just Joined!
- Join Date
- May 2007
- Posts
- 8
Thanks to you all for your support and ideas...now I am able to get the same from mention below script"
#!/bin/sh
cdir=`pwd`
while [ ! $RESULT ]
do
if [ -e out/$1.txt ]
then
RESULT="out/$1.txt"
cp out/$1.txt .
cp out/$1_log.txt .
break
elif [ -e in/$1.txt ]
then
RESULT="in/$1.txt"
cp in/$1.txt .
cp in/$1_log.txt .
break
fi
sleep 30
done
Is it possible to check the time stamp of older files through script. If $1.txt is already existing in (in or out dir), the said above script will copy the same as it is. Can we check for time of $1.txt (suppose, if $1.txt creation time is 1 hr ago, then it shouldn't copy). Please suggest.
Regards,
...Jay
- 05-23-2007 #9
The easies solution I guess is cp -u, it will only copy then if the source file is newer than the destination file, or if the destinationfile doesn't exist.


Reply With Quote
