Should I run scripts like sh /absolute_path/to/scriptname.sh or just /absolute_path/to/scriptname.sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Should I run scripts like sh /absolute_path/to/scriptname.sh or just /absolute_path/to/scriptname.sh
# 1  
Old 03-29-2017
Should I run scripts like sh /absolute_path/to/scriptname.sh or just /absolute_path/to/scriptname.sh

Shell : Linux Bash
OS version : RHEL 6.X, Oracle Linux 6.X

Question1. To execute shell scripts, sometimes I see Linux admins placing sh command before shell scripts like below

Code:
sh /home/appadmin/sync_pending_orders.sh

But, I execute shell scripts like below ie. Just the absolute path

Code:
/home/appadmin/sync_pending_orders.sh

Which is recommended ?

Question2. Similarly, in crontab entries , I see sh being placed before shell script names like below. Is this sh 'prefixing' really needed ?

Code:
[appadmin@ktsappwprg01 ~]$ crontab -l
35 2 * * * /bin/sh /home/appadmin/sync_orders.sh >> /home/appadmin/log/ind_critical.log

25 3 * * * /bin/sh /home/appadmin/sync_pending_orders.sh >> /home/appadmin/log/ind_crtical_2.log

# 2  
Old 03-29-2017
You prefix sh when you want a specific shell to be used to interpret the script and not the current shell of the current user, no matter if interactive or as crontab entry.

Last edited by cero; 03-29-2017 at 09:00 AM..
This User Gave Thanks to cero For This Post:
# 3  
Old 03-29-2017
Thank You Cero.
So, If I use shebang ( #!/bin/bash as the first line) in the script itself, I don't need to place sh before the script name to execute it.Right ?
# 4  
Old 03-29-2017
Quote:
Originally Posted by John K
Which is recommended ?
Both are valid ways of doing it. Look at a (properly written) scripts beginning and you will usually see:

Code:
#! /path/to/interpreter

... <rest of the script ...

This requests the system to first load /path/to/interpreter and then feed it the rest of the script as input. If your first line is #! /path/to/bash this interpreter will be bash and hence your script will be executed as bash-shellscript.

Every shell (make that: every shell i know of) also has the ability to accept a parameter on the command line, which denotes a list of commands which wll be in turn executed by the shell you have called:

Code:
/path/to/bash /some/file

will load /path/to/bash into memory and then - instead of initiating an interactive session - execute what is in /some/file one line after the other. (In principle there is no difference between scripts and commands entered at the commandline, a "script" is in fact just a list of commands in a file.)

The only difference between the two variants:

Code:
/path/to/shell /some/script.file
/some/script.file

is that in the second variant /some/script.file has to have the executable-flag set in order to be executed:

Code:
# ls -l
total 8
-rw-r--r--  1 bakunin users   86 Mar 28 15:49 will.not.execute
-rwxr-xr--  1 bakunin users   86 Mar 28 15:49 will.execute

in the first variant it is not necessary to have the x-flag set and in the above this:

Code:
# /path/to/shell /some/path/will.not.execute

will be processed as well. Therefore using this in crontab will allow you to execute something as script without having its x-flag set.

I leave it to you to decide if this is a desirable state of affairs. Personally i think it is not, but then, i have a certain way of doing things and you might come to different conclusions. There are more than one ways to skin a cat.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 03-29-2017
Basically there is no need to prefix the script with a shell - if you leave it out, your current shell will be used. But different shells use different syntax. So naming which shell to use ensures that the script is interpreted by the shell it was written for.
A shebang in a script does more or less the same what "prefixing" does: invoce the shell with the scriptname (+ arguments) as argument, so prefixing is not needed.
This User Gave Thanks to cero 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

Run scripts in background one by one

Hello all, Can someone help with one script? I have as example 3 scripts 1.sh sleep 60 & sleep 60 & sleep 10 & sleep 80 & sleep 60 & 2.sh sleep 40 & sleep 5 & sleep 10 & sleep 70 & sleep 60 & 3.sh (2 Replies)
Discussion started by: vikus
2 Replies

2. UNIX for Dummies Questions & Answers

Scripts can be run manually but couldn't run with cronjobs

I am from MQ/MB technology. My requirement is to display the queue manger and broker status on daily basis. If I manually run the script, it works fine and displays output. But when I have scheduled the same using cronjobs it shows only the queue manger status and not the broker status. Can... (3 Replies)
Discussion started by: Anusha M
3 Replies

3. Shell Programming and Scripting

Search jobname via scriptname in autosys

Hi I was wondering if there was any command with autorep that can be used to check what the jobname is by givin scriptname that is defined in the JIL of the job (1 Reply)
Discussion started by: sidnow
1 Replies

4. Solaris

Need to execute the script with script name wihtout using ./scriptname in Solaris 10

Hi, I am using solaris 10.Is there any way to execute the script with the scriptname wihtoug using ./scriptname?Also does it varies from shell to shell.I have scripts in bash,ksh,sh shells. Example:script.sh is the script name.I need to execute the script like this script.sh instead... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

5. Shell Programming and Scripting

how to run scripts....

I am going to run the scripts (filetest.sh) Its run with..... ./filetest.sh sh filetest.sh but not run with..... filetest.sh ( giviing error command not found) tell me the way how we can do it ? (6 Replies)
Discussion started by: ani83_pune
6 Replies

6. Shell Programming and Scripting

/bin/ksh: scriptname: not found. ???

I have started to write Korn scripts on the client's AIX 4.2 servers and there is this small problem that puzzles me. I code all my scripts the same way: - first line contains : #!/bin/ksh - I do this console command on every scripts: chmod +x scriptname But still, on some occasions,... (4 Replies)
Discussion started by: Browser_ice
4 Replies

7. AIX

To run scripts

How to run a script from any directory? Should i include any environment or wat should i start with in writing a script ? (1 Reply)
Discussion started by: rollthecoin
1 Replies

8. Solaris

bash: ./<scriptname>: No such file or directory

I have simply downloaded a file to my local folder, made one small change and uploaded it back, ever since it will not run the file. I have now uploaded the original, unchanged file, which still doesn;t work. Evertime it is run ot says bash: ./nrcardprint: No such file or directory The... (10 Replies)
Discussion started by: Heidi.Ebbs
10 Replies

9. UNIX for Dummies Questions & Answers

run script without ./scriptname

Hi everyone, I'm very new, I just wrote a script "Hello World" :) LOL How can I execute the script without the ./myscript. I want to just type "myscript" basically from anywhere without the " ./ ". Thanks a bunch, NinjaX (2 Replies)
Discussion started by: NinjaXman
2 Replies

10. Shell Programming and Scripting

echo Scriptname

I have several crontab scripts running which email me status. Some of these scripts perform identical tasks so I get a similar emails sent to myself. How do I get the script to print it's own path/filename so I can include it in the status updates without hardcoding trhe filename into the... (1 Reply)
Discussion started by: uXion
1 Replies
Login or Register to Ask a Question