내 호스팅 공급자 플랫폼의 CentOs 공유 호스팅 파티션에 있는 모든 WordPress 사이트를 업데이트하는 데 사용되는 다음 명령 세트가 있습니다(일일 cron을 통해).
wp세트 내부의 명령은 다음 pushd-popd과 같습니다.WP-CLIWordPress 웹사이트에서 다양한 쉘 수준 작업에 사용되는 Bash 확장 프로그램입니다.
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all
wp core update
wp language core update
wp theme update --all
popd
fi
done
디렉토리는 public_html모든 웹사이트 디렉토리가 위치한 디렉토리입니다(각 웹사이트에는 일반적으로 데이터베이스와 기본 파일 디렉토리가 있습니다).
public_html일부 디렉토리가 있다고 가정하면그렇지 않은 것WordPress 웹 사이트 디렉토리는 WP-CLI와 관련하여 오류를 반환합니다.
이러한 오류를 방지하려면 다음을 수행할 수 있다고 가정합니다.
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all 2>myErrors.txt
wp core update 2>myErrors.txt
wp language core update 2>myErrors.txt
wp theme update --all 2>myErrors.txt
popd
fi
done
네 번(또는 그 이상) 쓰는 대신 2>myErrors.txt모든 명령의 모든 오류가 한 줄로 동일한 파일로 이동되도록 하는 방법이 있습니까?
답변1
연산자 는 쓰기를 위해 > file열지 file만 처음에는 잘립니다. 즉, 새로운 파일이 나올 때마다 > file파일의 내용이 교체됩니다.
에 모든 명령의 오류가 포함되도록 하려면 해당 파일을 한 번만 열거나 처음 열었다가 두 번 myErrors.txt열면 됩니다 (파일이 다음 위치에서 열림).>>>추가방법).
pushd여기에서 / popd오류도 로그 파일로 이동해도 괜찮다면 전체 for루프를 리디렉션할 수 있습니다.
for dir in public_html/*/; do
if pushd "$dir"; then
wp plugin update --all
wp core update
wp language core update
wp theme update --all
popd
fi
done 2>myErrors.txt
또는 예를 들어 2, 3 이상의 fd에서 로그 파일을 열고 로그 파일로 리디렉션하려는 각 명령 또는 명령 그룹에 대해 2>&3(또는 2>&3 3>&-필요하지 않은 fd로 명령을 오염시키지 않도록) 사용할 수 있습니다. :
for dir in public_html/*/; do
if pushd "$dir"; then
{
wp plugin update --all
wp core update
wp language core update
wp theme update --all
} 2>&3 3>&-
popd
fi
done 3>myErrors.txt
답변2
중괄호를 사용하여 다음을 수행할 수 있습니다.그룹블록을 만들고 모든 출력을 리디렉션합니다.
for dir in public_html/*/; do
if pushd "$dir"; then
{
wp plugin update --all
wp core update
wp language core update
wp theme update --all
} 2>myErrors.txt
popd
fi
done


