Solved

Korn Shell - Checking parameters

Posted on 2006-06-13
9
931 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

757 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

19 Experts available now in Live!

Get 1:1 Help Now