Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a dynamic shell script? 2

Status
Not open for further replies.

SMerrill

Programmer
Jan 19, 2002
145
US
I'm not a UNIX guy, but I need to tell UNIX to search for all files in recursive subdirectories that are named YYMMDD.Z where YYMMDD is yesterday's date.

So far, I've computed yesterday's date with this:
Code:
# Yesterday.AWK
BEGIN {
  printf "Yesterday's filename is '%s.Z'\n",strftime("%y%m%d",systime()-86400)
}


Now for the rest of it. I think in DOS I'd simply say
Code:
 DIR /S /B YYMMDD.Z and it would list out the files that matched.
Once I get the names of the files (if any), I need to FTP them into the ftp://hostname/ACD/Loc99/YYMMDD.Z location, where "/loc99/" is the same part of the filename from the above directory listing.  (The same directory already exists on the destination side.)

So I need to know how to convert this DOS idea into a pure UNIX idea, like this:

Compute yesterday's filename (with the above AWK scriptlet)
build an "ls" command (I think that's the equivalent of DIR) that looks for file names in the UNIX system
I want the output of the ls command to resemble this:
[code]
/u/data/reports/loc13/030917.z
/u/data/reports/loc5/030917.z
/u/data/reports/loc19/030917.z
I need to use AWK to convert that ls output into an FTP script such as

Code:
user username
passwierd
cd ../../../u/data/reports/
binary
put loc13/030917.Z
put loc5/030917.Z
put loc19/030917.Z
disconnect
quit

then I finally need to execute that dynamic FTP script.

Is this too much to ask on the forum? If not, thanks for thinking it through. If so, please let me know.
Thanks,


Take care,
--Shaun Merrill
Seattle, WA
 
Here are a few bits which should help you

Code:
#!/bin/sh

yesterday ( ) {
  awk 'BEGIN { print strftime("%y%m%d",systime()-86400) }'
}

# where to start looking for files
root=/u/data/reports/

# work out the name of yesterdays file
file=`yesterday`.Z
echo $file

# find all those files, and remove the root prefix from each one
files=`find $root -name $file -print | sed 's#'$root'##g'`
echo $files

# now create the command script
(
cat << HERE1
user username
passwierd
cd ../../../u/data/reports/
binary
HERE1

for i in $files ; do
  echo &quot;put $i&quot;
done

cat << HERE2
disconnect
quit
HERE2
) > newscript.sh

# then perhaps something like
# ftp -s newscript.sh

--
 
I like the idea of using awk to do everything....

find $root -name $file -print |awk -v root=$root 'BEGIN {
print &quot;open hostname&quot;;
print &quot;user username&quot;;
print &quot;passwierd&quot;;
print &quot;cd ../../../u/data/reports/&quot;;
print &quot;binary&quot;;
} {
sub(root,&quot;put &quot;);
print $0;
} END {
print &quot;bye&quot; ;
}' | ftp -n
 
Hey, thanks for your help. I'll put this to good use.

Take care,
--Shaun Merrill
Seattle, WA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top