Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm stumped with this program I'm working on, can someone help. I'm new to UNIX and wanting to learn. The program I'm working on is to:
Write a Korn shell script that will search the UNIX file system of directories for a set of files passed to the script as a set of command line arguments. An example command line of how this script should be exited is:
$ script5 /export/home/studen1 myfile1 myfile2
Where:
script5 -- Name of shell script.
/export/home/studen1 -- Directory in which your search should begin.
myfile1 and myfile2 -- Files you are searching for.Note that the script must allow for 1 OR MORE file names. This example just happens to show only two.
Instructions
1. When a file is found, the full pathname for the file should be displayed.
2. If a file is not found, no message is necessary.
3. The script should validate the directory entered on the command line and display an error message if the directory was not found.
4. The script must handle one or more file names. The example command line above shows two names, but any number must be supported.
5. The script must validate that at least two arguments have been provided.Please, no advanced code, I'm not ready for it. Thank you, Sterling

You'll get more help if you show us the code that you've tried and we'll help you to figure out what is wrong.

This is in ksh:
if [ $# -lt 2 ]
then
echo "Minimum two arguments required"
exit
fiDIR=$1
ARGS=$#
counter=1if [ ! -d $DIR ]
then
echo "$DIR invalid directory"
exit
fishift
while [ $counter -lt $ARGS ]
do
find $DIR -name $1 -print 2> /dev/null
shift
(( counter = $counter + 1 ))
done
The find command will complain if you don't have permissions to view all directories under the directory you specified as $1.

Or for variety:
#!/bin/ksh
function error
{
print -u2 "$*"
exit 1
}pathname=$1
shift[[ -d ${pathname} ]] || error "No such directory '${pathname}'."
[[ -x ${pathname} ]] || error "You do not have search permission on directory '${pathname}'."cd ${pathname}
[[ -z "$*" ]] && error "No filenames specified."
for f in $*
do
[[ -f $f ]] && ls -al $f
done

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |