Can't execute Ruby file using Powershell - ruby

I want to execute a ruby file using windows 10 powershell. In my powershell terminal, when I type
ruby test.rb
I receive
ruby: The term 'ruby' is not recognized as the name of the cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
At line:1 char:1
+ ruby test.rb
+Category Info :ObjectNotFound: (ruby:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
These commands don't work either
ruby.exe test.rb
rb.exe test.rb
I'm in the right directory. I suspect the problem has to do with PATHs, but I don't understand PATHs yet. I have been trying to execute .rb files in both Powershell and my beginner IDE, Visual Studio Code.
Thanks

It is absolutely a problem with your path. Try this:
$Path = 'C:\Path\to\ruby.exe'
& $Path args
Make sure your root folder for ruby.exe is in the $env:Path variable.
$env:Path -split ';'

For me I had to kill all instances of powershell -- in my case vscode then relaunch and it picked up the new path environment variables and ruby worked!
Sometimes a full restart is what the doctor prescribed.
I've also had cases where you had to run the program as an administrator to work and have the proper updated new path variable.

In powershell, navigate to the directory where your ruby file is stored. It will look something like this C:\Users\Joe\Desktop\Ruby_Practice_folder>
Type
#!/usr/bin/env ruby
Hit enter. Type
ruby your_ruby_file.rb

Related

After uninstalling Conda 3.8 cmd became unavailable

Overview
I uninstalled Conda and it broke cmd. I am unable to start cmd and I also get an error when opening a PS terminal. When I installed Conda I ran conda init for ps and cmd. I would like to reverse the effect of this command. I no longer have conda and so cannot use that.
Powershell behaviour:
conda : The term 'conda' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again. At line:1 char:1
conda init --reverse
+ CategoryInfo : ObjectNotFound: (conda:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Cmd behaviour:
Doesn't open, brief flash of a window.
Resolution
In the registry, there is a key; HKEY_CURRENT_USER\Software\Microsoft\Command Processor\autorun with the value set to if exists. I removed that key's value. If you have a statement that runs batch scripts, don't just bin it.
What is this key?
According to this resource the key is used to run batch scripts prior to opening cmd.
Why?
if exist was evaluating to false and that blocked cmd from starting. I tested with a dummy expression if exist 1=1 which led to cmd starting.
Culprit
This happened when uninstalling Conda 3.8.

Anaconda Command Prompt Window Won't Open After Reinstallation

I have had to uninstall and then reinstall miniconda. After reinstalling, when I run, the powershell prompt shows the following message:
& : The term 'C:\Users\jenj0\anaconda3\Scripts\conda.exe' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\jenj0\Documents\WindowsPowerShell\profile.ps1:4 char:4
+ (& "C:\Users\jenj0\anaconda3\Scripts\conda.exe" "shell.powershell" "h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\jenj0\...ripts\conda.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
(base) PS C:\Users\jenj0>
Additionally, the miniconda command prompt window doesn't launch at all.
I tried looking for solutions. For example, I tried this, but when I try to launch cmd.exe, I see a quick flash of a window opening and then closing and nothing happens.
I have also tried uninstalling/reinstalling miniconda again and had the same issue. I was doing that because I was having issues to run a program. Now I am even further away from solving the problem.
There is some invalid paths in the startup scripts for powershell and also the registry keys for cmd, stemming from your previous anaconda installation.
You say that you uninstalled anaconda and then installed miniconda, so I am assuming that the path
C:\Users\jenj0\anaconda3
does not exist and is now
C:\Users\jenj0\miniconda3
Here is what you do:
Powershell (from your error message) is trying to read C:\Users\jenj0\Documents\WindowsPowerShell\profile.ps1 which contains references to the invalid path. Backup said file and then delete any lines that contain the invalid path
cmd (and anaconda prompt). Run regedit and then delete the reference to the invalid path in the keys at
Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Unexpected char when executing .ps1 file using PuTTY Plink

I'm executing remotely some scripts to get information from a server, using Plink tool from putty. The trouble comes when I use a .ps1 file, because one '?' appears on the beginning, making the first line incorrect, but with .bat files works as desired.
For example, I want to print the content of a file:
GetDate.bat:
type C:/Data/DateOfCompilation.txt
And then:
PS C:/Users/MyUser> plink -ssh <User>#<IP> -i C:\Key.ppk -m C:\Scripts\GetDate.bat
10/09/2018 14:32:02,72
Everything okay
GetDate.ps1:
Get-Content -Path C:/Data/DateOfCompilation.txt
Execution:
PS C:/Users/MyUser> plink -ssh <User>#<IP> -i C:\Key.ppk -m C:\Scripts\GetDate.ps1
?Get-Content : The term '?Get-Content' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again. At line:1 char:1
+ ?Get-Content -Path C:/Data/DateOfCompilation.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (?Get-Content:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Also, if I add more code, the other lines work fine, it's just the first one which fails with that '?' added at the beginning.
(However, running locally the script works fine)
I have other ps1 scripts much more extended, so using only bat files is not the best option.
I have looked at the documentation, other forums and here, but I'm not able to find anything. Maybe I do not know anything about ps1 files.
Check if there's UTF-8 BOM at the beginning of GetDate.ps1 - If there is, remove it.
Though the root cause your problem may be your misunderstanding of that -m switch of Plink does. It makes Plink read the file and send its contents (and only the contents) to the server. The server never learns, what the file extension is. So it makes no sense to use .ps vs .bat. No matter, what the extension is, the file will be interpreted by the default shell of your Windows SSH server. What is PowerShell (according to the error message).
So even your type command in .bat file was executed by PowerShell, not by cmd.exe. In PowerShell, type is an alias to Get-Content.
The reason why your .bat file works is most probably that it has no BOM, while your .ps1 has BOM. Had you executed your .ps1 with PowerShell, it would handle the BOM correctly. But you are not executing .ps1 with PowerShell, you are executing its contents, and in that case the BOM probably causes problems.
The difference is basically like between:
powershell.exe -File GetDate.ps1
and
powershell.exe < GetDate.ps1
Both do basically the same, but the latter fails with BOM, while the first handles it correctly.

The term 'go' is not recognized as the name of a cmdlet, function, script file, or operable program

After running...
go run main.go
I get this error
go : The term 'go' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
SOLVED: Go to visual studio code (assuming this is installed) ctrl + shift + p to open command window type
Go: current GOPATH
this should give you a path create a system environment variable named GOPATH and add that path into that variable.
Make sure the system environment variables are set to:
GOPATH is set (in my case) to C:\Users[username]\go
GOROOT is set to C:\go\
IDE is Visual Studio Code.
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
write the code to the terminal and run it. Eazy way to solve the problem.
I had same issue. Then found the below solution on the book "Learning Go" of O'REILLY.
If you are on a Unix-like system using bash, add the following lines to your .profile.
(If you are using zsh, add these lines to .zshrc instead):
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
You’ll need to source $HOME/.profile to make these changes take effect in your cur‐
rent terminal window.
On Windows, run the following commands at the command prompt:
setx GOPATH %USERPROFILE%\go
setx path "%path%;%USERPROFILE%\bin"
After running these commands, you must close your current command prompt and
open a new one for these changes to take effect.
A couple of solutions:
Run refreshenv to refresh the PATH environment variable in the current terminal session.
PS C:\> refreshenv
Refreshing environment variables from the registry for powershell.exe. Please wait...
Finished
PS C:\> go version
go version go1.18.4 windows/amd64
Open a new terminal window and try again.
Restart PC and try again.
I was facing similar issues for commands installed via pip, I had to add the full path to Scripts folder to the PATH variable
C:\Users\username\.pyenv\pyenv-win\versions\3.x.y\Scripts
For go too I believe something similar is to be done.

'gitk --all' command not being recognized in Windows 8 Powershell

gitk is not working for me. I get the error saying "the term 'gitk' is not recognized as the name of a cmdlet, function, script file, ...."
One interesting thing I noted was that inside C:\Program Files(x86)\Git\bin\, there was a gitk file, but it had no extension. Most other files in that folder had a .exe extension.
I also made sure that C:\Program Files(x86)\Git\bin\ is specified in the PATH environment variable.
Also, I tried adding C:\Program Files(x86)\Git\cmd\ to PATH because it contains a gitk.cmd file, but that did not help either.
Edit : I was using Powershell earlier, since I have poshgit installed. I switched over to bash, and I was able to do gitk --all from there.
I just got it to work after having the same problem.
At the PowerShell prompt it typed: "Bash" and hit return.
This put me into a bash shell.
Then I typed "gitk" at the Bash prompt and it worked.
So that gitk file is a UNIX shell script and requires the shell to work.
I suppose you could always have one of your Power Shell window in Bash mode and then this tool is available.

Resources