Solved

Korn Shell - Checking parameters

Posted on 2006-06-13
9
968 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 75 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
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!

 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 25 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 25 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

Technology Partners: 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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.

763 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