Linux
shell中兩個標籤之間的註釋行
我想註釋/取消註釋以禁用/啟用
httpd
實例中兩個配置部分標記之間的記憶體,使用sed
orawk
。我可以註釋/取消註釋以
Cache
/Expires
關鍵字開頭的行,但這仍然使模組標籤被註釋。如何以這樣一種方式評論/取消評論,即開始和結束標籤之間的每一行都通過單個命令進行評論/取消評論。這是一個範例記憶體配置。
#<IfModule mod_cache.c> # #<IfModule mod_disk_cache.c> # CacheRoot "/var/cache/mod_proxy" # CacheEnable disk # CacheEnable disk # CacheEnable disk # CacheIgnoreCacheControl On # CacheDirLevels 1 #</IfModule> # #</IfModule> #<IfModule mod_expires.c> # Header set Cache-Control "max-age=604800, public" # ExpiresActive On # ExpiresDefault "access plus 1 week" # ExpiresByType text/cache-manifest "access plus 0 seconds" # ExpiresByType text/html "access plus 1 year" # ExpiresByType text/xml "access plus 0 seconds" # ExpiresByType application/xml "access plus 0 seconds" # ExpiresByType application/json "access plus 0 seconds" # ExpiresByType application/rss+xml "access plus 1 hour" # ExpiresByType application/atom+xml "access plus 1 hour" # ExpiresByType image/x-icon "access plus 1 week" # ExpiresByType image/gif "access plus 1 month" # ExpiresByType image/png "access plus 1 month" # ExpiresByType image/jpg "access plus 1 month" # ExpiresByType image/jpeg "access plus 1 month" # ExpiresByType video/ogg "access plus 1 month" # ExpiresByType audio/ogg "access plus 1 month" # ExpiresByType video/mp4 "access plus 1 month" # ExpiresByType video/webm "access plus 1 month" # ExpiresByType application/x-font-ttf "access plus 1 month" # ExpiresByType font/opentype "access plus 1 month" # ExpiresByType application/x-font-woff "access plus 1 month" # ExpiresByType image/svg+xml "access plus 1 month" # ExpiresByType application/vnd.ms-fontobject "access plus 1 month" # ExpiresByType text/css "access plus 1 year" # ExpiresByType application/javascript "access plus 1 year" #</IfModule>
所以我想要一個命令,可以評論標籤開始之間的每一行,
<IfModule mod_cache.c>
直到它被關閉,等等<IfModule mod_expires.c>
。
試試這個,
在腳本中有以下內容來評論或取消評論模組
if [ "$2" == uncomment ]; then sed -i "/<IfModule $1>/,/<\/IfModule>/ s/^#//" apache.conf elif [ "$2" == comment ]; then sed -i "/<IfModule $1>/,/<\/IfModule>/ s/^\(<\| \)/#\1/" apache.conf fi
執行腳本的語法:
sh script.sh <moduleName> <comment/uncomment>
例子:
sh script.sh mod_disk_cache.c uncomment
您可以使用
awk
. 為簡單起見,我們假設mod.xml
包含:#stuff # #<IfModule mod_disk_cache.c> # CacheRoot "/var/cache/mod_proxy" # CacheEnable disk # CacheEnable disk # CacheEnable disk # CacheIgnoreCacheControl On # CacheDirLevels 1 #</IfModule> # #other stuff
然後,
$ cat mod.xml | awk -F'#' 'BEGIN { state = 0; } { if (/mod_disk_cache/) state = !state; if (state) print $2; else print $0; if (/\/IfModule/) state = !state; }' > umod.xml $ cat umod.xml #stuff # <IfModule mod_disk_cache.c> CacheRoot "/var/cache/mod_proxy" CacheEnable disk CacheEnable disk CacheEnable disk CacheIgnoreCacheControl On CacheDirLevels 1 </IfModule> # #other stuff
state
變數記錄我們何時在 ( )state == 0
或在 (state != 0
) 我們想要評論/取消評論的塊內。**注意:**這裡重要的一件事是上面的塊不包含以 a 結尾的子塊,
</IfModule>
因為這會過早地重置狀態。如果你需要支持這種情況,那麼國家需要擷取更多。例如,考慮將狀態設置為在-1
目標塊之外時的整數,當您進入目標塊時設置為0
,並在您進入子塊時遞增。此命令處理
mod_cache.c
塊具有嵌套子塊的原始輸入:$ cat mod.xml | awk -F# 'BEGIN { state = -1; } { if (/<IfModule mod_cache\.c>/) state = 0; else if (/<IfModule .*>/) state++; if (state >= 0) print $2; else print $0; if (/<\/IfModule/) state--;}'
回到簡單的例子,重新評論你可以做類似的事情,只需設置輸出欄位分隔符(OFS)並在計算為真
#
時在每行之前列印出一個:state
$ cat umod.xml | awk -F'#' 'BEGIN { OFS=""; state = 0; } { if (/mod_disk_cache/) state = !state; if (state) print "#", $0; else print $0; if (/\/IfModule/) state = !state; }' #stuff # #<IfModule mod_disk_cache.c> # CacheRoot "/var/cache/mod_proxy" # CacheEnable disk # CacheEnable disk # CacheEnable disk # CacheIgnoreCacheControl On # CacheDirLevels 1 #</IfModule> # #other stuff