Giter VIP home page Giter VIP logo

csub's Introduction

csub

Command Substitution for the Windows Command Shell

Summary

Most Unix-like shells support a technique called command substitution: a way to use the output of one command in the middle of another command. The Windows standard shell (command prompt) lacks this ability, but the csub command provides a simple way to mock this for most situations.

Usage

csub [--help|-h|/?] <command> [<strings>] [`<command>`] [<strings>] [`<command>`] …

Description

In most shells, one can accomplish command substitution by quoting a command with backquotes like so:

> echo It's a fine day in `hostname`-land.
It's a fine day in mycomputer03-land.

In other shells, like Bash, you can also accomplish this by enclosing the command in $(…), like this:

> echo It's a fine day in $(hostname)-land.
It's a fine day in mycomputer03-land.

The csub command provides a way to do this in the Windows command shell — anything in the csub arguments between backquotes is executed and replaced with the output, like so:

C:\> csub echo It's a fine day in `hostname`-land.
It's a fine day in mycomputer03-land.

Limitations

csub cannot change the environment at the calling level

The csub command is executed inside its own context, so commands that would change the environment cannot change the environment of the caller. For example,

C:\qux\baz\frotz> csub cd `echo \qux`

C:\qux\baz\frotz>

In the above example, you'd like the current directory to be C:\qux after running the command. The csub command does change the directory, but only it its own environment; it does not change the environment at the calling level.

Building

This project uses the CMake build tool. CMake is a meta-build system that locates and uses your local development tools to build the project if possible.

To build, first install [CMake][https://cmake.org/]. Then go to the project root directory and run the following command:

cmake -B build

This will locate your installed development tools and configure your project build in the build/ directory. After that, whenever you want a new build, run this command:

cmake --build build

This will build a debug version of the project, located in build/Debug/. To build a release version, run

cmake --build build --config release

You can find the built release executable in build/Release/.

Installation

The built executable is csub.exe, and can be copied anywhere to your command path. There is no Windows installation required for this tool.


Steve Hollasch, [email protected]
https://github.com/hollasch/csub

csub's People

Contributors

hollasch avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

csub's Issues

Add --joinType argument

It would be nice to have control over how multi-line output is joined. Or should that be left to another tool?

Error when using `dir`

The command csub echo -- `dir` -- yields "The system cannot find the file specified.".

Multi-line output yields mangled command

The command csub echo `hex changelog.md` fails with "'R' is not recognized as an internal or external command, operable program or batch file.".

There seems to be some flakiness with packaging command output.

Csub Doesn't properly handle my pipe |

Problem: I tried running the command:

csub adb pull `adb shell "pm path com.android.packageinstaller | sed s/package://g"`

It returned:

'sed' is not recognized as an internal or external command,
operable program or batch file.
adb.exe: pull requires an argument

What I expected:
I expected it to run everything inside the backticks, that is:
adb shell "pm path com.android.packageinstaller | sed s/package://g", and use the ouptut of it in the full command.

When I run the command manually:

C:\Users\Admin\Downloads>adb shell "pm path com.android.packageinstaller | sed s/package://g"
/system/priv-app/PackageInstaller/PackageInstaller.apk

Now I can run adb pull /system/priv-app/PackageInstaller/PackageInstaller.apk and have no errors:

/system/priv-app/PackageInstaller/PackageInstall..., 0 skipped. 14.4 MB/s (2803184 bytes in 0.185s)

As you see, I ran adb shell then put quotes around the shell command, so sed is ran inside of adb shell, not inside of windows. But csub doesnt respect that properly.

Here is another example of an issue with pipes:

C:\Users\Admin\Downloads>dir | findstr test.txt
11/01/2023  06:02 PM           120,606 test.txt

C:\Users\Admin\Downloads>csub echo `dir | findstr test.txt`
Error:  Mismatched ` quotes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.