1752778 Members
6308 Online
108789 Solutions
New Discussion юеВ

Re: Input Parameters

 
SOLVED
Go to solution
Diesel
Occasional Contributor

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
10 REPLIES 10
Pete Randall
Outstanding Contributor
Solution

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
Torsten.
Acclaimed Contributor

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! thumb below!   
Torsten.
Acclaimed Contributor

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! thumb below!   
A. Clay Stephenson
Acclaimed Contributor

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.
Geoff Wild
Honored Contributor

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.
Gregory Fruth
Esteemed Contributor

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
Diesel
Occasional Contributor

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
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin
Diesel
Occasional Contributor

Re: Input Parameters

Excellent Bill..thank you.

These forums are great!!

-diesel