1

Virtualhost と *. その結果、コンソールにエコーされる唯一のものは

<VirtualHost 

私がやりたいことは、文字列全体がコンソールにエコーされることです。

<VirtualHost *:80>
        DocumentRoot /Applications/MAMP/htdocs/web
        ServerName web.localhost
        <Directory /Applications/MAMP/htdocs/web>
        Options Indexes FollowSymLinks MultiViews +Includes
        AllowOverride All 
        Order allow,deny
        allow from all 
        </Directory>
</VirtualHost>

参照用のスクリプトは次のとおりです。

#!/bin/bash
# This script should be used to automate the web site installation

checkFileForString ()
{
    # $1 = file
    # $2 = regex
    # $3 = text to be added
    declare file=$1
    declare regex=$2
    declare file_content=$( cat "${file}" )

    if [[ ! " $file_content " =~ $regex ]]; then
        echo "$3" #>> $file
    else
        replaceStringInFile $file $regex $3
    fi
}

replaceStringInFile ()
{
    # $1 = file
    # $2 = old string
    # $3 = new string

    sed -i -e 's|${2}|${3}|' $1
}

createFile ()
{
    # $1 = file
    declare fileToCheck=$1

    if [ ! -f $fileToCheck ]; then
       touch $fileToCheck   
    fi
}

# Add vhosts to httpd-vhosts.conf
echo "Adding vhosts to httpd-vhosts.conf"
currentFile="/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf"
currentRegex="<VirtualHost\s[*]:80>\s+DocumentRoot\s/Applications/MAMP/htdocs/web\s+ServerName\sweb.localhost"
newText="<VirtualHost *:80>
    DocumentRoot /Applications/MAMP/htdocs/web
    ServerName web.localhost
    <Directory /Applications/MAMP/htdocs/web>
    Options Indexes FollowSymLinks MultiViews +Includes
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
</VirtualHost>
"

checkFileForString $currentFile $currentRegex $newText
4

1 に答える 1

4

単語分割やワイルドカード展開を行わずに変数を展開するには、変数を二重引用符で囲む必要があります。

checkFileForString "$currentFile" "$currentRegex" "$newText"

スクリプトのもう 1 つの問題はreplaceStringInFile()関数です。変数は、単一引用符ではなく、二重引用符内でのみ展開されます。したがって、次のようになります。

sed -i -e "s|${2}|${3}|" "$1"
于 2013-09-13T15:15:32.210 に答える