Script to write result to a file

Hello,

How can I run this script every 1 hour and save its result to result.txt

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

Regards
Shaan

You have two options:

  1. Schedule it in Crontab
00 * * * * /path/to/your/script/script.sh >> result.txt
  1. Write an infinite loop and call this command every 1 hour
while true
do
  ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' >> result.txt
  sleep 3600
done

I recommend to use 1st option.

1 Like

Thank you. This is how Crontab looks like and I have added the last line. Is this correct?

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
00 * * * * /usr/local/src/loop/testip.sh >> result.txt

---------- Post updated at 11:50 AM ---------- Previous update was at 11:23 AM ----------

A step forward, What I am trying to do is to capture the IP address discussed in the previous message, and append the IP to my config.sh. My config.sh file looks like this

webproxy -l 112.102.142.107 -s

I want the new IP to be replaced in place of 112.102.142.107

You can have more than one IP address as the result of above cmd pipe:

$ ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
10.1.1.1
192.168.2.254

, so make sure you know exactly what you are doing. Once you have your new ip in a variable, say, NEWIP, you can use

awk '/webproxy/ {sub(/...\....\....\..../, newip)}1' newip=$NEWIP  config.sh

. Some awk version do have an easier way to specify the replacement pattern using EREs. Redirect to new file and then rename.

1 Like

Thank you. The config file is bit long though. I have no clue how to replace 112.121.107.100 with new IP in two places.

webproxy -l 112.121.107.100 -s udp:127.0.0.1:9722 -F -L 501 -m 51001 -M 53000 -P 127.0.0.1 -B Start
htmproxy -E -n 1 -l 112.121.107.100 -l 127.0.0.1 -W start_et -f /usr/local/src/htmproxy-cfg/htmproxy-n.cfg -p 127.0.0.1

---------- Post updated at 10:44 PM ---------- Previous update was at 07:58 PM ----------

I did a /webproxy|htmproxy/ and it worked. But the problem is when the NEWIP is shorter. for eg: 111.111.1.111 it is not adding. It need to be all 3 numeric value to work. Is there solution?

---------- Post updated 12-29-12 at 07:44 AM ---------- Previous update was 12-28-12 at 10:44 PM ----------

Solved.. I added left zero padding awk code. Thank you RudiC and bipinajith

Pls show your solution. When EREs are available, the IP could be represented by sth like ([0-9]{1,3}\.){3}[0-9]{1,3}