1826345 Members
3860 Online
109692 Solutions
New Discussion

help with a shell script

 
SOLVED
Go to solution
Mike Fishe_r
New Member

help with a shell script

Hi,

I am completely new to scripts and shell scripts.

I am looking to write a shell script to find the files in a catelog and print out..

like the script should search for a Parameter which is the name of a catalog... and check the existance of the catalog...If it does not exist throw an error else find the files that exists within this catalog excluding the subcatalogs and with the help of command anything like “list_files” and shows the type of each file
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: help with a shell script

Hi Mike:

If by "catalog" you mean a directory consider the following:

# cat ./mysh
#!/bin/sh
typeset DIR=$1
if [ ! -d "${DIR}" ]; then
echo "'${DIR}' is not a directory"
exit 1
fi
find ${DIR} -xdev -exec ls -ld {} +
exit 0

...pass as an argument, the name of a directory that you want to examine; as for example:

# ./mysh /home/mike

The script validates that the first argument passed is a directory and exits if not. If the directory test is passed, a 'find()' is used to run a 'ls' command for every file and subdirectory found. The '-xdev' argument to find() prevents the search from leaving the filesystem in which the directory occurs. The "+" terminator to the '-exec' argument improves performance by bundling many arguments together for each invocation of 'ls'.

For a good, beginning guide to shells and shell scripting, see:

http://docs.hp.com/en/B2355-90046/index.html

Learn the Posix shell which is the HP standard and a superset of the Korn88 ('ksh') shell. Avoid the C shell as it is fraught with dysfunctional features.

Regards!

...JRF...

Mike Fishe_r
New Member

Re: help with a shell script

Thanks JRF ...

James R. Ferguson
Acclaimed Contributor

Re: help with a shell script

Hi (again) Mike:

If you change:

typeset DIR=$1

...to:

typeset DIR=${1:-.}

...then you can always search the current directory by omitting any argument to the script.

The notation '${parameter :-word }' says that if the parameter is set and is nonnull, substitute its value; otherwise, substitute word. In this case, "word" is a dot ('.') which means the current directory.

Don't forget to look at and use the 'sh-posix(1)' manpages. There is a wealth of good information there.

Regards!

...JRF...