
다음과 같은 구성 파일이 있습니다.
# Default LIST="nil"
LIST="element1 element2 element3"
쉘 스크립트에서 LIST를 수정하는 가장 간단한 방법은 무엇입니까?
답변1
사용 sed
:
sed -i 's/LIST\=.*/LIST="element4 element5"/' config_file
주석 처리되지 않은 경우에만 LIST를 업데이트하려면 ^
(줄 시작)을 추가하세요.
sed -i 's/^LIST\=.*/LIST="element4 element5"/' config_file
답변2
허용된 답변과 동일한 답변이지만스크립트. 누군가가 유용하다고 생각하는 경우:
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
modifyVariableInFile () {
variable="${1}"
content="${2}"
file="${3}"
if [ ! -f "${file}" ]; then
echo "modifyVariableInFile: file doesn't exist: ${file}"
exit 1
fi
sed -i "s/^${variable}\=.*/${variable}=\"${content}\"/" "${file}"
}
modifyVariableInFile ${@}