Question about running script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about running script.
# 1  
Old 01-09-2019
Question about running script.

Hi Gurus,

I have problem to run script.
I can run script with command
Code:
 ksh script_name.ksh

but if i run script as
Code:
 ./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.
# 2  
Old 01-09-2019
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.
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-09-2019
Quote:
Originally Posted by vgersh99
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.
thanks vgersh99 for you responding.
when running script with ./script_name.ksh, it doesn't give anything.
initial script like below
Code:
 #!/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?
Code:
#!/bin/ksh 
cd $HOME
. .profile
if (( $# < 1 ))
 then
echo "Invalid parameter(s): Usage: $0 par1"
 exit -1
fi

# 4  
Old 01-09-2019
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.
# 5  
Old 01-09-2019
Quote:
Originally Posted by green_k
it works fine. could you please let me know why I can not use . ${HOME}/.profile in the script?
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:

Code:
#! /bin/ksh
echo $#
exit

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

Code:
./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:

Code:
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:

Code:
#!/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
This User Gave Thanks to bakunin For This Post:
# 6  
Old 01-10-2019
Quote:
Originally Posted by bakunin
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:

Code:
#! /bin/ksh
echo $#
exit

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

Code:
./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:

Code:
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:

Code:
#!/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
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 ---

Quote:
Originally Posted by bakunin
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:

Code:
#! /bin/ksh
echo $#
exit

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

Code:
./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:

Code:
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:

Code:
#!/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
question resolved, don't know how to delete this post

Last edited by green_k; 01-10-2019 at 01:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why script is running sometimes and not running sometimes?

Hi, I have a script which does couple of database connection and run some SELECT queries to get some output in the file. I am surprised to see :eek: that when i run my script some times it gives the desired out put and sometimes it shows some error :confused: . Suppose if i execute it say... (3 Replies)
Discussion started by: Sharma331
3 Replies

2. Linux

Question Regarding mounting a drive and running a software

Hello; Whenever I reboot a system which one takes precedence; mounting the drives or running the init.d scripts Here is my situation: Every time I boot my system, I need to mount a RAID10 drive and run a software whose config files are on the RAID10 drive I want the software to only start... (1 Reply)
Discussion started by: ramky79
1 Replies

3. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

Quick question about finding the PID of long-running processes

The end result that I'd like is to terminate any process on my ps -u username list that extends beyond 20 minutes. I know for a fact that this process will be named l.exe, but I don't know the number in between and I won't know the PID. Is there a way to use grep or pidof to do this task every 20... (2 Replies)
Discussion started by: Bolanok
2 Replies

6. Linux

running java program question

hi, i have just written a simple hello world java program in my linux server, installed jdk ... and tried to compile and run it and it gave me some errors. please details below: # cat HelloWorld.java import java.util.*; import java.io.*; public class HelloWorld { public static void... (4 Replies)
Discussion started by: k2k
4 Replies

7. AIX

Question on background running job

Guys, We use AIX 5.3 at our work place. I only in my team have a strange problem of not able run jobs background. Other colleagues are able to run without any problem. Once I kick off background job using nohup and & command, It immediately stops. The following error I get when I run. ... (2 Replies)
Discussion started by: anandsbr
2 Replies

8. Solaris

Running from Shell Vs running from RC script

Hi, i have a script which need to do behave differently when run as a startup process from init.d/ rc2.d script and when run manually from shell. How do i distinguish whether my script is run by init process or by shell?? Will the command /proc/$$/psinfo | grep "myscript" work well???... (2 Replies)
Discussion started by: vickylife
2 Replies

9. UNIX for Dummies Questions & Answers

Running a script in cron question

There is this script I'd like to put into cron, but it asks for date verification. It'll prompt you to press enter to continue. Usually, 100% of the time the dates are ok, so is there a way to run this script in cron and bypass the "enter" prompt? (3 Replies)
Discussion started by: NycUnxer
3 Replies

10. Programming

question with running dbx

If your program unfortunately takes a LONG time to finish running in dbx and you find yourself hitting Ctrl+C or Ctrl+Z to stop the running, does it produce any adverse effects at all ? The reason I am asking is because my program is in an infinite loop state (which leads to the huge delay in... (1 Reply)
Discussion started by: JamesGoh
1 Replies
Login or Register to Ask a Question