Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using TCL/TK to change files under Linux

Status
Not open for further replies.

wlerner

Programmer
Jan 8, 2004
6
US
I would like to use TCL/TK to create a GUI that I can use to change the Hostname, IP Address, and MAC address of a Linux based system. Does anyone have any code to share that would help me do this?

Thanks!
 
How would you do it with BASH?

Bob Rashkin
rrashkin@csc.com
 
Are you asking me how to accomplish this in BASH or are you being sarcastic?

While it is possible to use BASH to do this, and I do have a script that calls SED and makes the change, I need this to run in a TK widget. I need the output directed into the widget after an .entry box is used to get the new address.

 
No, I wasn't being sarcastic (maybe assuming Bash was wrong). I don't know how to do the UNIX function you want (which files to change, what changes to make in them), if you told me that, I'm sure I could tell you a way to do it in Tcl.

Bob Rashkin
rrashkin@csc.com
 
Hi,

Thanks for the reply. I have a BASH script that will use SED to change the IP address of a host system by editing the /etc/sysconfig/networking/devices/ifcfg-eth0 file. The script merely removes the IPADDR='xx.xx.xx.xx' line and replaces it with whatever the user has input.

So really, I guess I am looking for a method in TCL/TK of substituting one line in a text file for another. I read a little on regexp and regsub, but I am unsure how to approach this. A small coding sample would go a long way in this instance.

Again everyone, thank you for all the assistance!
 
Okay. I think I can suggest something that might help. I would read in the whole file make a change, then write it out again, as follows.

Let's say the file name is "/a/b/c/abc.log"

set fid [open /a/b/c/abc.log r]
set linelist [split [read $fid] \n]
close $fid
#each line is an element of the list
#now search through the list for the trigger text
#and make the change
foreach line $linelist {
if {[string range $line 0 5]=="IPADDR"} {
set newstring "IPADDR=aaa.bbb.ccc.ddd"
} elseif {<more cases as needed>} {
} else {
set newstring $line
}
lappend newlist $line
}
set fid [open /a/b/c/abc.log w];# write it out new
foreach line $newlist {puts $fid $line}
close $fid


Bob Rashkin
rrashkin@csc.com
 
oops. that should be &quot;lappend newlist $newstring&quot;

sorry.

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top