Link to home
Start Free TrialLog in
Avatar of seremaz
seremaz

asked on

Simple shellscript help please - reading parameters and checking whether the first is a directory

Ok, my objective is as follows:

Write a fully commented bourne shell script program that outputs how many parameters were given on the command line, if there is more than one parameter given the program should report to the user if the first parameter exists as a directory or not.

this is the shellscript so far:

#!/bin/sh

read input
echo "The number of parameters you entered was $#"

if [ -d $1 ]
then
echo "The first parameter you entered is a directory"
else
echo "The first parameter you entered is not a directory"
fi

Problem: when i run the shellscript program if always gives me "0" as the number of entered parameters, and always says "the first parameter you entered is a directory" even when the directory doesn't exist, so clearly this isn't working,

any help on this matter appreciated, thanks alot, seremaz
Avatar of avizit
avizit

>>>> read input

since you are reading from command line arguments you dont need that line

comment out that line and your script should work fine
Avatar of seremaz

ASKER

er, if i omit 'read input' then the program runs without any input from the user, so how can it find out how many parameters there are, and whether the first parameter is a directory, if you dont give the program any parameters? i used read input as thats the only way i could find of actually entering parameters for the program to read
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seremaz

ASKER

ahhhhh! thankyou, in that case it works just as i want it to, perfect