The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Code Understand ? varungupta UNIX for Advanced & Expert Users 2 05-02-2008 08:34 PM
Can't understand sar command shaan_dmp UNIX for Dummies Questions & Answers 1 10-23-2007 08:04 AM
can't understand this at all. severndigital UNIX for Dummies Questions & Answers 4 02-21-2007 12:21 PM
can't understand BoyArcher UNIX for Dummies Questions & Answers 5 03-14-2006 09:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-24-2007
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
Want to understand the meaning of the following line

HI All
Please find the code below from a script called test.sh

echo "Hello World"
. test_common.lib
get_info


in the file test_common.lib i have the following contents

get_info()
{
c_cnt=0;
cm="";
echo "Inside get_info"
}

when i run the script test.sh

i get a error message

test.sh: line 3: get_info: command not found

First of all i would like to know how this line behaves
"test_common.lib"


Regards
Dhanamurthy
  #2 (permalink)  
Old 05-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
. test_common.lib
Executes test_common.lib in the same shell so that you can access variables and functions defined in it.

What you are doing is correct. Check whether test_common.lib in test.sh directory contains definition of that function?
  #3 (permalink)  
Old 05-24-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
What shell are you using for this? I tried this with sh, ksh and bash, and it worked with all three. I had to make one modification though:
Code:
#!/usr/bin/bash

echo "Hello World"
. ./test_common.lib
get_info
Without the "./" it was giving a "./test.sh: test_common.lib: not found" error under sh and ksh.

As for the explanation of how the ". test_common.lib" line works, here's how:

When you run . test_common.lib, or in my case . ./test_common.lib, the statements in the test_common.lib file are processed inside the calling shell itself, unlike when you just run ./test_common.lib (this forks a seperate process). Because the calling shell processes these, any statements such as variable definitions, function definitions, etc are stored in the calling shell and are available for later use.

Hope that was clear (though it probably isn't).
  #4 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
Quote:
Originally Posted by blowtorch
What shell are you using for this? I tried this with sh, ksh and bash, and it worked with all three. I had to make one modification though:
Code:
#!/usr/bin/bash

echo "Hello World"
. ./test_common.lib
get_info
Without the "./" it was giving a "./test.sh: test_common.lib: not found" error under sh and ksh.
Works fine if "." is included in the PATH variable, for example :
Code:
PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:.
Jean-Pierre.
  #5 (permalink)  
Old 05-24-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Yes, I know, but I never have '.' in my path...
  #6 (permalink)  
Old 05-24-2007
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
The line
. test_common.lib though executes successfully in the script, the get_info function is not able to execute as the definition for get_info is not made available.
When i ran . ./test_common.lib.

It's proceeding further by calling the function.
I am using bash scripting.

Not sure of how this line behaves.

Thanks for your inputs.


REgards
Dhanamurthy
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0