How do I escape m4 definitions in a string? - macros

In my code, I have a certain register which I called "foo" using the m4 macros:
define(foo, w19)
Later on, I'm trying to print a string with the following format:
print: .string "The value of foo is %d\n"
Of course, my output is:
"The value of w19 is 5"
When I want my output to be:
"The value of foo is 5"
How do I escape the macro within this string?

According to the first reference I found, and also the Wikipedia page on m4, expansion is suppressed by quoting, i.e.
`foo' is foo
Note that the opening and closing quote delimiters are backtick and apostrophe respectively.

Related

How to pass space to Cobra CLI string slice flag?

I am using a StringSliceP from the Pflag libraray accept a list of strings as CLI arguments.
I am calling the Go application from the Windows Command Prompt.
I would like some of the strings of the list to contain a (") double quote character, but I have not been able to do this.
Escaping the quotes does not work:
goapp.exe --string-slice-list "a\"b",c,d,e
Expected result: []string{"a\"b", "c", "d", "e"}
Actual result: Error: invalid argument "a\"\\b,c,d,e" for "--string-slice-list" flag: parse error on line 1, column 1: bare " in non-quoted-field
Doubling up the quotes does not work:
goapp.exe --string-slice-list "a""b",c,d,e
Expected result: []string{"a\"b", "c", "d", "e"}
Actual result: Error: invalid argument "a\"b,c,d,e" for "--string-slice-list" flag: parse error on line 1, column 1: bare " in non-quoted-field
Here is how to do it from a Windows command prompt:
goapp.exe --string-slice-list \"a\"\"b\",c,d,e
yields [a"b c d e] and
goapp.exe --string-slice-list \"a\\\"\"b\",c,d,e
does [a\"b c d e] (I’m not sure which one you actually want).
The reason for this is, as has been pointed out, that the Pflag library makes use of the Go standard library encoding/csv supporting the format described in RFC 4180. If we refer to section 2 from paragraphs 5, 6 and 7:
If fields are not enclosed with double quotes, then
double quotes may not appear inside the fields.
Fields containing line breaks (CRLF), double quotes, and commas
should be enclosed in double-quotes.
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote.

How PowerShell does decide which mode when parsing?

I can't quite understand how PowerShell parses commands and need your help.
I read the following explanation by Microsoft's about_parsing documentation:
When processing a command, the PowerShell parser operates in expression mode or in argument mode:
In expression mode, character string values must be contained in quotation marks. Numbers not enclosed in quotation marks are treated as numerical values (rather than as a series of characters).
In argument mode, each value is treated as an expandable string unless it begins with one of the following special characters: dollar sign ($), at sign (#), single quotation mark ('), double quotation mark ("), or an opening parenthesis (().
If preceded by one of these characters, the value is treated as a value expression.
I can understand when parsing a command, PowerShell uses either expression mode or argument mode, but I can't quite understand the following examples.
$a = 2+2
Write-Output $a #4(int), expression mode
Write-Output $a/H #4/H(str), argument mode
I wonder PowerShell expands variable first and then decide which mode when parsing, but is it right?
If so, there's another question about data type.
It seems reasonable for me the former command produces integral, but the latter one doesn't. Why can integer 4 be put next to string /H?
I tried this example and it worked. It seems variables turn into string whatever data type they are when expanded. Is it right?
$b = 100
Add-Content C:\Users\Owner\Desktop\$b\test.txt 'test'
I appreciate for your help.
Edited to clarify the point after got the comment
I've got the comment that the both Write-Output examples are argument mode, so can the examples be interpreted like this?
Write-Output "$a"
Write-Output "$a/H"
I'm terribly sorry for too ambiguous question, but I want to know:
In argument mode, double quotations are omitted?
The Write-Output examples are quoted from microsoft's document I linked and it says the first example produces integral, but is it wrong?

Not sure of the reason for output value

I have these two statements in a perl code that I'm trying to understand.
our $CSITOOLS=`/x/eng/csitools/netapp_menu/common/csitools.sh`;
print "${CSITOOLS}\n";
Output:
/x/eng/csitools
How does ${VARIABLE_NAME} work? (In this case {CSITOOLS})
Thanks!
From perldata:
As in some shells, you can enclose the variable name in braces
to disambiguate it from following alphanumerics (and underscores).
That is:
If you have a variable like $foo and you want to interpolate that whithin a string:
my $string = "This is $foobar";
then Perl would look for the variable $foobar. To make clear your variable's name
is just $foo you need to write
my $string = "This is ${foo}bar";
This is the way to delimit the variable's name. In your particular case the curlies {} aren't needed because "$CSITOOLS\n" already is unambiguous. However, it does no harm.
Perl allows you to surround the name of a scalar variable with braces ({}) to separate its name from any surrounding characters. Imagine you have the word "item" in a variable, and you want to print item99 within a string, with no whitespace in between:
my $name = "item";
print "${name}99\n";
Without the braces, perl would interpret the variable like this:
print "$name99\n";
...which is an undeclared, undefined variable, and definitely not what you mean. With warnings enabled, would display something like:
Use of uninitialized variable in ...
If warnings are not enabled, the program may merrily run along and do other things, possibly/likely breaking something far down the line. That makes things really difficult to troubleshoot.
In your simple case, the braces aren't needed, and you don't see them all that very often in print statements, unless you need to combine variables where there's no whitespace character between it and other valid variable characters when interpolating within a string.
From perldoc.perl.org
A string enclosed by backticks (grave accents) first undergoes double-quote interpolation. It is then interpreted as an external command, and the output of that command is the value of the backtick string, like in a shell.
Apparently, the result of csitools.sh is the string /x/eng/csitools

Arguments in Powershell are not evaluated correctly

Arguments in powershell don't get evaluated as I expect them.
A mathematical expression as argument is casted to a string if the parameter type is object or string.
Powershell code: write-host 1+1
Result printed on screen: 1+1
Expected result: 2
Could somebody tell me why does this happen?
What is the logic behind this behavior?
In the language specification it clearly states that if the parameter is of type object then the value will be passed as is with no casting.
Could you please point me to the language specification that describes this behavior as I could not find it.
Note: any function that accepts an object or a string will suffer from the same behavior.
this link here
When processing a command, the Windows PowerShell parser operates
in expression mode or in argument mode:
- In expression mode, character string values must be contained in
quotation marks. Numbers not enclosed in quotation marks are treated
as numerical values (rather than as a series of characters).
- In argument mode, each value is treated as an expandable string
unless it begins with one of the following special characters: dollar
sign ($), at sign (#), single quotation mark ('), double quotation
mark ("), or an opening parenthesis (().
Example Mode Result
------------------ ---------- ----------------
2+2 Expression 4 (integer)
Write-Output 2+2 Argument "2+2" (string)
Write-Output (2+2) Expression 4 (integer)
$a = 2+2 Expression $a = 4 (integer)
Write-Output $a Expression 4 (integer)
Write-Output $a/H Argument "4/H" (string)

How to execute nested Perl commands in same line

I am new to perl , Is it any way to write nested perl commands in single line like below (shell)
echo "I am `uname -n` and today is `date`"
I tried like below ; but not working
my $i=0 ;
print "$i++\n" ;
print "localtime()" ;
You can only interpolate variables into double-quoted strings. But those variables can be anonymous, allowing you to provide an expression to generate them. This looks like this:
my $i = 0;
print "Number ${\($i++)} date ${\scalar localtime}";
You can also use #{[ some-expression ]} instead of ${\ some-expression }. In either case, you have an expression and you create a reference to its value (or in the former case, to an anonymous array containing it), and then dereference it in the string. The expression will be in list context, so you may need to add scalar, as above.
Is it any way to write nested perl commands in single line like below (shell)
Of course you can.
print "I am " . `uname -n` . "and today is " . `date`;
Perl's backtick operator (`) doesn't work inside double quotes (""). So, the string "I am ", the output of uname -n, the string "and today is " and the output of date are joined by dot operator (.).
I don't understand what the latter part of your question means in relation to the former part (and I think ysth has already answered to it).