Need to call tcl function from other file !!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to call tcl function from other file !!
# 1  
Old 10-21-2015
Need to call tcl function from other file !!

Hi,

Can a function written in tcl in some other file be called in unix scripts ?
Like this ?

Code:
This is my code now---
shell.sh:
      #!/bin/bash
      tclsh snmpv2-conf-sam.tcl $SERVER $NODESYSIP $SPASSWD

but i need this in a different way like , without having a .tcl file i want the tcl file content to be in a function and to be called in the unix script.

Shell.sh:
     #!/bin/bash
     snmpv2 $SERVER $NODESYSIP $SPASSWD

     snmpv2()
     expect {
    password: {
        send "$nodePass\r"
        exp_continue
    } "yes/no)?" {
        send "yes\r"
        exp_continue
    }
    "# "
    }

Can this be done ?
# 2  
Old 10-21-2015
As a program maybe... so you could have the following at the front of your .tcl script and rename that file to just "snmpv2" and make sure it's on your PATH and executable.

Code:
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

This doesn't get rid of tcl, just embeds the tcl script into a shell wrapper. Your function would be making a call to execute a "program" in this case which is essentially a shell script which is a wrapper around tcl.

Just some ideas...

Tcl (expect) is a wondrous thing... ever thought to doing whatever you wanted to do using tcl? And there are expect alternatives out there.
# 3  
Old 10-21-2015
Hi.

I'm not sure about what is being asked, but here is a template that I often use for tcl code:
Code:
#!/usr/bin/env tclsh

# @(#) hi     Demonstrate tclsh feature.

puts stdout ""
set version [ info tclversion ]
set message " Hello, world from tclsh ($version)"
puts stdout $message
set message "$auto_path"
puts stdout " auto_path: $message"

set my_library ""
set my_library "$env(HOME)/lib/tcl"
lappend auto_path $my_library
if { ! [ auto_load Debug ] } then {
  # Define Debug procedures as null.
  puts stderr " (Hiding debugging procedures.)"
  proc Debug {x} {}
  proc DebugOn {} {}
  proc DebugOff {} {}
}

puts stdout ""
if { $argc == 0 } then {
  puts stdout " No command line parameters provided."
  set file "stdin"
} else {
  set i 0
  puts stdout " Command line arguments:"
  foreach arg $argv {
    set m1 " $i $arg"
    incr i
    set m2 ""
    if { [ file exists $arg ] } then {
      set m2 [ join [ list "(" [ file type $arg ] ")" ] ]
    }
    puts stdout "$m1 $m2"
	set file $arg
  }
}

puts ""
puts " Debugger validation:"
DebugOn 
Debug { Hello, debugger. }
DebugOff
Debug { Goodbye, debugger. }

# Copy from file if it exists.
puts stdout ""
puts stdout " Attempting copy from last argument:"
if { ! [string equal $file "stdin"] } {
  if { [file exists $file] } {
  puts " Opening and reading from file $file"
  set input [open $file "r"]
  } else {
    puts " No such file $file available for read, exiting."
	exit 0
  }
} else {
  puts " Reading from stdin"
  set input $file
}
while { [gets $input line] >= 0} {
  puts $line
}

exit 0

producing:
Code:
$ ./hi a b c

 Hello, world from tclsh (8.4)
 auto_path: /usr/share/tcltk/tcl8.4 /usr/lib /usr/local/lib/tcltk /usr/local/share/tcltk /usr/lib/tcltk /usr/share/tcltk

 Command line arguments:
 0 a 
 1 b 
 2 c 

 Debugger validation:
 Hello, debugger. 

 Attempting copy from last argument:
 No such file c available for read, exiting.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

2. Shell Programming and Scripting

How to Call a Function

Hi I have created a function in a Shell Script test.sh function fnTest() { echo "My first Method } I have called this function in my test.sh cat abc.txt | grep "test" echo " test" fnTest But while running the shell script i got the following error: ... (2 Replies)
Discussion started by: nanthagopal
2 Replies

3. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. Shell Programming and Scripting

Function call

Hi foiks i am unable to find what is wrong in my code mu functionality is to exit from shell when i give 99 but it is not calling function ext Could you please correct me. read option if ; then ext else echo "out" fi function ext { echo "tested 99 and exit... (12 Replies)
Discussion started by: kojo
12 Replies

5. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

6. Shell Programming and Scripting

Function Call

Hi, I have a string corresponding to a function. How I can call that function without if statement? Thanks in advance. (4 Replies)
Discussion started by: Zaxon
4 Replies

7. Shell Programming and Scripting

shell script equivalent for tcl function

Hello, I need experts help in converting the below tcl function to korn shell function equivalent. proc lsNetMaskToBits {mask} { set dw ; # Top N bits set set dw 0x ; # Make sure it's hexadecimal, else XOR fails. puts "lsNetMaskToBits dw $dw" set dw ; # Complement => low 32-N bits... (1 Reply)
Discussion started by: JackMelson
1 Replies

8. Shell Programming and Scripting

function call

hi, can any one help me to correct this function call. awk -F "," '{ {first=$1; sec=$2; tro=$3;quat=$4 } if (tro == "") { $3 = search "$file2" "$first" "$file3" {print $1","$2","$3","$4} } else {print $1","$2","$3 $4}}' $file1 > $file search () { (2 Replies)
Discussion started by: kamel.seg
2 Replies

9. Shell Programming and Scripting

call function

I have a function check_ok in my abc.sh. which return me 1 or 0 . I want to call this fuction through other shell script. this shell also send two parameter to calling function. Can you please tell me how. I am very new in unix. #!/bin/bash date_equal() { sqlplus -silent... (4 Replies)
Discussion started by: Jamil Qadir
4 Replies

10. Shell Programming and Scripting

function call

can I call a function in bash script just as in C++ while do function() done function() thanks, Steffen (3 Replies)
Discussion started by: forever_49ers
3 Replies
Login or Register to Ask a Question