For debug purposes, how can I see if a variable is set in b2 ?
aka. test if a variable is defined in b2
This is not perfect, but it is possible to tell if a variable is defined using an echo and Variable expansion:
:E=value
Assign value to the variable if it is unset.
Example:
echo "Variable FOO has value $(FOO:E=was_not_previously_set)" ;
Will display:
Variable FOO has value was_not_previously_set if FOO was unset before the call to echo.
Related
gforth : how to check a variable is set
I currently set a variable from command line like :
: functionname
variable !
;
so it gets its value from
gforth -e"5" myfile.fs
but then I would ensure the variable is set by a default value even if user runs
gforth myfile.fs
how can I check variable is set ? (then I can give it a default value in a if statement)
finally I changed method for reading the parameter :
first the function is now like :
: valorise_hauteur
argc # 2 < if
cr
." no argument using default 5 value"
5 hauteur !
cr
else
1 arg s>number drop hauteur !
endif
;
and I can call the full script :
gforth myscript.fs 10
where 10 is my arg number 1
this method is more command line way to do that & permit to use a default value.
I have a batchscript mybatch in which I try to store the first user argument in a variable called FILE
set FILE = %1
if defined FILE (
echo defined
echo do something with %1
) else (
echo not defined %1
)
If I execute my batch via mybatch test1 I get always not defined test1. Why is variable FILE not defined?
You have unwanted spaces in your variable assignment, so you have defined a variable with a space in the name that always has a value beginning with a space. Your IF statement is checking if a variable without a space exists.
See Declaring and using a variable in Windows batch file (.BAT)
I recommend your first line should be:
set "FILE=%~1"
You can try like this :
#echo off
set "FILE=%~1"
if Exist "%FILE%" (
echo.
echo "%FILE%" Exist
echo do something with "%FILE%"
) else (
echo "%FILE%" is not defined
)
Pause
i'd like to test if %PROGRAMFILES(X86)%\Folder exists but it seems like if EXIST doesn't like environment variables :/
my way would be :
if EXIST %PROGRAMFILES(X86)%\Folder (
set VAR=%PROGRAMFILES(X86)%\Folder
)
no matter how i try, it always outputs false...
if EXIST "%PROGRAMFILES(X86)%\Folder" (
set "VAR=%PROGRAMFILES(X86)%\Folder"
)
To check whether an environment variable exists:
if defined VARIABLE echo Yep, it's defined.
The following also works and prints the current value of the variable if set.
set VARIABLE && echo Found it! || echo Nope, sorry!
Note that SET also responds to prefixes, though, so if you have a var named VARIABLE, then "set var" and "set v" will also return true.
Not the question you asked, but it was how I read the title, so somebody else might have the same question.
So let's say I set a bunch of variables in my .zshrc:
function print_color () {
echo -ne "%{\e[38;05;${1}m%}";
}
# color guide: http://misc.flogisoft.com/_media/bash/colors_format/256-colors.sh.png
RED=`print_color 160`
DEFAULT="%{$fg[default]%}"
PROMPT='$RED%${DEFAULT} $ '
The problem is these remain set, once I am actually running commands
$ echo $RED
%{%} # With colors, etc.
How would I unset them after use in my .zshrc? What is the best practice here?
Thanks,
Kevin
Unsetting a parameter is done with the built-in unset
unset PARAMETER
unsets PARAMETER. This can be done as soon as you no longer need PARAMETER, at the end of the file or anywhere inbetween.
"Best practice" depends highly on the use case:
In the case of colors you probably want them to be available from top to bottom, so that you can use the same colors in the whole file. Therefore you probably want to unset them near bottom of ~/.zshrc.
If you are just storing some output for a few lines of code, which you do not need in any other part of the file, you can unset it immediately after the last usage or at the end of the block of code in question.
Also, if you need a parameter only inside a function, you can declare it with local
function foo () {
local PARAMETER
# [...]
}
It will then be only available inside this function without the need for unset.
That being said, in this case you actually need RED to be available in your running shell as it is needed each time your prompt is evaluated (everytime just before it is printed). This is due to PROMPT being defined in single quotes (and the shell option PROMPT_SUBST being set, see output of setopt | grep promptsubst).
If you do not intend to change RED during runtime just put it in double quotes when defining PROMPT:
PROMPT="$RED"'%${DEFAULT} $ '
This will substitute $RED when PROMPT is defined and only ${DEFAULT} each time PROMPT is evaluated.
After that you can just do unset RED. Note that there must be no $ before RED, else the shell would substitute it by the value of RED and try to unset a parameter named like the value:
% FOO=BAR ; BAR=X
% echo "> $FOO | $BAR <"
> BAR | X <
% unset $FOO
% echo "> $FOO | $BAR <"
> BAR | <
% unset FOO
% echo "> $FOO | $BAR <"
> | <
A possibly better solution than unsetting variables is to make them local in a function, so that if they were in the environment at the shell startup, then wouldn't be lost:
putprompt()
{
local RED=`print_color 160` DEFAULT="%{$fg[default]%}"
PROMPT="$RED%${DEFAULT} \$ "
}
then just execute the putprompt function.
Just put unset RED at the end of .zshrc.
In my Windows batch file I have a various amount of variables. Lets say I have the following variables:
set varTest1=test1
set varTest2=test2
set otherVar=variable500
set varS=string
set yetAnotherVar=foo
They do really make no sense buts thats not the point. I am looking for a method that prints out all values of variables that start with var:
So when I run my batch with a certain help parameter it should print out all three variables starting with var and its value.
The output could look like this:
These are the available variables:
varTest1 : test1
varTest2 : test2
varS : string
I created the following for reading the parameter:
IF "%1" == "" (
echo No help parameter was set. Program will exit. ) ELSE (
IF "%1" == "help" (
call :showAllAvailableVars ) ELSE (
echo Do something else))
Now I would have my method
:showAllAvailableVars
I think the solution could be something with the findstr method but I could not figure it out how to do that because findstr is mainly for files and not for searching through own program variables.
Create array instead of different variables. Like,
set var[0]=test1
set var[1]=test2
set var[2]=string
then in your 'showAllAvailableVars' function do this
for /L %%i in (1,1,%n%) do echo !var[%%i]!
You could use set var to print all variables which begins with var.
See also set /?