Richard W.M. Jones
2016-Dec-16 17:09 UTC
[Libguestfs] IRC question: appending newline to end of file
16:43 < martingo_> hi all 16:43 < martingo_> I am using write_append to write at the end of one file 16:43 < martingo_> but newline he is not taking 16:43 < martingo_> guestfish add ./overcloud-full.qcow2 : run : mount /dev/sda / : write_append /etc/fstab "nodev /mnt/huge_qemu_1G hugetlbfs rw,pagesize=1G 0 0\n" 16:43 < martingo_> any ideas 16:48 < martingo_> any help? An example of what can go wrong is shown here: $ guestfish -N fs -m /dev/sda1 \ write /foo "hello" : write-append /foo "goodbye\n" : hexdump /foo 00000000 68 65 6c 6c 6f 67 6f 6f 64 62 79 65 5c 6e |hellogoodbye\n| 0000000e Notice that the literal characters '\' and 'n' have been added to the file. It helps to look at the API calls being made, by adding the guestfish -x option: $ guestfish -x -N fs -m /dev/sda1 \ write /foo "hello" : write-append /foo "goodbye\n" : hexdump /foo [...] libguestfs: trace: write_append "/foo" "goodbye\n" (Notice that the trace output escapes non-printable characters using \x.., so again that's the literal two characters '\' and 'n'). This isn't really surprising because we're asking the shell to send those two characters to guestfish, as you can see by doing: $ echo "\n" | hexdump -C 00000000 5c 6e 0a |\n.| 00000003 The guestfish *command line* will turn \n into a newline character as described here: http://libguestfs.org/guestfish.1.html#escape-sequences-in-double-quoted-arguments but this doesn't apply to the shell command line because it uses a different code path. Anyway to fix this you can either do: $ guestfish -N fs -m /dev/sda1 \ write /foo "hello" : write-append /foo "goodbye " : hexdump /foo You have to actually press the newline key: ``"goodbye<CR>''. Or slightly less cumbersome is to use a script which invokes the guestfish command line code path: $ cat test.sh guestfish -N fs -m /dev/sda1 <<'EOF' write /foo "hello" write-append /foo "goodbye\n" hexdump /foo EOF $ bash test.sh 00000000 68 65 6c 6c 6f 67 6f 6f 64 62 79 65 0a |hellogoodbye.| 0000000d HTH, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html