difference between sh filename.sh and . filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting difference between sh filename.sh and . filename
# 1  
Old 05-05-2011
difference between sh filename.sh and . filename

Hi, Gurus,
I feel sorry to ask this easy question, but to be honest, I really don't know.
I have a question about execute shell script.
I have script named as filename.sh with -rwxr-xr-x permission.
when I execute it with
Code:
. filename.sh

It works properly.
when I exec it with
Code:
sh filename.sh

It does not work.
SmilieSmilie

Any idea about this.

Thanks in advance
ken002
# 2  
Old 05-05-2011
Quote:
"It does not work"
If you tried, I don't think you could be more vague.

Based on such limited information the best I could suggest is that you didn't export one or more variables from wherever you are trying to run filename.sh from (sh filename.sh - not an issue when you source (with .) filename.sh).

Please try to be more descriptive than saying "It does not work".

Running it "sh ..." will run the script in a new sub-shell (sh - or whatever that point to (bash, ksh, dash, etc.)). Running it with . will source (effectively load) it into the current shell's environment.
This User Gave Thanks to Scott For This Post:
# 3  
Old 05-05-2011
Is this a Bourne or Korn (or whatever script)?
If the first line of your script does not call out a shell, it will run in the shell from which you are logged in as.
Using the . will use the current shell unless overridden by a
Code:
#!/usr/bin/ksh

statement on line #1.
Using sh <script> will force using Bourne shell unless overridden by line #1
# 4  
Old 05-05-2011
Quote:
Originally Posted by Topaz
Is this a Bourne or Korn (or whatever script)?
If the first line of your script does not call out a shell, it will run in the shell from which you are logged in as.
Using the . will use the current shell unless overridden by a
Code:
#!/usr/bin/ksh

statement on line #1.
Using sh <script> will force using Bourne shell unless overridden by line #1
Both methods you suggest render #!/usr/bin/ksh ineffectual.
# 5  
Old 05-05-2011
Quote:
Originally Posted by ken002
Hi, Gurus,
I feel sorry to ask this easy question, but to be honest, I really don't know.
I have a question about execute shell script.
I have script named as filename.sh with -rwxr-xr-x permission.
when I execute it with
Code:
. filename.sh

It works properly.
when I exec it with

. filename.sh executes filename.sh in the current shell.

"When bash is not in posix mode, the current directory is searched if no file is found in PATH."
Quote:
Code:
sh filename.sh

It does not work.

sh will search the directories in $PATH for the file, but it will not search the current directory.
This User Gave Thanks to cfajohnson For This Post:
# 6  
Old 05-05-2011
Thanks everybody,
my default shell
Code:
$ echo $SHELL
/usr/bin/bash

my script exactly same as

Code:
 # --------------------------------------------------------------------------
       #
       # SCRIPT NAME : env.sh
       # DESCRIPTION : set environment variables
       # CREATED     : 23 Nov 2010
       # UPDATED     : 05 Apr 2011
       #  --------------------------------------------------------------------------

ORACLE_SID=abc  
export ORACLE_SID
DB_USR=aaa       
export DB_USR
DB_PASSWD=bbb    
export DB_PASSWD

when I exec sh filename.sh it did not export these variables.

Smilie, Smilie,

Thanks in advance
ken002
# 7  
Old 05-05-2011
Just a detail i want to point out :
Note that with those command
Code:
. filename.sh

and
Code:
sh filename.sh

since filename.sh is an argument, it will be executed even if your filename.sh does not have the +x chmod.

---------- Post updated at 09:49 PM ---------- Previous update was at 09:46 PM ----------

... whereas simply running :
Code:
filename.sh

you may encounter a permission denied if you forgot to set the chmod +x on it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

2. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

3. UNIX for Dummies Questions & Answers

"tail -n 1 filename" error while "head -n 1 filename" is ok?

Hi all, I was wondering why tail -n 2 filename produce an error when I manage to do similar command on head -n 2 filename SunOS{type8code0}: tail -n 2 filename usage: tail ] tail ] (2 Replies)
Discussion started by: type8code0
2 Replies

4. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

5. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies

6. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

7. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies

8. Shell Programming and Scripting

how to get filename

I have a list of pathnames in file A.text then I want to get only filenames to B.text. For example, File A.text File B.text (should be) How to do it ? Thank in advance (5 Replies)
Discussion started by: unitipon
5 Replies

9. UNIX for Dummies Questions & Answers

shortcut for tar cvf - [filename] | gzip > [filename].tar.gz

i'd like to have an alias (or something similar) where i can type a command like "archive" and a filename and have it tar and gzip the file, so... $ archive filename results in filename.tar.gz...do i have to write a script to do this? (4 Replies)
Discussion started by: bcamp1973
4 Replies

10. Programming

Filename from fp or fd

Help!!!!!!!!!!!! Urgent...... How can i retrieve filename of a file associated with a FILE *fp or with a file descriptor???:confused: (2 Replies)
Discussion started by: ManishSaxena
2 Replies
Login or Register to Ask a Question