Want to understand the meaning of the following line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to understand the meaning of the following line
# 1  
Old 05-24-2007
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  
Old 05-24-2007
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  
Old 05-24-2007
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  
Old 05-24-2007
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
# 5  
Old 05-24-2007
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.
# 6  
Old 05-24-2007
Yes, I know, but I never have '.' in my path...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

3. UNIX for Dummies Questions & Answers

What is meaning of given line?

Hello Guys, I was study some links, where I was unable to understand below line, please enhance me these code. (3 Replies)
Discussion started by: aaditya321
3 Replies

4. Shell Programming and Scripting

Meaning

Please let me know the meaning for the below statements in shell scripting. 1) exit -99 -------------------------------- 2) set prgdir = `pwd` set runFlag = runFlag:FALSE ------------------------------------- 3) if (-f $prgdir/maillst.eml) then set distEmail = `cat $prgdir/maillst.eml`... (1 Reply)
Discussion started by: lg123
1 Replies

5. Shell Programming and Scripting

Help to understand multi-line

This code cat <<'EOF' Red Green Blue EOF gives output to screen Red Green Blue Same output as echo "Red" echo "Green" echo "Blue" (2 Replies)
Discussion started by: Jotne
2 Replies

6. UNIX for Dummies Questions & Answers

meaning of <<!

Hi all, I wanna know the meaning of the last word "<<! " sudo su - user <<! please help on this !!!! (1 Reply)
Discussion started by: sudharson
1 Replies

7. Shell Programming and Scripting

meaning of !*

can someone please tell what !* means in shell syntax. Regards, (3 Replies)
Discussion started by: busyboy
3 Replies

8. What is on Your Mind?

Line 2238 Unix V6 Comment: You are not expected to understand this.

Here is the famous line 2238 of Unix V6 which is part of some of the most delicate parts of the kernel, context switching. This comment received huge publicity and just may be the the most famous source code comment in computing history. 2230 /* 2231 * If the new process paused because it... (0 Replies)
Discussion started by: Neo
0 Replies

9. Shell Programming and Scripting

Meaning of this line in script

Can anyone explain me the meaning of line #2 in these lines of shell script: if ; then ${EXPR} " ${MACTIONS } " : ".* ${ACTION} " >/dev/null 2>&1 || die "$USAGE" else Sorry in case this is a trivial thing (I am not an expert in this). (3 Replies)
Discussion started by: radiatejava
3 Replies

10. Shell Programming and Scripting

what is the meaning here?

#!/bin/sh $ORACLE_HOME/bin/sqlplus -S $orauserid/$orapasswd@$oradb << _TMP alter session set nls_date_format = 'YYYYMMDD HH24:MI'; set linesize 100 set pagesize 400 ok the above is part of a script..i just wanna know what does sqlplus -S means?? as in why we need to insert the -S behind? (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question