Re: Input Parameters (199 Views)
Reply
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 1 of 11 (199 Views)
Accepted Solution

Input Parameters

Hello Folks,

I'm a newbie at scripting and I'm trying to write a script that will examine each argument passed and report whether the argument is a directory, link, other or does not exist. If no arguments are passed I want to print out the usage statement.

All I have at this point is:

USAGE="usage:param.ksh arg1 arg2 arg3"

I am litterally drawing a blank. Any guidence would be much appreciated.

thanks,
diesel
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 2 of 11 (199 Views)

Re: Input Parameters

if [ $# -ne 1 ]
then
echo "USAGE=param.ksh arg1 arg2 arg3"
exit 1
fi

That gives you your usage. Then you can go on to test the args using $1 $2 $3, etc.


Pete

Pete
Please use plain text.
Acclaimed Contributor
Torsten.
Posts: 23,251
Registered: ‎10-02-2001
Message 3 of 11 (199 Views)

Re: Input Parameters

try

# file -h file1 file2

Is this, what you want?

HTH!

Hope this helps!
Regards
Torsten.

__________________________________________________

There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________

No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! star in the left column!   
Please use plain text.
Acclaimed Contributor
Torsten.
Posts: 23,251
Registered: ‎10-02-2001
Message 4 of 11 (199 Views)

Re: Input Parameters

just to test:

# file -h `ls`

Hope this helps!
Regards
Torsten.

__________________________________________________

There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________

No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! star in the left column!   
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 11 (199 Views)

Re: Input Parameters

Something like this should get you started:

#!/usr/bin/sh

typeset PROG=${0}
typeset -i STAT=0

if [[ ${#} -ge 1 ]]
then
while [[ ${#} -ge 1 ]]
do
typeset F=${1}
shift
echo "${F} \c"
if [[ -e "${F}" ]]
then
if [[ -f "${F}" ]]
then
echo "regular file"
fi
if [[ -d "${F}" ]]
then
echo "directory"
fi
if [[ -h "${F}" ]]
then
echo "symbolic link"
fi
else
echo "does not exist"
fi
done
else
echo "Usage: ${PROG} arg1 [arg2 ...]" >&2
STAT=255
fi
exit ${STAT}
If it ain't broke, I can fix that.
Please use plain text.
Honored Contributor
Geoff Wild
Posts: 7,168
Registered: ‎06-04-2001
Message 6 of 11 (199 Views)

Re: Input Parameters

I do it like Pete in a way:

if [ $# -lt 1 -o \( $# -gt 1 -a $# -lt 4 \) ]
then
echo "Usage:"
echo "usermem \"userid\""
echo "Example:"
echo "usermem gwild"
exit 1
fi
echo " "

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Please use plain text.
Esteemed Contributor
Gregory Fruth
Posts: 356
Registered: ‎05-14-1996
Message 7 of 11 (199 Views)

Re: Input Parameters

Use the [[ -X item ]] operators (where X
is a,b,c,etc.) described in the sh-posix
man page.

for item
in $*
do
[[ -f $item ]] && echo "$item is a file"
[[ -d $item ]] && echo "$item is a dir"
etc.
done
Please use plain text.
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 8 of 11 (199 Views)

Re: Input Parameters

Hey folks,

I appreciate all your rapid responses. All of you have something I can use so thank you so much.

-diesel
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 9 of 11 (199 Views)

Re: Input Parameters

Here's a start:

#!/usr/bin/sh
set -u
export PATH=/usr/bin
MYNAME=${0##*/}

if [ $# -lt 1 ]
then
echo "\nUsage: $MYNAME arg1 [...arg2...]\n"
exit
fi

# Now run through each parameter

for MYARG in $@
do
if [ -f $MYARG ]
then
echo "$MYARG is a file"
elif [ -d $MYARG ]
then
echo "$MYARG is a directory"
elif [ -h $MYARG ]
then
echo "$MYARG is a symbolic link"
else
echo "$MYARG does not exist or is some other filetype"
fi
shift
done

Of course you can adjust the code to meet your needs.
Please use plain text.
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 10 of 11 (199 Views)

Re: Input Parameters

Excellent Bill..thank you.

These forums are great!!

-diesel
Please use plain text.
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 11 of 11 (199 Views)

Re: Input Parameters

Thanks to all of you who responded. You have been most helpful and I really appreciate the sharing of knowledge.

Have a great day....
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation