如何注释bash中的代码行?

时间:2019-06-06 17:02:37

标签: bash

我需要在apache配置中注释掉3行:

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    #RewriteCond %{REQUEST_FILENAME} !-f    <- 
    #RewriteCond %{REQUEST_FILENAME} !-d    <- 
    #RewriteRule . index.php [L]            <- 

适用于1000台机器。

如何在所有这些机器上通过ssh在bsh命令上使用bash命令注释掉最后三行?

样本输入

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f    <- 
    RewriteCond %{REQUEST_FILENAME} !-d    <- 
    RewriteRule . index.php [L]            <- 

样本输出

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    #RewriteCond %{REQUEST_FILENAME} !-f    <- 
    #RewriteCond %{REQUEST_FILENAME} !-d    <- 
    #RewriteRule . index.php [L]            <- 

2 个答案:

答案 0 :(得分:0)

您可以使用以下sed注释掉这些行:

resolv.conf

答案 1 :(得分:0)

如果要注释每个文件的后三行,则应该使用ed

ed file <<END
$;#
-2;#
s/[^[:blank:]]/#&/
+s
+s
wq
END

演示:

#!/bin/sh
f=$(mktemp)

trap 'rm "$f"' EXIT

cat >"$f"  <<END
    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f    <- 
    RewriteCond %{REQUEST_FILENAME} !-d    <- 
    RewriteRule . index.php [L]            <- 
END

ed "$f" <<END >/dev/null
$;#
-2;#
s/[^[:blank:]]/#&/
+s
+s
wq
END

cat "$f"

输出:

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    #RewriteCond %{REQUEST_FILENAME} !-f    <- 
    #RewriteCond %{REQUEST_FILENAME} !-d    <- 
    #RewriteRule . index.php [L]            <-
相关问题