Catching Command-Line Errors Using %X

Catching command-line errors using %x

So this doesn't directly answer your question (won't capture the command's output). But instead of trying begin/rescue, you can just check the exit code ($?) of the command:

%x(command to run)
unless $? == 0
"ack! error occurred"
end

Edit: Just remembered this new project. I think it does exactly what you want:

https://github.com/envato/safe_shell

was unexpected at this time.

If you're running within a batch/cmd file, you need to double the % markers:

for %%i in (*.txt *.doc) do copy %%i c:\test2

The single % variant only works from the command line.

How to pass command line arguments to a rake task

Options and dependencies need to be inside arrays:

namespace :thing do
desc "it does a thing"
task :work, [:option, :foo, :bar] do |task, args|
puts "work", args
end

task :another, [:option, :foo, :bar] do |task, args|
puts "another #{args}"
Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])
# or splat the args
# Rake::Task["thing:work"].invoke(*args)
end

end

Then

rake thing:work[1,2,3]
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}

rake thing:another[1,2,3]
=> another {:option=>"1", :foo=>"2", :bar=>"3"}
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}

NOTE: variable task is the task object, not very helpful unless you know/care about Rake internals.

RAILS NOTE:

If running the task from Rails, it's best to preload the environment by adding => [:environment] which is a way to setup dependent tasks.

  task :work, [:option, :foo, :bar] => [:environment] do |task, args|
puts "work", args
end

Cmd shows ...unexpected at this time response while calling batch file with parentheses in it's path

Thanks to @ConnorsFan I've figured out the cause. The issue appeared from one of batch files called recoursively from acompc.bat. All of them use ~dp0 param to set some vars and make further calls.

So, in compc.bat there is a string

if "x%AIR_SDK_HOME%"=="x"  (set AIR_SDK_HOME=%~dp0..) else echo Using AIR SDK: %AIR_SDK_HOME%

Nevermind AIR_SDK_HOME variable have been set already or not, you'll get the same problem while calling this batch from a folder containing parentheses in it's path (Adobe Flash Builder 4.7 (64 Bit) in my case).

Because it was impossible to fix the issue from my own batch file, I had to cover the whole assignment with double quotes:

if "x%AIR_SDK_HOME%"=="x"  (set "AIR_SDK_HOME=%~dp0..") else echo Using AIR SDK: %AIR_SDK_HOME%

And that did the job!)

How can you find and replace text in a file using the Windows command-line environment?

A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution.

I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file:

powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"

To explain it:

  • powershell starts up powershell.exe, which is included in Windows 7
  • -Command "... " is a command line arg for powershell.exe containing the command to run
  • (gc myFile.txt) reads the content of myFile.txt (gc is short for the Get-Content command)
  • -replace 'foo', 'bar' simply runs the replace command to replace foo with bar
  • | Out-File myFile.txt pipes the output to the file myFile.txt
  • -encoding ASCII prevents transcribing the output file to unicode, as the comments point out

Powershell.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is C:\WINDOWS\system32\WindowsPowerShell\v1.0

Update
Apparently modern windows systems have PowerShell built in allowing you to access this directly using

(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt

Windows CMD %time% giving same time when used with & operators

Windows command interpreter first preprocesses/parses the entire command line before execution. During this preprocessing all environment variable references in the form %variable% are expanded so that in your case the command line really executed is:

echo 13:44:44.64   & timeout /t 5   & echo 13:44:44.64

It is necessary to force a delayed expansion of the environment variables.

Usually delayed expansion is used in a batch file like this:

setlocal EnableDelayedExpansion
echo !TIME! & timeout /t 5 & echo !TIME!
endlocal

Or on specifying executable TIMEOUT with fully qualified file name and using immediate environment variable expansion on first TIME reference while using delayed expansion on second TIME reference:

setlocal EnableDelayedExpansion
echo %TIME% & %SystemRoot%\System32\timeout.exe /t 5 & echo !TIME!
endlocal

But there is a second method to get a delayed environment variable expansion in a batch file by using the command CALL.

echo %TIME% & %SystemRoot%\System32\timeout.exe /t 5 & call echo %%TIME%%

This command line in batch file results in executing finally the command line:

echo 13:44:44.64   & C:\Windows\System32\timeout.exe /t 5   & call echo %TIME%

The command CALL results in a second parsing of echo %TIME% before executing second ECHO resulting in getting output time about 5 seconds later than first time output.

In a command prompt window a different command line is required due to different interpreting of % in comparison to batch file processing.

echo %TIME% & timeout /t 5 & call echo ^%TIME^%

The percent sign characters are escaped with caret character ^ to be first interpreted as literal characters to finally execute call echo %TIME%. In real it is enough to escape just second % while escaping just first % has not the wanted effect on time output. So working in a command prompt window as expected is also:

echo %TIME% & timeout /t 5 & call echo %TIME^%

But not working as expected in a command prompt window is:

echo %TIME% & timeout /t 5 & call echo ^%TIME%


Related Topics



Leave a reply



Submit