bash - Write to a file from a shell script -
i have file myfile
has text this:
self.server('10.0.0.1', '00:00:00:00:00:01', 1) self.server('10.0.0.2', '00:00:00:00:00:01', 2)
i have written shell script write myfile
. script has variables defined:
hosts=100 servers=20 ......
i want modify line self.server('10.0.0.x', '00:00:00:00:00:0x', x)
in myfile
, write many times value of variable servers
, x ranges 1 value of servers(20 in case).
note: myfile
might have of entries. example if previous value of servers
4 , new value 6, 2 entries need added. also, if previous value 6 , new value 3, first 3 entries need removed.
can please guide how can achieve it?
thanks.
#!/bin/bash servers=20 ((i=1; i<=servers; i++));; printf "%s%02d%s\n" "self.server('10.0.0.$i', '00:00:00:00:00:" "$i" "', $i)" >> myfile done
run result:
self.server('10.0.0.1', '00:00:00:00:00:01', 1) self.server('10.0.0.2', '00:00:00:00:00:02', 2) self.server('10.0.0.3', '00:00:00:00:00:03', 3) self.server('10.0.0.4', '00:00:00:00:00:04', 4) self.server('10.0.0.5', '00:00:00:00:00:05', 5) self.server('10.0.0.6', '00:00:00:00:00:06', 6) self.server('10.0.0.7', '00:00:00:00:00:07', 7) self.server('10.0.0.8', '00:00:00:00:00:08', 8) self.server('10.0.0.9', '00:00:00:00:00:09', 9) self.server('10.0.0.10', '00:00:00:00:00:10', 10) self.server('10.0.0.11', '00:00:00:00:00:11', 11) self.server('10.0.0.12', '00:00:00:00:00:12', 12) self.server('10.0.0.13', '00:00:00:00:00:13', 13) self.server('10.0.0.14', '00:00:00:00:00:14', 14) self.server('10.0.0.15', '00:00:00:00:00:15', 15) self.server('10.0.0.16', '00:00:00:00:00:16', 16) self.server('10.0.0.17', '00:00:00:00:00:17', 17) self.server('10.0.0.18', '00:00:00:00:00:18', 18) self.server('10.0.0.19', '00:00:00:00:00:19', 19) self.server('10.0.0.20', '00:00:00:00:00:20', 20)
Comments
Post a Comment