The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed and parameters scotty_123 Shell Programming and Scripting 7 03-26-2007 01:22 AM
Max NO of parameters Shivdatta Shell Programming and Scripting 1 07-24-2006 05:11 AM
parameters aekaramg20 Shell Programming and Scripting 6 07-10-2006 10:15 PM
Need Parameters Help. james2006 Shell Programming and Scripting 3 06-08-2006 07:46 AM
tar parameters kmar UNIX for Advanced & Expert Users 4 10-23-2001 12:03 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
parameters

i'm supposed to come up with a script that
-accepts a directory as an optional command line parameter
-display an error message and terminates if more than one parameter is provided
-use the current directory if no parameter is provided
-displays an error message and terminates if the provided parameter is not a directory

this is what i came up with..

Code:
if [ $# -ge 2 ]; then
       echo "Too many parameters. Usage: file-info [directory]"
       exit
fi

if [ -d $1 ]; then
       echo "Processing `pwd`: "
       cd $1
       ls -l
       cd
   else
       echo "$1 is not a valid directory... terminating..."
fi
my question is, how do i use the current directory if no parameter is provided?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-13-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,056
Code:
if [ -d "$1" ]; then
  cd "$1"
else
  echo "$0: $1 is not a directory" >&2
  exit 255
fi
ls -l
Reply With Quote
  #3 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
I tried that and it still doesn't work.
Reply With Quote
  #4 (permalink)  
Old 04-13-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 458
Quote:
Originally Posted by jaay View Post
Code:
if [ $# -ge 2 ]; then
       echo "Too many parameters. Usage: file-info [directory]"
       exit
fi
my question is, how do i use the current directory if no parameter is provided?
If no argument is provided, set the first argument to current directory.
Code:
if [ $# -ge 2 ]; then
       echo "Too many parameters. Usage: file-info [directory]"
       exit
elif [ $# -eq 0 ];then set `pwd`;
fi
Reply With Quote
  #5 (permalink)  
Old 04-14-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
Thanks! that helped me alot!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0