Calling subscript with sh vs. ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Calling subscript with sh vs. ksh
# 1  
Old 12-05-2017
Calling subscript with sh vs. ksh

I ran into an issue today that I was able to resolve, but I don't quite understand why the resolution worked. I'm wondering if anyone can help me make sense of it.

I have a "kicker" script that calls 4 subscripts on a RHEL 6.9 server. It calls the scripts in sequence checking for a 0 exit code from each before running the next.

Both the kicker script and each of the subscripts were written in kornshell and all start with #!/bin/ksh as the first line.

Code:
 #!/bin/ksh 
 sh /opt/scripts/script1.ksh
 rc=$?
 if [[ ${rc} -gt 0 ]]; then
   exit
fi
sh /opt/scripts/script2.ksh
 etc.

This worked fine and as expected when running it from the command line, but generated syntax errors from the subscripts when run via a scheduler (Control-M). The syntax errors were like the following:

Code:
 /opt/scripts/script2.ksh: line 252: [: -eq: unary operator expected
 /opt/scripts/script2.ksh: command substitution: line 288: unexpected EOF while looking for matching `''
 /opt/scripts/script2.ksh: command substitution: line 289: syntax error: unexpected end of file
 /opt/scripts/script2.ksh: command substitution: line 291: unexpected EOF while looking for matching `''
 /opt/scripts/script2.ksh: command substitution: line 292: syntax error: unexpected end of file

The errors made me suspect that it was running using the wrong shell, so I changed the kicker script to call the subscripts using "ksh /opt/scripts/script2.ksh" instead of "sh /opt/scripts/script2.ksh".

The next time the job ran, it ran successfully (both from the command line and through the job scheduler.)

So, while I'm glad my issue is fixed, I'm trying to understand why what I did fixed it.

script2.ksh
has #!/bin/ksh at the top. How does that interact with invoking the script using "sh script" vs "ksh script"? Does one override the other? Am I barking up the wrong tree entirely?

Thanks!

Last edited by rbatte1; 12-06-2017 at 05:56 AM..
# 2  
Old 12-05-2017
Hi,
The line magic number is not considered when the script is calling with a particular shell.

And, in the case of sh, errors seems to be a variable that is empty.

Regards.
# 3  
Old 12-06-2017
Also, since the first line has a space in front of the shebang on the first line, it would not work even if you call the script without specifying a shell when calling the script. The shebang only works if the first two bytes of the file are #!

If you want to call the shell without using the shebang, you need to use the same shell as part of the command to use the script as argument, which in this case is ksh for all scripts. ksh is a different shell than sh.

Last edited by Scrutinizer; 12-08-2017 at 08:58 AM..
# 4  
Old 12-06-2017
Quote:
Originally Posted by disedorgue
Hi,
The line magic number is not considered when the script is calling with a particular shell.

And, in the case of sh, errors seems to be a variable that is empty.

Regards.
Thank you for the reply. That is a useful piece of the puzzle. So in this case, when I was calling script2 with the sh it was ignoring the #!/bin/ksh and using sh to run it.

I'm still unclear why it worked correctly when executing the kicker script from the command line, but not through the scheduler. Both ways it would be explicitly calling the subscripts using sh. I would have expected it to fail in both cases. I think there must be something else at play besides the difference between ksh and sh. I agree that the errors are due to the variable it's trying to compare being empty, but not sure why the variable would be empty when running it with sh through the scheduler, but not with sh from the command line and not with ksh from the scheduler. At this point it's working, so I'll just move on. Smilie

Thanks again!

Oh - and to Scrutinizer, thank you also for the reply. In the actual script there isn't the space before the shebang, I must have inadvertently added that while copying and pasting to the post.

Last edited by derndingle; 12-06-2017 at 03:36 PM..
# 5  
Old 12-06-2017
Quote:
Originally Posted by derndingle
I'm still unclear why it worked correctly when executing the kicker script from the command line, but not through the scheduler.
Different PATH value causes it to find a different sh, perhaps?
# 6  
Old 12-06-2017
Hi derndingle...
It might be worth using this test site:

ShellCheck – shell script analysis tool

It can cater for sh, dash, bash, ksh and perhaps others now.

'dash' is as near as dammit pure 'sh' and will pluck out the vast majority of coding errors if 'sh' is what you really want.

The errors in your scripts show in a separate window and are very verbose...

Bazza...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling Oracle stored procedure from ksh script

Friends, I'm newbie with ksh so wanting some help.... 1. I'm trying to call oracle stored procedure from ksh script by taking variable value from runtime, feed into script and execute procedure. 2. Put name1 and name2 value from script run replacing $3 & $4 I'm trying to put name1 in... (4 Replies)
Discussion started by: homer4all
4 Replies

2. UNIX for Dummies Questions & Answers

Calling commands with ksh

Hi, I am not able to run below command on linux, it however works on solaris. If anyone knows the reason and a solution for it can you please let me know ? Linux ----- $> ksh 'echo hi' ksh: echo hi: No such file or directory $> which ksh /usr/bin/ksh Solaris ------ $> ksh 'echo... (2 Replies)
Discussion started by: krishnaux
2 Replies

3. Shell Programming and Scripting

Calling Function in KSH

I have a script with 2 functions 1) show_menu 2) create Ths show_menu function works fine....... Sort of.... When I select option 2 of the menu the code does a few commands and then calls another function called create. It's at this point that I get "create: not found"..... However,... (2 Replies)
Discussion started by: hxman
2 Replies

4. Shell Programming and Scripting

calling jar file from ksh

Hi, I have a below command in ksh .... $JAVA_HOME/bin/java -cp "/usr/orabase/product/10.2.0/lib:/usr/java/javajar/common/sched.jar:/usr/orabase/product/10.2.0/jdbc/lib/ojdbc14_g.jar:/usr/orabase/product/10.2.0/jdbc/lib/classes12.jar" com.abrt.smart.Smart I want to know where the... (1 Reply)
Discussion started by: PRKS
1 Replies

5. Shell Programming and Scripting

calling two procedures from one ksh ???

Hi to all, This is first posting in this forum. I quite new to this KSH....i guess i ll have some fun...:) Well, I have two indivdual korn shell scripts. i m calling PL/SQL stored procedure from korn shell script. It works absolutely fine. Once its completed the log is updated and data is... (5 Replies)
Discussion started by: shashi369
5 Replies

6. Shell Programming and Scripting

calling expect script in ksh is failing via cron

I'm calling an expect script via a ksh script in cron and it is failing. The script runs fine if i run it manually. Does anyone know if it is an issue with compatibilty and if there is a way around it? (2 Replies)
Discussion started by: bhatia
2 Replies

7. Shell Programming and Scripting

Get return value from PERL script calling from KSH

All: I am calling a PERL script from KSH. I need specific codes to be returned by the PERL Script. For ex: Ksh ----- result=`test.pl $FILE` My idea is to get the value of result from the test.pl, by specifically making the test.pl to print the return code. Since I had some other print... (1 Reply)
Discussion started by: ucbus
1 Replies

8. Shell Programming and Scripting

calling csh script from ksh shell

hi, I have a csh script, which has setenv X xyz etc My shell is korn Is there some way I can "source" this to have the variables in my current korn shell? thanks (3 Replies)
Discussion started by: JamesByars
3 Replies

9. Shell Programming and Scripting

calling a PL/SQL stored procedure from KSH

Hi I have a stored procedure which should be called from KSH. Could ayone please help me with this. Thanks (1 Reply)
Discussion started by: BlAhEr
1 Replies

10. Shell Programming and Scripting

Calling subscript but sleep halts the main script

Ok, I have written a main script which checks a directory contents every 30 secs then sleeps. The subscript does a usermod, if the user is logged on, it sleeps for 30 secs and then trys again over and over again. Here's the problem. when the subscript is called ./subscript.sh or exec... (1 Reply)
Discussion started by: doublejz
1 Replies
Login or Register to Ask a Question