Methods of declaring a variable in shell script - shell

What is the difference between the following two methods of declaring a variable in shell script?
var='some/path'
var=${var:-"some/path"}

#this will set var value with some/path,
#no matter var is empty or not (overwrite)
var='some/path'
# this will set value of var to "some/path"
#only if var is empty/or not declared yet.
var=${var:-"some/path"}

var='some/path'
Will always set var to some/path
var=${var:-"some/path"}
Will only set var to some/path if var if not already set. If it is set, its value will not change.

Related

Pass Variable inside the another variable in Jmeter

I Have a scenario like need to pass a random variable example ${__Random(1,25,)} in inside one more variable like
Globel variable:
Test = TestResults
${__Random(1,25,)}
Sample Request Data:
${Test(randome variable)}, ${Test(randome variable)},${Test(randome variable)},-------- ${Test(randome variable)}
Sample expected response:
TestResults1,TestResults10,TestResults5,----------- TestResults20
How should I pass this scenario?
Use the V function
${__V(TestResults${__Random(1,25,)})}
V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references
You can replace TestResults with ${yourVarName} if you want to dynamically use additional variable

Variable appears to be lost when setting a property in jmeter

I want create a property from a variable. The variables was created by calling a variable from a xpath extraction, then using a substring to then get the last 4 characters. The substring string value is saved to a variable, then set to a property.
When I run the script, the log.info(vars.get("lastcard")); returns the value of the variable. However it then fails to save to a property, because when that property is called(${__property(lastNum)} it will display - ${lastcard}
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", String.valueOf(last4));
log.info(vars.get("lastcard"));
${__setProperty(lastNum,${lastcard})};
Any ideas as to what is going on
You should read user manual about scripting:
ensure the script does not use any variable using ${varName}
You should use JSR223 variables vars and props to handle variables and properties. In your case change last line to:
props.put("lastNum", vars.get("lastcard"));
Also you can set variable in shorter way:
vars.put("lastcard", vars.get("card").substring(tesTe.length()-4));
There was 2 changes that need to be made to resolve the issues.
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", last4); //Already string therefore no need to use String.valueOf()
log.info(vars.get("lastcard"));
props.put("lastNum",vars.get("lastcard")); //Setup to use props.put instead of set property

Wakanda query with dynamic attribute name in a variable

Please, take a look at following code:
var platNom = "apero"; // I set the value of my attribute in one variable
var monAttribut = ds.Convives.attributes[platNom]; //this works
var invitants = ds.Convives.query("$(this[platNom] == 'oui') ", {allowJavascript: true});// This does not work
Is there a problem in my syntax?
If I understand correctly you would like to use attribute variable to construct the query string. Then you can do this by simply reference platNom directly.
The correct syntax should be:
var invitants = ds.Convives.query(platNom + " == 'oui'")

Go capturing template variables and names

I am experimenting with Go's templates.
I am curious: is it possible to do the following: {{$myVariable := randomStringFunc | saveFunc}}
Where:
$myVariable - randomly chosen variable name
randomStringFunc - function that generates random strings
TRICKY: saveFunc - a function that saves the variable name and its value
I have looked into capture-or-assign-golang-template-output-to-variable but I am not sure how and if it can help me achieve my goal.
EDIT
In the end I would like to have a mapping between the variable name that is defined in the template and the value that is assigned to it:
var variableMapping map[string]string
After template execution the content of variableMapping should be something like:
{
"$myVariable:"randomString1",
"$anotherVariable":"5",
"$thirdVariableInMyTemplate":"false"
}

variable was written but never used in Swift 2.0 & Xcode 7

When I create a variable without String() to initialize the variable an error shows up alerting, "variable not initialize", same error shows up when I directly assign
var username: String = usernameTextfieldSigup.text!
And when I initialize the variable using the code below,
var username: String = String()
username = usernameTextfieldSignup.text!
warning comes up, variable 'username' was written but never used.
I know I could just ignore these warnings but I'd like a cleaner code.
I'm using Xcode7 beta5 release.
You can declare the username without a value first:
var username: String
and then assign a value to it in your initializer:
// replace with your (valid) initializer
func init() {
username = usernameTextfieldSignup.text!
}
However, I'm sure that usernameTextfieldSignup.text won't have a value until the user ha actually provided a value. So in that case I would recommend to make username an optional so you can set it's value when you need to:
var username: String?
and then when setting it:
username = usernameTextfieldSignup.text
and when accessing it:
if let uname = username {
// do something with the uname
}
Also, the variable 'username' was written but never used warning you're seeing is probably because you write to / initialise username but there's no code actually reading it, so it's kind of a useless variable according to the compiler then.
To init an empty string you have to either make it explicit or optional like so:
let string: String!
let string: String?
You can do this with any datatype.

Resources