Tuesday, February 20, 2007

Trip คำสั่ง diff

คำสั่ง diff เป็นคำสั่งเปรียบเทียบไฟล์ เช่น ในกรณีที่เราแก้ไขค่าคอนฟิกสืของโปรแกรม samba ซึ่งก่อนแก้ไข smb.conf นี่เราได้ทำการ backup ไว้เป็น smb.conf.bak แล้วเราก็ทำการปรับแต่ง ที่นี่เราสามารถใช้คำสั่ง diff เพื่อตรวจทานความแตกต่างกันระหว่าง 2 ไฟล์ เพื่อให้ทราบความว่าเราได้แก้ไขจุดไหนไปบ้างแล้ว ดังตัวอย่างที่ยกมาจากเว็บไซต์ www.ubuntuclub.com ดังนี้

$ diff apache2.conf apache2.conf.orig

อยากลัพธ์ก็ คือ

32c32
< Timeout 350
---
> Timeout 300

43c43
< MaxKeepAliveRequests 110
---
> MaxKeepAliveRequests 100

ความหมายก็คือ บรรทัดที่ต่างกันเป็นบรรทัดที่ 32 โดยไฟล์ทางซ้ายมือ มีค่าเป็น Timeout 350 ส่วนไฟล์ทางขวามือ มีค่าเป็น Timeout 300

แ ละอีกส่วนที่ต่างกัน คือ บรรทัดที่ 43 โดยไฟล์ทางซ้ายมือ มีค่าเป็น MaxKeepAliveRequests 110 ส่วนไฟล์ทางขวามือ มีค่าเป็น MaxKeepAliveRequests 100

Reference
http://www.ubuntuclub.com/html/index.php?option=com_content&task=view&id=508&Itemid=55

เพิ่มเติม
colordiff

#diff -y test.txt test1.txt  --> แสดงออก 2 ส่วนในแนวตั้ง
#diff -u test.txt test1.txt  --> แสดงออก 2 ส่วนในแนวนอน มีเครื่องหมาย --- และ +++

ติดตั้ง package colour เพื่อแสดงสี
#apt-get install colordiff
การใช้งาน
#colordiff -y test.txt test1.txt  --> แสดงออก 2 ส่วนในแนวตั้ง
#colordiff -u test.txt test1.txt  --> แสดงออก 2 ส่วนในแนวนอน มีเครื่องหมาย --- และ +++

OR

#diff -y test.txt test1.txt | colordiff --> แสดงออก 2 ส่วนในแนวตั้ง
#diff -u test.txt test1.txt | colordiff --> แสดงออก 2 ส่วนในแนวนอน มีเครื่องหมาย --- และ +++

ตัวอย่างการตรวจสอบคำสั่ง diff กับ Samba share file
1.
------------------------create file checkdiff.sh-----------------------------------------
#!/bin/bash

variable=$(diff -u /etc/samba/smb.conf /etc/samba/smb.conf.bak)

if [[ $variable -eq 0 ]]
then
  echo $"nothing"
else
  echo -n "$variable" | mail -s "Alert check diff file samba configure" email@domain.com

fi

-----------------------End--------------------------------------------------------------------
2. Create crontab

00 00 * * * root /root/check_diff/checkdiff.sh





No comments: