Shell Script Passing Parameters For Directory Listing

i have this basic piece of code that i am trying to debug to accept input parameter to be able to display a directory listing of files.

cd /u02/app/eatv/dev/out
CURDIR=`pwd`
echo directory listing of $CURDIR
echo
if [ $1 -e 0 ]; then
  ls -latr
else
  ls -latr $1
fi

basically if the script does not see any parameter being passed it will display all the file listing of the current directory. and if it find a parameter it display only a specfic files on the directory. the above code does not work and when i run i am getting this error:

$ sh dir_list_eatv_out.sh *.ini
directory listing of /u02/app/eatv/dev/out

dir_list_eatv_out.sh[20]: -e: 0403-012 A test command parameter is not valid.
ls: 0653-341 The file cca_monthly_load_data.ini does not exist.
$

thanks,
warren

Try

if [ $1 -eq 0 ]; then

Or

if [ $1 = "0" ]; then

regards,
Arun.

thanks. i tried both and still getting an error.

cd /u02/app/eatv/dev/out
CURDIR=`pwd`
echo directory listing of $CURDIR
echo
if [ $1 -eq 0 ]; then
  ls -latr
else
  ls -latr $1
fi
$ sh dir_list_eatv_out.sh *.ini
directory listing of /u02/app/eatv/dev/out

dir_list_eatv_out.sh[20]: cca_monthly_load_data.ini: 0403-009 The specified numb
er is not valid for this command.
ls: 0653-341 The file cca_monthly_load_data.ini does not exist.
$
cd /u02/app/eatv/dev/out
CURDIR=`pwd`
echo directory listing of $CURDIR
echo
if [ $1 = "0" ]; then
  ls -latr
else
  ls -latr $1
fi
$ sh dir_list_eatv_out.sh *.ini
directory listing of /u02/app/eatv/dev/out

ls: 0653-341 The file cca_monthly_load_data.ini does not exist.
$

not sure what you're after, but....

if [ ${#} -eq 0 ]

thanks. in pseudo code

if input file variable is null then
display the file variable listing
else
dislay all the files in the directory
end if

assuming the i pass a parameter of *.ini it will display all the files having an extension of *.ini otherwise displays all the files on the directory

i tried and still there is an error

#!/bin/sh
cd /u02/app/eatv/dev/out
CURDIR=`pwd`
echo directory listing of $CURDIR
echo
if [ ${1} -eq 0 ]; then
  ls -latr
else
  ls -latr $1
fi
$ sh sample.sh *.ini
directory listing of /u02/app/eatv/dev/out

sample.sh[6]: cca_monthly_load_data.ini: 0403-009 The specified number is not va
lid for this command.
ls: 0653-341 The file cca_monthly_load_data.ini does not exist.
$

Check for an empty parameter:

cd /u02/app/eatv/dev/out
CURDIR=`pwd`
echo directory listing of $CURDIR
echo
if [ -z "$1" ]; then
  ls -latr
else
  ls -latr $1
fi

i tried that and it did not work.

$ ls -latr
total 112
drwxr-sr-x    3 ccalftdv ccalogrp        512 Sep 13 2006  dev
-rwxr-xr-x    1 ccalftdv ccalogrp       5586 Jun 08 15:55 cca_monthly_load_file.
ksh
-rwxr--r--    1 ccalftdv ccalogrp       5729 Jun 08 15:55 load_file.ksh
-rwxrwxrwx    1 ccalftdv ccalogrp         63 Jun 11 15:14 display_date.sh
-rwxr-xr-x    1 ccalftdv ccalogrp        680 Jun 25 08:50 cca_monthly_load_data.
ini
drwxr-sr-x    4 ccalftdv ccalogrp        512 Jun 25 08:50 ..
-rw-r--r--    1 ccalftdv ccalogrp        679 Jun 25 10:28 load_data.ini
-rw-r-----    1 ccalftdv ccalogrp       1281 Jun 26 10:10 dir_list_ccalloc_in.sh
-rw-r-----    1 ccalftdv ccalogrp       1281 Jun 26 10:36 dir_list_ccalloc_out.s
h
drwxr-sr-x    3 ccalftdv ccalogrp        512 Jun 26 15:02 .
-rw-r-----    1 ccalftdv ccalogrp        142 Jun 26 16:17 dir_list_eatv_out.sh


$ sh dir_list_eatv_out.sh
directory listing of /u02/app/eatv/dev/out

total 0
drwxrwxr-x    4 oracle   eatvgrp         256 Jun 19 09:41 ..
drwxrwxr-x    2 oracle   eatvgrp         256 Jun 24 08:00 .
$ sh dir_list_eatv_out.sh *.ini
directory listing of /u02/app/eatv/dev/out

ls: 0653-341 The file cca_monthly_load_data.ini does not exist.
$

If you give *.ini in the command line, shell will expand it, so your script get list of files, not string ex. *.ini

[ $# -lt 1 ] && ls -altr && exit
for f in $*
do
    # but remember, if argument is directory, ls will so it ...
    ls -altr $f
    # but you can test file/dir
    # regular file only
    [ -f $f ] && ls -altr $f && exit
    # directory
    [ -d $f ] && ls -dla $f && exit
done

But if you like to give '.ini' for script and not like to shell expand it, then command line is
sh script '
.ini'

[ $# -lt 1 ] && ls -altr && exit
ls -altr $1

i got it to work by assigning the parameter to another variable.

#!/bin/sh
biff n
cd /u02/app/eatv/dev/out
CURDIR=`pwd`
pfile=$1
echo directory listing of $CURDIR
echo
if [ -z "$pfile" ]; then
  ls -latr
else
  ls -latr $pfile
fi
$ sh dir_list_eatv_out.sh
directory listing of /u02/app/eatv/dev/out

total 16
drwxrwxr-x    4 oracle   eatvgrp         256 Jun 19 09:41 ..
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.csv
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
drwxrwxr-x    2 oracle   eatvgrp         256 Jun 30 09:31 .
$ sh dir_list_eatv_out.sh *.txt
directory listing of /u02/app/eatv/dev/out

-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
$ sh dir_list_eatv_out.sh *.csv
directory listing of /u02/app/eatv/dev/out

-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.csv
$

---------- Post updated at 10:01 AM ---------- Previous update was at 09:53 AM ----------

thank you kshji that works too. on the same code you posted how do i add a parameter for the directory? yet it works if the parameter i entered is a combination of directory/file extension name. but for readability purpose we use the output as a log file to keep track of the files on the directory. we wanted to see something that on the first line of display it shows the directory name then followed by the files.

  directory listing of /u02/app/eatv/dev/out

  total 16
  drwxrwxr-x    4 oracle   eatvgrp         256 Jun 19 09:41 ..
  -rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.csv
  -rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
  drwxrwxr-x    2 oracle   eatvgrp         256 Jun 30 09:31 .
  $

If there are "*.ini" files in the directory where you run your script, the *.ini will expand to a list of the files in your current directory before the script even changes directory to
/u02/app/eatv/dev/out.