Tuesday, 2 October 2012

Continuing long lines in a batch script

Unintentionally I found for myself that a batch script has a nice feature to break long lines without loosing of possibility of reading them.

Imagine, you have some weird combination of commands as below that is not put completely in one line of a screen.

some-command-with-many-arguments | another-lomg-command-with-its-own-arguments | etc | etc | etc

If the line of a code cannot be shown within one line on a screen it will be i) either turned and the rest of line will be shown below on one or more lines or ii) continued out of edges of the screen with a horizontal scrolling bar. Both of them are not convenient because we not able to observe the code entirely.

Fortunately, modern batch scripts (in XP, Vista, Win7) have blocks implemented by brace:

(
    some-command-with-many-arguments 
) | (
    another-long-command-with-its-own-arguments 
) | etc | etc | etc

I known this feature (because I used brace in the if-else and for statements) but did not realised right now.

So, this gives us good possibility to break the long line into a several lines without loosing of functionality. This is better than nothing and is better than undocumented feature using the DOS-like escaping character ^ at the very ending of the line because you have to look after that this character is in the lastest character of the line. See the same example with the esacaping character:

some-command-with-many-arguments | ^
another-long-command-with-its-own-arguments | ^
etc | etc | etc

Unix shell scripts are featured to continue long commands using by backslashes since the begining of the Unix wolrd.

some-command-with-many-arguments \
| another-long-command-with-its-own-arguments \
| etc | etc | etc

No comments:

Post a Comment