Solved

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

Posted on 2004-08-22
4
267 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:seremaz
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11864212
>>>> 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
0
 

Author Comment

by:seremaz
ID: 11864221
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
0
 
LVL 11

Accepted Solution

by:
avizit earned 250 total points
ID: 11864237
oh that way you have to give the input on the command line

as in  if your script is named test.sh  you can run it as

./test.sh  <your input>  <enter key>
0
 

Author Comment

by:seremaz
ID: 11864253
ahhhhh! thankyou, in that case it works just as i want it to, perfect
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now