The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to retrieve original contents of a modified file (modified using vi) novice100 UNIX for Dummies Questions & Answers 3 05-31-2007 08:50 PM
chmod command for recently modified files polka_friend UNIX for Dummies Questions & Answers 2 08-30-2006 03:25 PM
who modified my file!! mohanprabu UNIX for Dummies Questions & Answers 4 10-28-2005 10:18 PM
File last modified szzz High Level Programming 4 11-05-2003 11:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-26-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
ftp most recently modified file

Hi what is the most optimum way to ftp the most recently modified file starting with a particular string.

i tried this

Code:
    ftp -n 2>logfile 1>&2 <<EOF
    open xxxxxx
    user xxxx xxxx
    prompt
    ls -ltr f* res
    !var=`tail -1 |awk { print $9 }'`
    bye
EOF
that gives the error in ls any suggestions or alternatives???
  #2 (permalink)  
Old 02-27-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Here is a script that demonstrates how to use ftp to do a "dir" and parse the results. I wanted to keep it as simple as possible, so you might need to adjust the read statement.

Code:
#! /usr/bin/ksh

#
# Connnect to the host, execute a "dir" sending the output to a local
# file called "listing".

HOST=this
USER=that
PASSWD=secret

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p prompt
print -p dir listing
print -p bye


#
#  Get ready to decode the directory listing

typeset -Z2 nmonth day
typeset -i8 octal

set +A ts $(date "+%Y %m")  THISYEAR THISMONTH
THISYEAR=${ts[0]}
THISMONTH=${ts[1]}
((LASTYEAR=THISYEAR-1))

#
#  Function to convert month to numeric

conv_month() {
        typeset -l month
        month=$1
        case $month in
        jan)    nmonth=1  ;;
        feb)    nmonth=2  ;;
        mar)    nmonth=3  ;;
        apr)    nmonth=4  ;;
        may)    nmonth=5  ;;
        jun)    nmonth=6  ;;
        jul)    nmonth=7  ;;
        aug)    nmonth=8  ;;
        sep)    nmonth=9  ;;
        oct)    nmonth=10 ;;
        nov)    nmonth=11 ;;
        dec)    nmonth=12 ;;
        *)      nmonth=0  ;;
        esac
        echo $nmonth
        return $((!nmonth))
}



exec < listing


#
#  Read Loop
#  You need to adjust the number of "junk" entries in the read statement to line
#  up the fields.  There might be two formats:
#   -rwxr--r--   1 users        959 Dec 18  2001 listr.old
#   -rwxr--r--  1 user  None  2215 Feb 26 23:33 ftpjob


while IFS=" " read permstring junk junk size month day swing rawname ; do

        #
        # Get rid of total line and any entries for directories, symlinks, etc.

        char1=${permstring%%${permstring#?}}
        if [[ $char1 != "-" ]] ; then
                continue
        fi

        #
        #  decode permissions
        set -A perms -- $(print -- ${permstring#?} | sed 's/./& /g')
        extras=0
        [[ ${perms[2]} = S ]] && { ((extras=extras+4000)); perms[2]=- ; }
        [[ ${perms[2]} = s ]] && { ((extras=extras+4000)); perms[2]=x ; }
        [[ ${perms[5]} = S ]] && { ((extras=extras+2000)); perms[5]=- ; }
        [[ ${perms[5]} = s ]] && { ((extras=extras+2000)); perms[5]=x ; }
        [[ ${perms[8]} = T ]] && { ((extras=extras+1000)); perms[8]=- ; }
        [[ ${perms[8]} = t ]] && { ((extras=extras+1000)); perms[8]=x ; }

        binary=2#$(print -- ${perms[@]} | sed 's/ //g;s/-/0/g;s/[^0]/1/g')
        ((octal=binary))
        result=$(echo $octal)
        result=${result#??}
        ((result=result+extras))

        #
        # Decode date and time and convert it to yyyymmddhhmm
        # If no time is present, use 0000.
        # If no year is present, figure it out.
        nmonth=$(conv_month $month)
        if [[ $swing = *:* ]] ; then
                if [[ $nmonth > $THISMONTH ]] ; then
                        ((year=LASTYEAR))
                else
                        ((year=THISYEAR))
                time1=${swing%???}
                time2=${swing#???}
                time="${time1}${time2}"
                fi
        else
                year=$swing
                time="0000"
        fi

        echo $name size=$size perms=$result "timestamp=["$year $nmonth $day ${time}"]"
done


exit
  #3 (permalink)  
Old 02-27-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
here is what i could find the best. Please suggest me better ways than this:

Code:
for filename in `cat filelist` ; do
export file=`rexec remote_server ls -t ${filename}* | head -1`
echo "THE FILENAME IS" $filename
ftp -n <<-EOF
open bdwux001
user xxxxx xxxxxxx
mget $file
bye
EOF
echo "END"
done

Last edited by ahmedwaseem2000; 02-27-2007 at 11:05 AM..
  #4 (permalink)  
Old 02-27-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
You seem to reject my technique without commenting on it. If your script is working I guess it's good enough. But you are depending on the rexec protocol being enabled and often it is not available. You also use one ftp process per file. If you switch to a ksh coprocess, a single ftp process is enough for the entire job.
  #5 (permalink)  
Old 02-27-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Quote:
Originally Posted by Perderabo
You seem to reject my technique without commenting on it. If your script is working I guess it's good enough. But you are depending on the rexec protocol being enabled and often it is not available. You also use one ftp process per file. If you switch to a ksh coprocess, a single ftp process is enough for the entire job.
I AM SORRY, IF I HAVE SHOWED ANY SIGNS OF REJECTION. the only reason why I went in for this process is that my team wasnt comfortable with that. I would never doubt your techniques, you do splendid job always. and rexec was enabled and i .netrc is required before running it.

I am not aware of coprocess could you please explain it?? THANKS FOR ALL YOUR HELP AND EFFORTS TO HELP ME AND ALL OTHERS IN THE FORUM.
  #6 (permalink)  
Old 02-27-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
A coprocess is setup by:
command |&

Then "print -p" sends stuff to the coprocess as standard input and "read -p" gets stuff back from the coprocess' standard output. This only works with ksh and it is documented on the ksh man page.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0