Why can't I write to an environment variable? - cmd

I am trying to figure out why I get an error when trying to set an environment variable.
I write the following command in a cmd.exe shell.
set logFile=%time:~0,2%
but I keep on getting the following error message
Environment variable 13 not defined
Can someone assist?
GorovDude

That is really strange. Your code should work. It seems that the batch is ignoring the logFile= in your code.
To verify this try:
set logFile
to see if logFile is indeed not set.
Have you tried typing the code directly into the command prompt?

Related

Throw error in YAML file (from python) if environment variable is not set

I have a YAML file in which I use environment variables like this
torch_cache_dir: $TORCH_CACHE_DIR
I exported TORCH_CACHE_DIR variable in the terminal I am working on. Everything is working correctly. The YAML file is being used in a python-centric environment (i.e. the YAML file is used by a python script).
However, if I remove the exported variable by doing
unset TORCH_CACHE_DIR
and then run my program, everything still works and I would like instead to get an error thrown since my YAML file cannot find the TORCH_CACHE_DIR variable. How can I do it? Is that possible?
Thanks for your help.

Windows Batch variables available outside a script

I'm creating a script that is to create a variable called http_proxy. The script does a bit more than just set the proxy, it has a few statements in there as well as a prompt for user password on load.
I have set up a shortcut to cmd.exe with an extra parameter /k ".set_http_proxy.bat" to run on startup, which sets this variable.
Once the script exits, the command prompt remains open for the user to run their scripts. My problem is, the variable http_proxy has vanished now and no trace that it was set in the script that just ran.
Is there a way to set a variable that will remain in use for that session until the command prompt window is closed? I think in bash we just use export which is great!
current code is simply...
set http_proxy=http://proxy.address
If that's all, then it should work exactly as you expect, and in fact it did so for me when I tried it out.
Unless you use setlocal or launch another process for running a batch file, then environment variables persist even after the batch file finishes.

Shell script error in UNIX but works in CentOS - source: not found

myconf.sh
setting1=val1
setting2=val2
export setting1
export setting2
Then I call this conf file in my runner.sh, but I get the error "runner.sh: source: not found". Initially I was using source myconf.sh when calling it inside and saw in some posts that I should be using "." instead of "source".
myrunner.sh
#!/bin/sh
. myconf.sh
echo "$setting1"
echo "$setting2"
I'm calling myrunner.sh through this command
sh myrunner.sh
Please let me know if I'm actually doing something wrong. I'm able to call it properly without errors in my CentOS image but when we execute it in a UNIX box I'm hitting the error.
Thanks!
What is your $PATH env var set to?
You may just need to include the current directory in your path or prefix the filename.
Example: source ./myconf.sh
ETA: As noted; putting the current directory . into your path may not be the most desirable thing. I suggested it simply to explain why your script may work on one system but not another.

how to read content of a file in a environment variable in windows command line?

is there an equivalent in Windows Command to the following command in bash :
variable='cat mytextfile'
I found answers on the net, but they use loop or complicated ways... they're is probably a simple way to do it?
I use the following command, which works for me
set /p myVar=<myFile.txt

StringToBytes Issue in Expect Script

I am using the below command in my Expect script file.
set name [StringToBytes "Tamin"];
puts "name = $name"
When i run this script, i get the below error.
invalid command name "StringToBytes"
while executing
"StringToBytes "Tamin""
invoked from within
"set name [StringToBytes "Tamin"]"
Can anyone please help me to fix this?
As far as I know, this means that the command StringToBytes could not be found. StringToBytes is not listed in the documentation, so presumably you intended to define this command yourself?

Resources