如何使用终端向文件添加行

如何使用终端向文件添加行

我是 Linux 新手。我想制作一个自动编译 cm11 的脚本,这是我的脚本

#/bin/bash
cd cm11/.repo/
rm -rf local_manifests
mkdir local_manifests
cd local_manifests
touch kyleve.xml
echo "<?xml version="1.0" encoding="UTF-8"?>" >> kyleve.xml
echo "<manifest>" >> kyleve.xml
echo "<project name="Grace5921/device_samsung_kyleve" path="device/samsung/kyleve" revision="cm11" remote="github"/>" >> kyleve.xml
echo "<project name="Grace5921/vendor_samsung_kyleve" path="vendor/samsung/kyleve" revision="cm11" remote="github"/>" >> kyleve.xml
echo "<project name="Grace5921/kernel_samsung_kyleve" path="kernel/samsung/kyleve" revision="cm11" remote="github"/>" >> kyleve.xml
echo "</manifest>" >> kyleve.xml
cd
cd cm11
repo sync -j4
. build/envsetup.sh
lunch
brunch kyleve
echo "Done!"

但是 echo 命令将这一行添加到 kyleve.xml

<project name=Grace5921/device_samsung_kyleve path=device/samsung/kyleve revision=cm11 remote=github/>

但我的输出文件中需要这一行

<project name="Grace5921/device_samsung_kyleve" path="device/samsung/kyleve" revision="cm11" remote="github"/>

这些双引号弄乱了在这种情况下哪个命令可以起作用

答案1

使用单引号使字符串文字:

echo '<project name="Grace5921/device_samsung_kyleve" path=\
  "device/samsung/kyleve" revision="cm11" remote="github"/>' >>kyleve.xml

或者使用以下命令转义相关的双引号\

echo "<project name=\"Grace5921/device_samsung_kyleve\" \
  path=\"device/samsung/kyleve\" revision=\"cm11\" remote=\"github\"/>" >>kyleve.xml

答案2

您应该使用单引号 ( ') 而不是双引号 ( ") 来括起您的行。例如:

echo "myQuoted line "foobar" " >> test.txt
echo 'myQuoted line "foobar" ' >> test.txt

cat test.txt
myQuoted line foobar
myQuoted line "foobar"

相关内容