Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

815 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

8 Experts available now in Live!

Get 1:1 Help Now