Top command-line Questions

List of Tags

I am running a program and want to see what its return code is (since it returns different codes based on different errors).

I know in Bash I can do this by running

echo $?

What do I do when using cmd.exe on Windows?

Answered By: Sigmund Floyd ( 175)

Two ways...

(1) The results are stored in a pseudo environment variable named errorlevel so...

echo Exit Code is %errorlevel%

(2) and a special syntax of the if command:

if errorlevel

see if /? for details.

For Example

@echo off
my_nify_exe.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

Warning: if you set an environment variable name errorlevel, %errorlevel% will return that value and not the exit code. Use (set errorlevel=) to clear the environment variable.

UPDATE (solution)

Months later someone turned me on to an awesome solution to this. If you install Git for Windows, it comes with this console app called Git Bash that does exactly what I descibed. I gather that it is derived from MSYS. It's easy and convenient and gives you all the Unix bash command line conveniences. Tab-completion works the way it does in bash (completes up to the point of ambiguity and doesn't try to guess beyond that forcing you to backspace out of an incorrect completion) and you can control-R to search command history, and history is preserved across restarts.


Can someone recommend a Windows XP program that provides a command line environment? I am an avid Cygwin user, but it doesn't solve my current problem. The problem with Cygwin that's tripping me up is that paths within the Cygwin environment are different from those under Windows, so some things I need to do are failing. My current project requires the Windows environment, not an alternate universe.

I've also considered PowerShell and Windows Services for Unix. I might try them later but for this project, I don't have time to learn a whole programming language that is PowerShell, and I suspect that WSU will have the same path problem as Cywin.

What I'm really looking for to solve my current problem is an app that gives me a Windows console but with some basic amenities added such as...

  • Tab-completion.
  • Being able to cut and paste text appearing in the window.
  • Search command history.
  • Being able to make the window wider than 80 chars.
Answered By: Burnstreet ( 142)

There is also Console: http://sourceforge.net/projects/console/ it is a replacement for the Windows console window and much more flexible (tabs, transparency, presets, etc) For a better tab completion you need a different command interpreter like 4nt / take command.


Alternatively, there is now ConEmu, another console replacement but more configurable and actively maintained than Console.

As for bash-style completion, recent builds of ConEmu integrate with Clink. More info on what Clink can do here. Or you can use TCC/LE with ConEmu. It also offers powerful bash-style completion plus a whole lot more.

140
Thelema

I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of $1 easily:

foo $1 args -o $1.ext

I want to be able to pass multiple input names into the script - what's the right way to do this? Of course I want to handle filenames with spaces in them.

Answered By: Robert Gamble ( 210)

Use "$@" to represent all the arguments:

for var in "$@"
do
    echo "$var"
done

This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that when quoted the arguments are broken up properly if there are spaces in them:

sh test.sh 1 2 '3 4'
1
2
3 4