Iam new to bach script. following is the code i used to search the name entered through command line and search the entered string in outputLog.txt .if found update the current system time of that name string.
In command line
[root@localhost desktop]# ./myscript.sh name1
Logone2.txt
name1 = Tue Jan 20 14:00 19 IST 2012
name2 =Tue Jan 20 14:05 19 IST 2012
name3 = Tue Jan 20 14:20 19 IST 2012
name4 =Tue Jan 20 14:45 19 IST 2012
My problem is while i trying to enter name1,it is updated with the current system time also its is appending as last entry.
Eg:Logone2.txt
name1 = Tue Feb 20 14:00 19 IST 2012
name2 =Tue Jan 20 14:05 19 IST 2012
name3 = Tue Jan 20 14:20 19 IST 2012
name4 =Tue Jan 20 14:45 19 IST 2012
name1 = Tue Feb 20 14:00 19 IST 2012 (appended entry)
I only want to append the string with current system time if it is not found in the Logone2.txt.IF the string(name1) entered through command line is found in the Logone2.txt ,update the name with current system time as follows.
Eg:Logone2.txt
name1 = Tue Feb 20 14:00 19 IST 2012
name2 =Tue Jan 20 14:05 19 IST 2012
name3 = Tue Jan 20 14:20 19 IST 2012
name4 =Tue Jan 20 14:45 19 IST 2012
Here is the code iam using
#!/bin/bash
echo "${1} $(date)" >> /root/Desktop/scripts/Logone2.txt
fgrep "${1}" /root/Desktop/scripts/Logone2.txt && \
sed -i "/${1}/ s/.*/${1} = $(date)/" /root/Desktop/scripts/Logone2.txt || \
echo "${1} $(date)" >> /root/Desktop/scripts/Logone2.txt
if i got it right my first idea is to rewrite the entire file replacing name1 with sed. e.g.