1752290 Members
5450 Online
108786 Solutions
New Discussion юеВ

Re: Korn Shell Scripting

 
SOLVED
Go to solution
Joyce S. Solomon_1
Occasional Advisor

Korn Shell Scripting

Hi All,
Need help and suggestions from all you geniouses out there.

I am writing a tiny script to FTP some files from few directories. I have succeeded in the beginning where the scripts once run shows all files that are modified/created on the current date. I did that with the simple "ls -al|grep $dd" where I assign the $dd to be a variable where current date will be passed in.

This is my question. Once the files are displayed , how do i write the script to only FTP over only those files that are shown.
Say for example:
Once "ls -al|grep $dd" is run, i am shown three files, how do i only isolate this three files and FTP them over, the script to FTP I have completed, only the part where to chose only that particular files that I am not sure how to do.
Please help

Thanks in advance.

Joyce
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: Korn Shell Scripting

Hi Joyce:

You indicate that you have a script the actually does the ftp process you want. Let's assume that that script is called "myftp.sh" and that it is passed a single argument which is the name of the file to ftp. Given this, one solution to your question is this:

cd mydirectory
for F in `ls -l | grep "$dd" | awk '{print $NF}'`
do
./myftp.sh $F
done

Regards!

...JRF...
linuxfan
Honored Contributor

Re: Korn Shell Scripting

Hi Joyce,

You may also want to look at rdist, its pretty useful and allows what you are trying to achieve.
You can get rdist at
http://hpux.cs.utah.edu/hppd/hpux/Networking/Admin/rdist-6.1.5

But if that is a overkill, you can write a script.

-Regards
I am RU
They think they know but don't. At least I know I don't know - Socrates
Annied
Occasional Contributor

Re: Korn Shell Scripting

Hi Joyce,

You can use the "find" command (do: man find, to find more about the options you can use with this command, there is options you can use to select files depending on a creation/modified date,....).

Example, to list all files named *.txt that have not been accessed for a week... do the following command:

find / -name '*.txt' -atime +7 -exec ls -la {} \;

The "{}" will trap the files found in the find command and use this as the input list...

You can integrate your specific commands in the exec statement....

Hope this will help.
La Vita Bella
Jared Westgate_1
Valued Contributor

Re: Korn Shell Scripting

Hello Joyce,

How about having your script write a ftp configuration (I'm not sure what is is actually called) file. You could do something like this. I would just have it create the ftpconfiguration file and use it to ftp the files you need.

CURRDATE=`date +"%b %e"`
FTPCONFILE="/tmp/ftpconfig"
FILELOCATION="/home/jwestgat"
FTPDESTDIR="/tmp"
FILELIST=`ls -al $FILELOCATION | grep "$CURRDATE" | tr -s " " " " | cut -f9 -d "
"`

echo "open hostname" > $FTPCONFILE
echo "user username password" >> $FTPCONFILE
echo "binary" >> $FTPCONFILE

for FILE in $FILELIST
do
echo "put $FILELOCATION/$FILE $FTPDESTDIR/$FILE" >> $FTPCONFILE
done

echo "bye" >> $FTPCONFILE

ftp -vin < $FTPCONFILE

Give it a try, and I hope it helps,

Jared
Joyce S. Solomon_1
Occasional Advisor

Re: Korn Shell Scripting

Hi All.

Thanks a zillion for the help.
I have coded my scripts and when i run it , at the FTP prompt, the below is the message that i got, any idea?

***********
Sending files ....
Connected to
220 Anonymous FTP Server Ready
331 Hello joyce, mail me at softhead@online.no in case of trouble
230 User joyce logged in to server
We only support non-print format, sorry.
?Invalid command
.
.
.
********************

The scenario is I am using FTP to send files from my HPUX box to a windows 98 pc. At the windows pc , i have installed a ftp server to enable this action.

My script is something like below:
////////////////////////////
#in the hpux server
DIR="/tmp"
#in the windows 98 machine
FILEDES="/tmp"

FILELIST=`ls -al $DIR | grep "$dd" | tr -s " " " " | cut -f 9 -d " "`
# FTP file from Billing Server
ftp -n -v <user
for FILE in $FILELIST
do
put $DIR/$FILE $FILEDES/$FILE

done
bye
////////////////////////////

Please do advice

Thanks in advance

Regards
Joyce
Jagadeesh Kumar
Advisor

Re: Korn Shell Scripting

Hi,

You are getting the following error as you are using "for" statement in your ftp-text.
- "We only support non-print format, sorry. "

The input that is given to 'ftp' must contain only the text that can be understood by ftp.

Try modifing your script as follows:
////////////////////////////
#in the hpux server
DIR="/tmp"
#in the windows 98 machine
FILEDES="/tmp"

FILELIST=`ls -al $DIR | grep "$dd" | tr -s " " " " | cut -f 9 -d " "`

FTP_TEXT=/tmp/ftp_text
echo "user " > $FTP_TEXT
for FILE in $FILELIST
do
echo "put $DIR/$FILE $FILEDES/$FILE" >>$FTP_TEXT
done
echo "bye " >>$FTP_TEXT

ftp -niv <$FTP_TEXT
////////////////////////////

If you need to add some more parameters use echo appropriately and insert the contents to the FTP_TEXT file.

Hope this works and helps.

Jagadeesh
Jagadeesh Kumar
Advisor

Re: Korn Shell Scripting

Hi,

If you feel not to involve one more text file(/tmp/ftptext) as ion my previous response,
try this alternative

////////////////////////////
#in the hpux server
DIR="/tmp"
#in the windows 98 machine
FILEDES="/tmp"

FILELIST=`ls -al $DIR | grep "$dd" | tr -s " " " " | cut -f 9 -d " "`

( echo "user "
for FILE in $FILELIST
do
echo "put $DIR/$FILE $FILEDES/$FILE"
done
echo "bye"
) | ftp -niv

////////////////////////////

Hope this works and helps.

Jagadeesh
Jared Westgate_1
Valued Contributor

Re: Korn Shell Scripting

Wow Jagadeesh!

I really like your second example. I never thought doing it like that.


Joyce,

Jagadeesh is correct. You will either have to send the text to a file, like in mine and Jagadeesh's examples, or you can do it like Jagadessh's last example.

The problem is that when you specify "<
What you want is for the script to execute the for loop, and send its results to FTP. You should be able to use either solution to do this for you.

Best of luck!

Jared