I am trying to store an expression language in YAML file
"name": "${foo.data:toLower().equals('hello')}"
I tried putting '\' in front of '$' and {}, but it doesn't work.
I just want to set the "name" key to the expression language above.
Failed attempts results name key to get a property with "toLower().equals('hello')"
If you put a backslash (\) in front of the $ within the double quoted string, you should get an error because \$ is an unknown escape sequence in YAML.
If you don't get that error, your parser is broken, but you might be able to get around that by using double backslashes:
"name": "\\${foo.data:toLower().equals('hello')}"
(you may also need them for the { and })
Related
I am using urfave/cli for my go program and I would like to have a cli flag that reads a json value like this one:
{"name":"foo","surname":"var"}
I am currently reading that variable as a cli.StringFlag which returns a string. Then, I was planning to json.Unmarshall it but it does not work. The problem is that the returned string by the cli library is like this:
[{name foo} {surname var}]
which is not a json anymore.
Is there a way to achieve this? Note that if it returned a simple map, that would work too
for Linux, try to pass the paramaters with shell escape
#!/bin/bash
echo "{\"name\":\"foo\",\"surname\":\"var\"}"
in go program, just marshal this string parameter
The issue is that the shell (bash, ksh, csh, zsh, ...) interprets
{"name":"foo","surname":"var"}
as a sequence of bareword and quoted word tokens:
Token Type
Value
bareword
{
quoted word
name
bareword
:
quoted word
foo
bareword
,
quoted word
surname
bareword
:
quoted word
var
bare word
}
As it happens, a comma (,) is a shell operator, used for arithmetic, and that essentially gets discarded (at least in zsh, what I use).
The whole is then spliced together to get
name:foo surname:var
You can see this in action by opening your shell and executing the command
echo {"name":"foo","surname":"var"}
If, however, you quote your JSON document with single quotes ('):
echo '{"name":"foo","surname":"var"}'
You'll get what you might expect:
{"name":"foo","surname":"var"}
Note, however, that this will fail if the text in your JSON document contains a literal apostrophe/single quote (', U+0027), so you'd want to replace all such occurrences within the JSON document with \, to escape them.
I have a .yaml file like this:
title: 'We'll do cool stuff'
draft: true
However, I get the following error:
Error parsing YAML: YAMLException: can not read a block mapping entry;
a multiline key may not be an implicit key at line 2, column 6:
draft: true
^
How can I fix it?
Note: this setup seems different than the other questions where this same error was raised, including the following posts:
Error parsing YAML
Getting following error on serverless.yaml
yaml syntax issues?
You can use a site like YAML Formatter to format and validate your yaml:
In this case, the error message and location is a bit of red-herring.
The error is actually caused by a string that was accidentally terminated because of an unescaped quote symbol within the string. A hint for this is the syntax highlighting of 'We'll do cool stuff'.
To fix, in this case, you can just skip wrapping the string quotes and rewrite like this:
title: We'll do cool stuff
draft: true
Further Reading
Do I need quotes for strings in YAML?
How to escape double and single quotes in YAML
title: "We'll do cool stuff"
draft: true
I have got the exact same issue, The problem with it is, we are using a single quote' in between the string and also wrapping the string with a single quote.
I resolved it by wrapping the string with a double quote.
You can also trace the issue more by reading this
I have the following data. Which is not valid in YAML. Double quotes are not working here:
data:"{\gateway\: \172.16.16.1/24\ \modulesModel\: [\N9K-X9364v\ \N9K-vSUP\]}"
Is there any better way to format it?
You are starting escape sequences with the backslash.
Use \\ to represent the backslash in a yaml string.
However your string looks like JSON where " are replaced with \ and , are missing completely. Maybe somebody told you to Escape the " with \. This is done this way:
data: "{\"gateway\": \"172.16.16.1/24\", \"modulesModel\": [\"N9K-X9364v\" \"N9K-vSUP\"]}"
In one of my variables I store result (string) from aws cli command.
When echoing the value it is displayed in " " but injecting it as a parameter shows that extra characters (") are added at the beginning and end of the string.
How to eliminate these? What do they represent?
Code and error log:
dms_arn=$(aws dms describe-replication-tasks --filter Name=replication-task-id,Values="$dms_name" `--query=ReplicationTasks[0].ReplicationTaskArn --region us-east-1)`
echo Stopping Task "$dms_arn"
build 02-Nov-2021 18:52:01 Stopping Task "arn:aws:dms:us-east-1:account:task:XYZ"
error 02-Nov-2021 18:52:02
error 02-Nov-2021 18:52:02 An error occurred (InvalidParameterValueException) when calling the StopReplicationTask operation: Invalid task ARN: "arn:aws:dms:us-east-1:account:task:XYZ"
" is the html code for double quote ("). The double quotes are being converted to html code.
Try using the —output text option with your aws command (so you don’t get quotes). See the documentation about output format: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output-format.html
If necessary, you can remove wrapping double quotes using shell parameter expansion:
dms_arn=${dms_arn%\"}
dms_arn=${dms_arn#\"}
echo "$dms_arn"
# quotes are gone
" is the HTML entity corresponding to the double quotation marks.
See: entity
To convert them check: Short way to escape HTML in Bash? and Bash script to convert from HTML entities to characters
I've made a mistake in the file below, but I cannot see where my mistake is. I have this command in my .gitlab-ci.yml configuration file.
- sed "s/use_scm_version=True/use_scm_version={'write_to': '..\/version.txt', 'root': '..'},\/"setup.py
It seems that the ":" are interpreted as a semicolon even if I surround the entire sed between double quotes.
(<unknown>): did not find expected key while parsing a block mapping at line 109 column 11
Any ideas ?
Since your double quotes are not at the beginning of the scalar node, they don't have special meaning in YAML and the colon is seen as the normal value indicator (and both the key and value have an embedded double quote).
I recommend you quote the whole scalar:
- "sed s/use_scm_version=True/use_scm_version={'write_to': '..\/version.txt', 'root': '..'},\/setup.py"
And optionally add \" (backslash escaped double quotes) as necessary within there if that doesn't work.