여러 줄 모드를 사용하여 bash에서 문자열 교체를 수행하는 방법은 무엇입니까?
이를 설명하기 위해 의사 코드를 제공했습니다.
TARGET_STR=' $N = "magic_quotes_gpc = <b>"._("On")."</b>";
$D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case.");
$S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'.");
$R = ini_get('magic_quotes_gpc');
$M = TRUE;
$this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M );'
REPLACE_STR=' /* NOTE: "Magic_quotes_gpc" is no longer required. We taught GOsa2 to deal with it (see /usr/share/gosa/html/main.php). By Questor */
/* Automatic quoting must be turned on */
/* $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case.");
$S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'.");
$R = ini_get('magic_quotes_gpc');
$M = TRUE;
$this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M ); */'
PATH_TO_FILE='/usr/share/gosa/setup/class_setupStep_Checks.inc'
some_command '$TARGET_STR/$REPLACE_STR' $PATH_TO_FILE
참고 1:bash 변수를 사용하고 싶습니다.
노트 2:이스케이프되지 않은 문자열(TARGET_STR 및 REPLACE_STR의 문자열)을 사용하고 싶습니다.
감사해요!
답변1
모든 관련 텍스트 문자열을 16진수 문자열로 변환하고 16진수로 대체를 수행한 다음 결과를 다시 텍스트로 변환할 수 있습니다. 다음과 같이 보일 수 있습니다.
#!/bin/bash
# replace.sh
# Set the target string, i.e. the string to replace
read -r -d '' TARGET_STR <<'HEREDOC'
$N = "magic_quotes_gpc = <b>"._("On")."</b>";
$D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case.");
$S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'.");
$R = ini_get('magic_quotes_gpc');
$M = TRUE;
$this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M );
HEREDOC
# Set the replacement string, i.e. the string to replace the target string with
read -r -d '' REPLACE_STR <<'HEREDOC'
/* NOTE: "Magic_quotes_gpc" is no longer required. We taught GOsa2 to deal with it (see /usr/share/gosa/html/main.php). By Questor */
/* Automatic quoting must be turned on */
/* $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case.");
$S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'.");
$R = ini_get('magic_quotes_gpc');
$M = TRUE;
$this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M ); */'
HEREDOC
# Set the path to the input file
PATH_TO_FILE="target-string.txt"
# Set file paths
PATH_TO_HEX_FILE="${PATH_TO_FILE}.hex"
PATH_TO_REPLACED_FILE="replaced-${PATH_TO_FILE}"
# Convert the two strings to hexadecimal
TARGET_STR_HEX="$(echo "${TARGET_STR}" | xxd -p | tr -d '\n')"
REPLACE_STR_HEX="$(echo "${REPLACE_STR}" | xxd -p | tr -d '\n')"
# Conver the input file to hexadecimal
xxd -p "${PATH_TO_FILE}" | tr -d '\n' > "${PATH_TO_HEX_FILE}"
# Perform the replacement using hexadecimal strings
sed -i'' "s/${TARGET_STR_HEX}/${REPLACE_STR_HEX}/g" "${PATH_TO_HEX_FILE}"
# Convert the result back to text
xxd -r -p "${PATH_TO_HEX_FILE}" "${PATH_TO_REPLACED_FILE}"
# Remove intermediate file
rm "${PATH_TO_HEX_FILE}"
그러면 제공한 테스트 데이터에 대해 원하는 결과가 생성됩니다.