Can i use a variable inside Ditto placeholder?
For example
[!Ditto? &tpl=`TPL-jc` &display=$myVar]
instead of
[!Ditto? &tpl=`TPL-jc` &display=`10`]
You can use another Element inside a Ditto placeholder, e.g.
[!Ditto? &tpl=`TPL-jc` &display=`[+myVar+]`]
(that's myVar as a placeholder)
[!Ditto? &tpl=`TPL-jc` &display=`[myVar]`]
(that's myVar as a Snippet that outputs the value you want)
[!Ditto? &tpl=`TPL-jc` &display=`[*myVar*]`]
(that's myVar as a Template Variable)
Related
shouldn't it be printing out HTTPS_PROXY= instead? (when $HTTPS_PROXY is not set)
I know I can work around using
echo HTTPS_PROXY=(echo $HTTPS_PROXY) or echo HTTPS_PROXY="$HTTPS_PROXY" , but I want to know why I need a work around in this case.
In fish, all variables are lists. When you concatenate a string and a variable, what it does is combine every list element with the string.
So
set bar 1 2 3
echo foo$bar
prints "foo1 foo2 foo3".
Now, when you have an undefined variable (or an empty one, set like set bar without values), this combines nothing with the string, which ends up eliminating it.
You can think of it like any variable expansion being a brace expansion - echo foo{1,2,3} is the same as echo foo$bar with bar set like above.
In many cases, that is exactly what you want. Imagine $bar being a list of directories. To go over all files in them you could use
for file in $bar/*
and if $bar was empty (there was no directory), the entire loop would be skipped instead of e.g. showing all files in "/".
The obvious solution is to quote the variable if you want to supress this. Quoting turns the variable into always exactly one argument, even if it's empty or has multiple elements, so
echo foo"$bar"
prints "foo1 2 3" (as one argument).
This is documented at https://fishshell.com/docs/current/#combining-lists-cartesian-product.
I need to run SPSS syntax in an IF statement, which tests if a variable exists in the document. I am having trouble getting the IF test right. I'm trying to do this:
do if (test if myVariable exists).
// execute code here
end if.
Execute.
I've looked here and tried this:
DO IF (myVariable) exist=1.
END IF.
Execute.
But I get the error 'There is extraneous text following the logical expression on a DO IF command. Have I misunderstood the code?
spssinc select variables command creates a list of variables according to a specified propertie. In this case the property will be the variable called "MyVar". If the variable doesn't exist, the list will stay empty:
spssinc select variables macroname="!findMyVar" /properties pattern="MyVar".
Now we define a macro that will run some commands only if the above list is not empty:
define doifMyVarexists ()
!if (!eval(!findMyVar)<>"") !then
* put your commands here, like following examples.
compute MyVar=MyVar*2.
freq MyVar.
!ifend
!enddefine.
* the macro is ready, now we call it:
doifMyVarexists.
If you run this multiple times, you will face a problem that if MyVar exists once and in a later run doesn't exist - the list doesn't get emptied (it is only overwritten if there were variables to put into it).
To solve that use the following line to empty the list before running select variables again:
define !findMyVar() !enddefine.
I have come across variables (or parameters) being declared like this:
${var_name} = "Hello world!"
As far as I can tell, this is no different to the following:
$var_name = "Hello world!"
I am wondering if the {} braces in the first example do or mean anything. Do they change the behaviour of the variable?
Curly braces in PowerShell variable names allow for arbitrary characters in the name of the variable. If there are no "pathological" characters in the variable name, then the braces are not needed and have no effect.
You'll find that "generated" code will sometimes use curly braces because they guarantee that the variable name is valid.
the {} braces use for declare variables with spaces in the middle or inside of the variable, like this:
${var_name hello } = "Hello world!2"
$var_name = "Hello world!"
it's not the same, 'cause you can't save data in a variable with spaces, powershell understand the variable until a space, except it's inside the braces.
Have a good day. (:
What might not be obvious on a first glance is that you can use any provider within ${}, for example:
${c:\tmp\foo.txt}="Hello World" # C: uses file system provider
The effect depends on the provider. For the file system provider the example changes the content of the specified file.
Problem
I want to perform parametric variable evaluation. The to-be-evaluated variable name is constructed with a string concatenation - from a namespace part and a name part being defined in a variable. Example: env:$var, where the value of $var is, for instance "OS". However, while using the expression
${env:OS}
gives the expected value Windows_NT, the construct
$var="OS"
${env:$var}
is a null-valued expression.
Motivation
I'm not really intereseted in the value of environment variables (but this was the simplest example, I could find). What I really want, is to refer to the content of a file via the ${c:<filename>} construct. I want to perform several, conditional in-file string substitutions and, I'd like to use a construct similar to this:
<identify files in a foreach>
${c:<filename>} -replace 'this', 'that' > ${c:<new filename>}
To achieve this, I need <filename> to be a value of an iterator variable.
Question
If the value of $var is OS, what shall be at ..., if I expect the value of the following expression to be Windows_NT?
${env:...var...}
Use Get-ChildItem on the env: PSDrive and expand the Value of the result:
(ls env:$var).Value
Edit: As #PetSerAl suggested in the comments using Get-Content is a more elegant approach:
(cat env:$var)
Can any one explain this format?
${name:-$devi}
Example:
"${1+"$#"}" will check for that first variable to be set , if not it will use that
command line argument.
What is the difference between :- and + between those variables?
${var:-val}
is the same as ${var} if var is set and non-null, otherwise it expands to val. This is used to specify a default value for a variable.
${var+val}
expands to nothing if var is unset, otherwise it expands to val. This is used to supply an alternate value for a variable.
"${1+"$#"}"
is a workaround for a bug in old shell versions. If you just wrote "$#", it would expand to "" when no arguments were supplied, instead of expanding to nothing; the script would then act as if a single, empty argument had been supplied. This syntax first checks whether $1 is set -- if there's no first argument, then there are obviously no arguments at all. If $1 is unset, it expands to nothing, otherwise it's safe to use "$#".
Most modern shell versions don't have this bug, so you can just write "$#" without the special check. I'm not sure if there are any other common use cases for the + construct in shell variable expansion.