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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using functions in Scripting 2

Status
Not open for further replies.

annay121

Programmer
May 3, 2005
11
US
I would like to put the following lines of code in a function:

LINE_COUNT=`wc -l ${LOCAL_DIR}/FileList.txt`
COUNT=`echo $LINE_COUNT | cut -s -d' ' -f1`

FILENAME=`cat ${LOCAL_DIR}/FileList.txt | /usr/xpg4/bin/awk -v mycount=${COUNT} 'BEGIN{FS=" "} {if(NR == mycount) {print $9}}'`


The function would take ANY 'filelist.txt' and would return the 'filename'. The contents of the 'filelist.txt' would be as follows:

-rw-rw-rw- 1 user group 631 May 16 16:26 test_20050516_163000.txt
-rw-rw-rw- 1 user group 631 May 16 16:41 test_20050516_164500.txt
-rw-rw-rw- 1 user group 630 May 16 16:56 test_20050516_170000.txt
-rw-rw-rw- 1 user group 631 May 16 17:11 test_20050516_171500.txt

 
Something like this ?
FILENAME=`/usr/xpg4/bin/awk 'END{print $9}' ${LOCAL_DIR}/FileList.txt`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
annay121 said:
I would like to put the following lines of code in a function:

OK. what's stopping you?

but why are you jumping though so many hoops to do a simple thing?

Aren't you just trying to get the file name from the LAST line in file filelist.txt?

Wouldn't a simple 'tail/awk' be enough?

tail -1 filelist.txt | nawk '{print $NF}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
PHV,
yeah that's better - Solaris' nawk is broken this way!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry for hooping around so much vgresh99 but i am relatively new to scripting.
Also, PHV the solution you have given does not give me any result.

The FileList.txt has this data :

-rw-r--r-- 1 annay vob_two 0 May 11 16:36 test_20050505_131500.txt
-rw-r--r-- 1 annay vob_two 0 May 11 16:36 test_20050505_132500.txt
-rw-r--r-- 1 annay vob_two 0 May 11 16:36 test_20050505_133500.txt
-rw-r--r-- 1 annay vob_two 0 May 11 16:36 test_20050505_134500.txt
-rw-r--r-- 1 annay vob_two 0 May 11 16:42 test_20050516_160000.txt

My complete script is as below:

#!/bin/ksh

LOCAL_DIR=
FTP_SERVER=
USERNAME=
PASSWORD=
FTP_DIR=

ftp -n $FTP_SERVER > ${LOCAL_DIR}/FileList.txt 2> ${LOCAL_DIR}/ftp.err <<START
user $USERNAME $PASSWORD
cd $FTP_DIR
prompt n
dir test*
quit
START

FILENAME=`awk 'END{print $9}' ${LOCAL_DIR}/FileList.txt`
echo "FILENAME:"$FILENAME

exit 0

Is there anything I am doing wrong ?
Thanks
 
Replace this:
FILENAME=`awk 'END{print $9}' ${LOCAL_DIR}/FileList.txt`
By either this:
FILENAME=`/usr/xpg4/bin/awk 'END{print $9}' ${LOCAL_DIR}/FileList.txt`
Or this:
FILENAME=`awk '{x=$9}END{print t}' ${LOCAL_DIR}/FileList.txt`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
a little change to PHV last "change":
FILENAME=`awk '{x=$9}END{print x}' ${LOCAL_DIR}/FileList.txt`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top