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.
$
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.
$
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.
$
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'
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.