Script to write result to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to write result to a file
# 1  
Old 12-28-2012
Script to write result to a file

Hello,

How can I run this script every 1 hour and save its result to result.txt
Code:
ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

Regards
Shaan

Last edited by Franklin52; 12-28-2012 at 09:07 AM.. Reason: Please use code tags for data end code samples
# 2  
Old 12-28-2012
You have two options:

1. Schedule it in Crontab
Code:
00 * * * * /path/to/your/script/script.sh >> result.txt

2. Write an infinite loop and call this command every 1 hour
Code:
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.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-28-2012
Thank you. This is how Crontab looks like and I have added the last line. Is this correct?
Code:
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
Code:
webproxy -l 112.102.142.107 -s

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

Last edited by Franklin52; 12-28-2012 at 09:07 AM.. Reason: Please use code tags for data end code samples
# 4  
Old 12-28-2012
You can have more than one IP address as the result of above cmd pipe:
Code:
$ 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
Code:
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.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-28-2012
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
# 6  
Old 12-29-2012
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}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl and write custom result to file

I have the following script, for i in $(cat MyWebApps); do curl -u manager:tH1s1s4f3k3p4ssw0Rd http://10.10.10.10:7529/manager/jmxproxy/"?get=Catalina:type=Manager,context=/$i,host=localhost&att=activeSessions"; done Which gives me an output like this OK - Attribute get... (12 Replies)
Discussion started by: charli1
12 Replies

2. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

3. Shell Programming and Scripting

Compare 2 file and write result

Hi friends...I have 2 files, file1.txt and reference.txt I could able to find difference using diff and following command awk 'NR == FNR { A=1; next } !A' reference.txt file1.txt above command listing data which is not in reference.txt 12.12 87 11.95 88 11.83 89 12.55 84... (12 Replies)
Discussion started by: Akshay Hegde
12 Replies

4. Shell Programming and Scripting

Script not spooling in result file

Hi everyone and nice to meet you :) I'm having some issues with a script I'm writing. It's probably most chaotic, I'm no ksh guru, but the idea is to extract an ID with that query, spool it into a file, and read that file making the ID a variable. This has to be done for every row extracted by... (10 Replies)
Discussion started by: Arkadia
10 Replies

5. Shell Programming and Scripting

Write result to output file

Good morning everybody, Beeing an absolute newbie in shell scripting I would like to look for some help here. I would like to read an external text file and format the data and write it to an output file. What I was trying to do was to display the result (this worked). But now I... (1 Reply)
Discussion started by: bluejean1976
1 Replies

6. Shell Programming and Scripting

How to write result of a query to more than 1 .csv

If the result of the query is greater than say, 50,000 then the next 50,000 should be written to the second file and so on. Is it possible? (1 Reply)
Discussion started by: Jassz
1 Replies

7. Shell Programming and Scripting

Avoid file creation in a script...achive same result

Guys following lines help me in getting numbers from PID column ,to be thrown into first column of a CSV file. COLUMNS=2047 /usr/bin/ps -eo pid,ppid,uid,user,args | grep -v "PID" > /tmp/masterPID.txt cat /tmp/masterPID.txt|while read line do PID=`echo $line|awk '{print $1}'` echo "$PID"... (4 Replies)
Discussion started by: ak835
4 Replies

8. Shell Programming and Scripting

shell script result to file

Since I'm not an expert in shell scripting, I have question on sending script result to file. I have script like this... if condition=0: then echo "service is not running" | mail -s "Server Status" uname@companyname fi sleep 10 if configtion=1: then echo "service is not running" | mail -s... (3 Replies)
Discussion started by: s_linux
3 Replies

9. Shell Programming and Scripting

Sending/append result from CSH script to xls file

Hi, 1st post... Done a quick search for this so apologies if I've missed it. Basically I want to output and and append several values generated by a csh script direct to an xls openoffice file, rather than send to txt file and then physically copy and paste to xls file. Already I send... (4 Replies)
Discussion started by: scottyjock
4 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies
Login or Register to Ask a Question