Solved

Korn Shell - Checking parameters

Posted on 2006-06-13
9
982 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 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
MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

 
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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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