Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Korn Shell - Checking parameters

Posted on 2006-06-13
9
Medium Priority
?
992 Views
Last Modified: 2013-12-26
I'm writing a script that takes a directory name as a parameter

the first thing it needs to do is make sure we only have one parameter.
Then it needs to ensure that the one parameter is actually a directory.

here was my attempt at coding this:

##########################

# Check to see if the parameter exists
if ($# <> 1)
then
         echo "You must send one parameter - path to directory to rename files."
       exit
fi

TARGET_DIR=$1

# Check to see that the parameter is a valid directory
if (test -d $TARGET_DIR) then
  echo $TARGET_DIR is a directory
else
  echo $TARGET_DIR is not a directory
fi

##########################


Here is what I get for output:

[odadmin] shodms20:/home/odadmin) ./renameFailed.ksh test
./renameFailed.ksh[17]: 1: 0403-006 Execute permission denied.
test is not a directory

This is line 17:

if ($# <> 1)

Why is it trying to Execute here?

What do I have to do to simply check the number of params without executing anything?
0
Comment
Question by:aflat362
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 300 total points
ID: 16896740

change these lines:

if [ $# != 1 ]
then
...etc...
if [ -d $TARGET_DIR ]
then
  echo ...etc...
0
 
LVL 3

Author Comment

by:aflat362
ID: 16897268
Didn't work:

[odadmin] shodms20:/home/odadmin) ./renameFailed.ksh
./renameFailed.ksh[17]: [0:  not found.
./renameFailed.ksh[26]: 0403-057 Syntax error at line 29 : `else' is not expected.


Here's the code with updates:

# Check to see if the parameter exists
if [$# != 1]
then
         echo "You must send one parameter - path to directory to rename files."
       exit
fi

TARGET_DIR=$1

# Check to see that the parameter is a valid directory
if [-d $TARGET_DIR] then
  echo $TARGET_DIR is a directory
else
  echo $TARGET_DIR is not a directory
fi



0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16897307
>  Didn't work: ...

You need whitespace around [ and ] ( as MikeOM_DBA showed ).



0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
ID: 16897337
The reason you need whitespace around [ is that it is not a syntactic delimiter, it is actually a command:

% ls -l /bin
total 8824
-r-xr-xr-x   2 root  wheel      18104 Mar 20  2005 [
-rwxr-xr-x   1 root  wheel     581636 Mar 20  2005 bash
...



0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16901813
Agree with Brett
U need to put spaces always after [ and before ]

alternative is to use the test command

if test $# != 1
if test -d $TARGET_DIR

But MikeOM_DBA has already supplied you the answer, and it is correct

0
 
LVL 3

Author Comment

by:aflat362
ID: 16901922
Getting Closer.

Running program with no params worked.

Running program with 1 param did not:

[odadmin] shodms20:/home/odadmin) ./renameFailed.ksh test
./renameFailed.ksh[26]: 0403-057 Syntax error at line 29 : `else' is not expected.

####
#Code:

if [ $# != 1 ]
then
         echo "You must send one parameter - path to directory to rename files."
       exit
fi

TARGET_DIR=$1

# Check to see that the parameter is a valid directory
if [ -d $TARGET_DIR ] then
  echo "$TARGET_DIR is a directory"
else
  echo "$TARGET_DIR is not a directory"
fi
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 100 total points
ID: 16901968
then

has to be on a new line

0
 
LVL 3

Author Comment

by:aflat362
ID: 16901998
Thanks all.

Strange that [ and ] are commands.

Have any good reference that explains it?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16902108
> Strange that [ and ] are commands.
only [ ist a command (usually a hardlink to test)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 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