wallyhall / shini

Portable /bin/sh routine for reading and writing INI files.
MIT License
39 stars 11 forks source link

How to add " " around value using shini_write? #11

Closed amtssp closed 7 years ago

amtssp commented 7 years ago

Hi I would like my shairportsync.cfg file to look like this: [SHAIRPORT-SYNC] SHAIRPORT="yes"

I have a piece of code looking like this:

SECTION_NAME=SHAIRPORT-SYNC 
SHAIRPORT=yes
shini_write "shairportsync.cfg" "${SECTION_NAME}" "SHAIRPORT" "${SHAIRPORT}"

which result in this shairportsync.cfg file: [SHAIRPORT-SYNC] SHAIRPORT=yes

I haven't figured out how to add "" around the value?

wallyhall commented 7 years ago

You'll just need to escape the double quotes in the string, for example:

#!/bin/sh
. shini.sh

shini_write "write_test.ini" "SECTION1" "unquoted" "string"
shini_write "write_test.ini" "SECTION1" "quoted" "\"string\""

cat write_test.ini
echo ""

(Note the \" in the example above.) Executing produces:

$ sh write_test.sh 

[SECTION1]
quoted="string"
unquoted=string
amtssp commented 7 years ago

Thank you for your support. I thought I had tried that as well - but obviously I did something wrong. It is working fine.

One minor issue - when you find the time. When I write to an ini file it always insert an empty line in the beginning. It is more cosmetic..

wallyhall commented 7 years ago

Yeah I just noticed that too... (the blank line). ;-)

I'll open a separate issue for it.

amtssp commented 7 years ago

Sorry, I still have a problem here. If I have more than one quoted string shini will "unquote" previously quoted strings.

Code like this

SECTION_NAME=TEST
shini_write "shairportsync.cfg" "${SECTION_NAME}" "unquoted" "string"
shini_write "shairportsync.cfg" "${SECTION_NAME}" "quoted" "\"string\""
shini_write "shairportsync.cfg" "${SECTION_NAME}" "quoted2" "\"string2\""

Results in only the last quoted string to be quoted.

[TEST]
unquoted=string
quoted=string
quoted2="string2"