Solved

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

Posted on 2004-08-22
4
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

737 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