Solved

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

Posted on 2004-08-22
4
264 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

23 Experts available now in Live!

Get 1:1 Help Now