What is the different between Empty String and none Created Variable in Batch File?
and if they have any different, can you show me an example of using empty string vs none created string?
#echo off
title Empty String Vs None Created Variable
set String=
set variable=exist
if [%String%] == [] echo its be a Empty String
if [%variable%] == [] echo its be a Empty String
if [%none_exist_var%] == [] echo its be a Empty String
Thanks a Lot!
Variables in batch files can be
Defined: there is a value stored and a associated name used to retrieve the value, that is, the variable name.
Undefined: there is no value and in consecuence there is not any need for an associated name, so it does not exist.
This two simple rules handle how the environment block (where variables/value are stored) is updated. If there is a value, the environment block has an entry for the value and the name to retrieve it. Without a value, the environment block does not create the entry or, when setting the variable to nothing, the entry is removed.
So, if
a never defined variable has not any entry in the environment block
a variable with not value has not any entry in the environment block
there is not any difference between the two cases.
note: While the traditional way to check if a variable stores a value / a variable exists is (as dbenham has commented, this syntax is not recommeded as quotes inside the variable value lead to syntax problems)
if "%varName%"=="" ....
if command extensions are enabled (and the default configuration is to have them enabled) you can also use a safer alternative
if not defined varName ....
if defined varName ....
note that in this syntax, as we are not trying to read the value in the variable, varName is used, not %varName%
There is no difference. Read Delete a variable:
Type SET with just the variable name and an equals sign:
SET _department=
Better still, to be sure there is no trailing space after the = place
the expression in parentheses or quotes:
(SET _department=)
or
SET "_department="
Related
When I set up the ProtonMail Bridge CLI, it generated a hierarchy in my .password-store where the top level is called protonmail-credentials. This contains a subfolder where the name is a gibberish string enclosed in single quotes with--according to wc -c--153 characters. The name of the item under this is itself a 93-character string.
I would like to be able to operate on this item with pass, but wildcards apparently don't work for paths in pass, and dealing with such absurdly long strings which differ from setup to setup by typing everything out is not at all viable.
I am able to isolate the subfolder name with ls -1 .password-store/protonmail-credentials, which gives me the string beginning 'cHJv and continuing onward. I thought I could assign this to an environment variable with export $LONGDIRNAME=$(ls -1 .password-store/protonmail-credentials), but bash throws a 'not a valid identifier' error and thinks I'm trying to set the variable to a string beginning `=cHJv. I do not know why it is adding a backtick and equals sign to the string before attempting to set the variable.
Renaming either of these breaks ProtonMail Bridge's functionality, so mv is not an option either. What should I do with these strings to make the usable in pass?
I have a problem with assigning a string value containg dots to a variable.
For example, after using this assignment:
var txt = "company.com.en";
the variable txt is set to "company.(class)"
How to avoid this behaviour, I want to keep this value unchanged.
OK, I've found my problem. It is really silly, but it is a feature of Debug viewer in Google Apps Script. String variables with dots are shown in this way (with ".(class)" string included). The oryginal value of variable remains unchanged. So, case closed.
The script (hivequery.hql:) looks like this:
Use ${platformType:platformName};
select * from hivetablename;
And this script is being called in a bash script as
#!/usr/bin/env bash
hive -f hivequery.hql
Within an hql file, the use command sets the default database. See Use Database.
The ${platformType:platformName} is Hive's variable notation where platformType is the namespace and platformName is the variable name. This is explained in the Using Variables section of the Language Manual.
If you want to see what value a specific variable has, you can just use set like:
set platformType:platformName;
and it will print out the value. You can also run set; to get a full listing of known variables in all namespaces.
The more correct way to write the construct ${word:word} would be to write ${parameter:offset} . It cause parameter expansion, it expands to the portion of the value of parameter starting at the character (counting from 0 ) determined by expanding offset to the end of the parameter . It has one more variant as ${parameter:offset:length } - Expands to the portion of the value of parameter starting at the character (counting from 0 ) determined by expanding offset as an arithmetic expression and consisting of the number of characters determined by the arithmetic expression defined by length.
So I think basically the in your case , it is meant to get the name of the database from platformType.
For more details on this look into the
Look for Parameter Expansion in the bash man page.
I have a question about the use of string map in TCL.
Is there a way to detect when this function has changed the previous value of the mapped string?
For example, in this case:
set location "default_user: admin"
set new_user "user"
set new_location [string map [list "admin" $new_user] $location]
In this case, I want to know if new_location has a different value than location (without comparing both variables, maybe there is a more elegant way).
My real case is more complicated than this one, I have a variable with the content of a html file, and I want to substitute a specific value for another one or read from another variable if there was no subtitution.
Thanks for your help, I hope everything is clear in the example above.
string map function does not imply a return number of replacements. In order to get the number of substitutions may be used regsub -all function that returns the value. You can also use a string first to value before string map, to learn whether there is a variable line of the desired value.
I have an application where users set up a bunch of objects by filling up a bunch of text boxes which represent values that these objects will take. Just like setting up a Person object which requires you to enter a Name and a LastName properties.
Now I want to introduce global variables that the user will be able to assign values to, or which's values will change during the execution of the program. And I want the user to be able to use them when filling up any object's properties. My first idea was to choose an special character that will mark the beginning of a variable name, and then let the user use the character itself twice to represent the character itself.
For instance, say I have a global variable called McThing. Then, say the symbol I choose to mark the beginning of a variable is %. The user would then be able to enter as a person's last name the string "Mc. %McThing", which then I'd replace using the value of McThing. If McThing's value is "Donalds", the last name would become "Mc. Donalds".
The problem with this is that, if I'd have a variable called He and another called Hello and the user enters "%Hello" as the string I wouldn't know which variable needs to be replaced. I could change my rules to, for instance, use the "%" symbol to mark both the beginning and the end of the variable name. But I'm not sure whether this will cause any other problem.
What would be the simplest possible set of rules to achieve this such that the user will be able to represent every possible string without ambiguities? Ideally, the variable names can have any character but I could restrict their names to a given set of characters.
Your approach of marking both beginning and end with % has one problem. What happens if the input string is %foo%%bar%? Do I get the value of foo and the value of bar? Or do I get the value of foo%bar? (Of course if % in variable names isn't allowed, this isn't a problem.)
The simplest way I can think of to avoid this problem is to use one symbol for the beginning and another (e.g. #) for the end. That should avoid any ambiguity. If the user wants a # in text or a variable name, he escapes it like so: %#. This causes no problems, since empty variable names are not a thing (at least I hope not).
It will be fine and easy to implement on the assumptions that:
You have no empty variable names (i.e. if we see a %% is that a % or an empty variable name?)
Variable names cannot contain %s (i.e. if we see a % in a variable name, is that the end or a %?)
OR
Variable names cannot start or end with % and you cannot have 2 variable names in a row
(i.e. is %a%%b% = a and b or a%b?)
These assumptions will ensure that any %% always represents a % character, and any % always represents the start or the end of the string.
These assumptions might not necessarily be required, but at the very least they will make the implementation a lot more difficult (with the above assumptions, we never have to look more than 1 character forward).
An alternative with no such restrictions, loosely based on the way C/Java/etc. does it:
Have % take on a role similar to \ in C/Java/etc. - use:
%s to denote the start of the string
%e to denote the end of the string
%% to denote the % character
You can also use the same characters to represent the start and end, but, we may as well make them different so it's easier to read.