Issues while trying to run a shell script using the command sh <filename.prog>


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues while trying to run a shell script using the command sh <filename.prog>
# 1  
Old 09-22-2016
Issues while trying to run a shell script using the command sh <filename.prog>

Hi,

I'm facing issues while trying to run a sample program on Linux.
If I try to run the script using the command "sh <filename.prog>", it doesn't work. But, if I try to execute it using the command "ksh <filename.prog>", it works fine.

Even ". ./filename.prog" works fine.

Can you please help in fixing this and make the file execute using the command "sh <filename.prog>".

Code:

Code:
#!/bin/sh
  
 printf "%s\n" "set pages 0 serverout on feed off" \
          "SELECT sysdate from dual;" \
           |sqlplus -s username/password \
           |{ read var1; }
echo "date is " $var1


Last edited by Corona688; 09-22-2016 at 01:45 PM.. Reason: Adding code .
# 2  
Old 09-22-2016
Use code tags for code please, the Image button.

This particular script will only work in ksh because of the way its constructed. It puts read after a pipe, which is something you can do in ksh but not a vanilla bourne shell because of the opposite order they create subshells.

You could use $( ) braces instead of read here, which ought to work in both:

Code:
DATE=$(printf "%s\n" "set pages 0 serverout on feed off" \
          "SELECT sysdate from dual;" \
           |sqlplus -s username/password )
echo "date is $DATE"

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-22-2016
Thanks for your response Corona. We have a similar code in almost 70+ programs and it would be a little tough to change it every where. Would it be a good idea to make the "sh" present under "/bin" to point to "ksh" i.e. creating a soft link for sh to point to ksh and try running it?
# 4  
Old 09-22-2016
I'd be wary of doing that. The difference you've just shown can as easily break programs as fix them, changing it globally could have wider problems, and #!/bin/sh is used by all sorts of system things.

Might it be easier to change the #!/bin/sh part to #!/bin/ksh in those 70+ programs?
# 5  
Old 09-22-2016
I tried changing the first line from #!/bin/sh to #!/bin/ksh, but it didn't work.
It still doesn't work when I try to execute the script as "sh <filename.prog>"
# 6  
Old 09-22-2016
That's because by running 'sh programname' you're asking sh to run that file. The #!/bin/ksh line only gets used when you do ./file.sh.

If they have to work in plain sh, you'll have to update them, they were definitely written for ksh.
These 2 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 09-22-2016
Thank you Corona.
This User Gave Thanks to venkatesh17 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Perl program to run a Shell script issues...

Hi all, I have the following Perl script which is intended to run a Shell script and generate some logging for the purposes of tracking weather or not the script ran. I get an error, of course, since I don't know what I'm doing really. Here is the code: #!/opt/perl/bin/perl -w ... (14 Replies)
Discussion started by: zixzix01
14 Replies

3. Shell Programming and Scripting

Help me!how to run autosys command in UNIX shell script

Hi all, Here is my scenario.. i need to get dates from an existing autosys calendar and compare it with current date with in a unix shell script. Please help me out and give me an approach to handle this....... the general autosys calendar command used is autocal_asc ,but this is... (1 Reply)
Discussion started by: shrik12345
1 Replies

4. Shell Programming and Scripting

run command in a script shell

Hello, Please i'd like to run command in a script shell , how can i do ? here my commands : cd blcr-build // run command in this rep sudo insmod ./blcr_imports/kbuild/blcr_imports.ko //root sudo insmod ./cr_module/kbuild/blcr.ko //root Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

5. Shell Programming and Scripting

Shell script to run command + compare values and post a warning message.

Hi all, I am trying to create shell script to run command then compare values with rule set for every 5 seconds and post a warning message if the compared value meet the rules. -the script is related to Oracle/Sun product messaging server 6.2 1) command that need to be run to gather... (2 Replies)
Discussion started by: Mr_47
2 Replies

6. Shell Programming and Scripting

Execute a shell script after a particular command is run

Hi, I need to run a script whenever the Cron file is modified. The requirement is whenever a user modifies the cron file, the script should run automatically. Can you please provide your inputs ? (5 Replies)
Discussion started by: harneet2004us
5 Replies

7. Shell Programming and Scripting

how to call java prog from shell script

I have a java program to validate a XML file. I want to call this java program in a shell script which will be registered as concurrent program in oracle apps. Can anyone please let me know the step by step appraoch required to call java program in shell script like ....intial steps... (1 Reply)
Discussion started by: kcp_pavan
1 Replies

8. Programming

how to run prog bet to break points

Hi, I have set two break points at 500 and 572 lines respectively. after running prog using (gdb) run i m on the line 500 but how two go to second breakpoints ie line 572 . when i m giving (gdb) run it is asking again to run from starting lines . (1 Reply)
Discussion started by: useless79
1 Replies

9. Shell Programming and Scripting

shell script prog help

How to write a program to store the list of ordinary files and the directory files in the specified directory in two files called dir.dat and ord.dat. Then print the largest directory and largest file? (1 Reply)
Discussion started by: rameshparsa
1 Replies

10. Shell Programming and Scripting

script to run shell command and insert results to existing xml file

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will 1. execute 'df -k' and return the volume names with specific text 2. surround each line of the above results in opening and closing xml tags 3. insert the results of step... (5 Replies)
Discussion started by: littlejon
5 Replies
Login or Register to Ask a Question