
다음과 같은 구성 파일이 있습니다.
# 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 ${@}