Question about running script.

Hi Gurus,

I have problem to run script.
I can run script with command

 ksh script_name.ksh 

but if i run script as

 ./script_name.ksh 

, it doesn't run. the permission for the script is: -rwxr-xr-x.
my server is SunOS 5.10 Generic_150400-48 sun4v sparc sun4v
do we need to setup something to run command as ./script_name.ksh?

thanks in advance.

when you say that it doesn't run , what does it mean? Any error messages? Anything get output?
Please post the first 10 lines of the script using code tags.

1 Like

thanks vgersh99 for you responding.
when running script with ./script_name.ksh, it doesn't give anything.
initial script like below

 #!/bin/ksh
. ${HOME}/.profile
if (( $# < 1 ))
 then
echo "Invalid parameter(s): Usage: $0 par1"
 exit -1
fi 

.
I changed to below, it works fine. could you please let me know why I can not use . ${HOME}/.profile in the script?

#!/bin/ksh 
cd $HOME
. .profile
if (( $# < 1 ))
 then
echo "Invalid parameter(s): Usage: $0 par1"
 exit -1
fi 

you had a leading space in the first line: #!/bin/ksh
The first line containing the path to shell interpreter to be used for the script should start with #! followed by a path to a shell interpreter.
Unless it's a copy/paste error when you posted.

That depends - among other things - on what is in ~/.profile . One conceivable cause could be: $# is the number of (commandline) parameters the current process got. i.e. Consider this script:

#! /bin/ksh
echo $#
exit

The following table shows what it would produce if called in the respective way:

./script         # 0
./script a b     # 2
./script "a b"   # 1
./script ""      # 1

etc.

You called your script without any parameter (as far as i can see) and so the test if (( $# < 1 )) should be true and the then-branch be executed. But inside your ~/profile script you could have a line:

set "X"

and because you dot-execute the script this makes changes to your current environment, by setting "$1" (the first positional argument) to "X" (notice that instead of "x" any other value would also be possible) - which in turn makes "$#" return 1, not 0 any more.

change your script the following way for debugging:

#!/bin/ksh
echo "before: $#"
. ${HOME}/.profile
echo "after: $#"
if (( $# < 1 )) ; then
     echo "Invalid parameter(s): Usage: $0 par1"
     exit -1
fi

and run again.

I hope this helps.

bakunin

1 Like

thanks bakunin and vgersh99. it is working now. I deleted the line . ${HOME}/.profile and re-type it. it works. before deleting, use cat -evt to check, there is no hiden characters. Not sure why.

thanks again for your valuable suggestion.

--- Post updated 01-10-19 at 12:47 PM ---

question resolved, don't know how to delete this post