Windows Command Line: Non Evaluation of Environment Variable - windows

I would like to provide the raw text referring to an environment variable to a command instead of evaluating the environment variable.
I need this to configure BizTalk from the command line, for example:
BTSTask.exe AddResource -ApplicationName:App1
-Type:System.BizTalk:BizTalkAssembly -Overwrite
-Source:..\Schemas\bin\development\App1.Schemas.dll
-Destination:%BTAD_InstallDir%\App1.Schemas.dll
This command adds a resource to a BizTalk application. I want the destination to be %BTAD_InstallDir%\App1.Schemas.dll, however at present it is evaluating the environment variable (to nothing) and using \App1.Schemas.dll.
Is it possible to escape or disable the evaluation of this environment variable while parsing\executing this command?
I have tried escaping the first and both percentage characters with a carrot (^), however this did not stop the evaluation.
[EDIT] When I execute this at the command prompt it doesn't replace the environment variable, however it does when I run it as a script, any thoughts as to why this is different?

Try echo ^%path^% in a command prompt it prints...
path
instead of expanding the environment variable so I guess the following should work for you as suggested by Mikeage
BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll

Did you try:
%%BTAD_InstallDir%%
in your script ?
That should prevent the script to interpret the variable, and it would pass %BTAD_InstallDir% to the program.

Try ^% instead of %.

Tried:
C:\PrgCmdLine\Unix\echo.exe "%"JAVA_HOME"%"
Got:
%JAVA_HOME%
[EDIT] Indeed, C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^% works too, and is simpler...
[EDIT 2] For the record: I used UnxUtils' echo to have the behavior of a plain program. Built in echo has a slightly different behavior, at least for quoted % signs.

Not sure if it's the same as my case, but i was troubling to use a batch file to create a script which has %temp% variable inside.
The workaround i found:
set test=%temp;
echo {command} %test%%>>path_to_my_batch_file;
Hope this helps someone:)

Related

How to pass multiple variables in shell script

I have a shell script that I'm trying to write to a file using multiple variables, but one of them is being ignored.
#!/bin/bash
dir=/folder
name=bob
date=`date +%Y`
command > $dir/$name_$date.ext
The $name is being ignored. How can I fix this?
Have you noticed that the _ was "ignored" as well? That's a big hint.
If you use set -u, you'll see the following:
-bash: name_: unbound variable
The way bash parses it, the underscore is part of the variable name.
There are several ways to fix the problem.
The cleanest is the ${var} construct which separate the variable name from its surroundings.
You can also use quotation in various ways to force the right parsing, e.g.: "$dir/$name""_$date.ext"
And in case your variables might contain spaces (now, or in the future) use quotation for words.
command >"$dir/${name}_$date.ext"
command >"${dir}/${name}_${date}.ext"
Both these are fine, just pick one style and stick to it.

Run simple programme using unix shell

I'm new to unix and its developing. In my new.sh script I wrote
$USERNAME=user
$PASSWORD=sekrit
echo $USERNAME
and ran new.sh using bash new.sh
But I get the following errors
new.sh: line 1: =user: command not found
new.sh: line 2: =sekrit: command not found
How do I run that command and print the username variable in terminal?
USERNAME is the name of the variable. $USERNAME is the replacement (aka contents, aka value). Since USERNAME is empty, you effectively try to run a command named =user, which is what the error message tells you.
Remove the $ from $USERNAME=... and it will work.
As Jens notes in his answer, the problem is that an assignment to a variable is not prefixed with a $, so:
USERNAME=user
PASSWORD=sekrit
is the way to write what you wanted. You got an error because USERNAME was not set, so after expansion, the shell looked at the command as:
=user
=sekrit
and it could not find such commands on the system (not very surprisingly). However, be aware that if you have previously written:
USERNAME=archipelago
PASSWORD=anchovy
then the lines:
$USERNAME=user
$PASSWORD=sekrit
would have been equivalent to writing:
archipelago=user
anchovy=sekrit
You could see that by running set with no arguments; it would show you the values of all the variables set in the shell. You could search for words such as USERNAME and archipelago to see what happened.
Now you've learned that, forget it. The number of times you'll need to use it is very limited (but it is handy on those rare — very rare — occasions when you need it).
For all practical purposes, don't write a $ on the left-hand side of a variable assignment in shell.

'export' is not recognized as an internal or external command

I need to set Maven options in machine. I ran following command and I got 'export' is not recognized as an internal or external command
export MAVEN_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
'export' command is valid only for unix shells.
In Windows - use 'set' instead of 'export'
I tried set, didn't work for me, in the end:
$env:
worked. But you have to put your string in quotes like:
$env:SPOTIFY_CLIENT_ID="XXXX"
SET does work as mentioned above.
In case anyone wants to know how to use the data stored in variable use %variable%.
if you want to print value in command prompt use echo %variable%
ex:
SET myVar = testsite.co.uk
To print in command prompt:
echo %myVar%
To use in code, just use %myVar%
Was facing the same issue.Turned out I was doing a rookie mistake. Try this instead:
set MAVEN_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
I am using this way:
$MAVEN_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
Use "git bash here" on Windows it will work. You can install git from https://git-scm.com/downloads

How to export dot separated environment variables

Execution of
user#EWD-MacBook-Pro:~$ export property.name=property.value
Gives me
-bash: export: `property.name=property.value': not a valid identifier
Is it possible to have system properties with dot inside? If so how do that?
As others have said, bash doesn't allow it so you'll have to use your favourite scripting language to do it. For example, in Perl:
perl -e '$ENV{"property.name"} = "property.value"; system "bash"'
This will fire up a subshell bash with the property.name environment variable set, but you still can't access that environment variable from bash (although your program will be able to see it).
Edit: #MarkEdgar commented that the env command will work too:
env 'property.name=property.value' bash # start a subshell, or
env 'property.name=property.value' command arg1 arg2 ... # Run your command
As usual, you only require quotes if you need to protect special characters from the shell or want to include spaces in the property name or value.
I spent better part of this afternoon trying to figure out how to access some property set by Jenkins (to pass a job parameters jenkins uses property format with a dot) - this was a good hint from Adrian and yes it works for reading properties in the script too. I was at a loss as to what to do but then I tried:
var=`perl -e 'print $ENV{"property.name"};print "\n";'`
This worked quite well actually. But of course that works in a shell that was started with the property set in the environment already i.e. in Adrian's example this could work in a script started from bash instance invoked in perl example he provided. It would not if this perl sniplet was put in the same shell only directly after his perl example.
At least I learnt something this afternoon so not all this time is a waste.
If you export those properties to run an application, some programs can support setting system property as an option, and allow . in the property name.
In Java world, most of tools support setting system property by -D option, e.g. you can set system property with dot like this -Dproperty.name=property.value.
Bash only permits '_' and alpha numeric characters in variable names. The '.' isn't permitted.
http://tldp.org/LDP/abs/html/gotchas.html

Difficulty executing shell script with directory reference

I have a simple script
...
dir=`pwd`
echo $dir
cd ./selenium-grid-1.0.8/
CMD="ant -Dport=$1 -Dhost=$2 -DhubURL=http://172.16.1.137:4444 -Denvironment="$3"-DseleniumArgs="-firefoxProfileTemplate C:/software/rc_user_ffprofile -multiWindow" launch-remote-control"
echo $CMD
$CMD 2>&1
#End
Whenever i run this command, i get: ./register_rc.sh: line 16: C:/software/rc_user_ffprofile: is a directory
this directory has to be an argument to the -firefoxProfileTemplate option. How do i include that in this string without it baffing??
help
thnx
I believe your command should read:
CMD="ant -Dport=$1 -Dhost=$2 -DhubURL=http://172.16.1.137:4444 -Denvironment=\"$3\"-DseleniumArgs=\"-firefoxProfileTemplate C:/software/rc_user_ffprofile -multiWindow\" launch-remote-control"
The backslashes are used to "escape" the quotation marks.
The answers here telling to escape your quotes are wrong. That will pass those quotes directly to ant, I doubt that's what you want.
What's the reason to store the command in a variable? It's a very bad idea. Why can't you just write that command as is? If you want to achieve modularity or code reuse, then define a function.
If you want to display executed commands, use set -x.
Looks like you're mixing your quotes up. Take a look at the syntax highlighting that StackOverflow did for you.
I recommend generating the CMD variable in multiple steps, and make sure you \-escape your quotes.

Resources