Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1001
  • Last Modified:

Korn Shell - Checking parameters

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
aflat362
Asked:
aflat362
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
MikeOM_DBACommented:

change these lines:

if [ $# != 1 ]
then
...etc...
if [ -d $TARGET_DIR ]
then
  echo ...etc...
0
 
aflat362Author Commented:
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
 
brettmjohnsonCommented:
>  Didn't work: ...

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



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
brettmjohnsonCommented:
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
 
rockiroadsCommented:
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
 
aflat362Author Commented:
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
 
rockiroadsCommented:
then

has to be on a new line

0
 
aflat362Author Commented:
Thanks all.

Strange that [ and ] are commands.

Have any good reference that explains it?
0
 
ahoffmannCommented:
> Strange that [ and ] are commands.
only [ ist a command (usually a hardlink to test)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now