📜 ⬆️ ⬇️

It turns out that in Windows there are also redirects, pipes, / dev, / dev / null, / etc / fstab, grub.cfg

Sections can be skipped without compromising understanding, exactly like any strange / uninteresting places in them.

Redirects


As usual:

echo foo > bar echo foo 2> bar echo foo > bar 2>&1 


cat


Yes, in Windows there is an analogue of the many-sided cat command, which is one of the commands expressing the essence of UNIX. This is the type command. It takes one or more arguments — filenames.
')
 type file 


If you want the type to read from the screen (for example, if you need to create a file, fill it with content on the fly), you need to type

 type con > file 


con is the equivalent of /dev/tty (more on this in the next section).
That is, the above command is analogous to the UNIX command cat /dev/tty > file or cat - > file or just cat > file .

When you finish typing the file, click on the new line Ctrl-Z and Enter. This is a sign of the end of the file, an analogue of Ctrl-D in UNIX.

Now there will be a bunch of technical details about Ctrl-D and Ctrl-Z, you can skip them and go to the next section.

In UNIX, when the user presses Ctrl-D, this combination is not processed by the application itself (for example, in GNU / Linux in tty1 it is processed by the kernel, if CONFIG_VT is enabled [by default (2012) CONFIG_VT is enabled by default on the desktop, although there is plans for throwing it out]). A running application receives EOF (End Of File, end of file) in its pure form (and not the key combination itself or the Ctrl-D character). That is, as if the input just ended. In other words, getchar in an application returns EOF, and read returns 0.

On Windows, the same thing, Ctrl-Z and Enter make getchar return EOF.

/ dev / null, / dev / tty, etc.


The /dev/null is a nul device. And it seems to be present in every folder. It stretches from the days of the old versions of DOS, when there were no folders.

Now about other devices. The analogue of /dev/tty is con, from the word console. Moreover, con is an analogy of exactly /dev/tty , and not /dev/console , since in Windows each command window has its own con.

Analogs /dev/ttyS0 , /dev/ttyS1 , /dev/ttyS2 , /dev/ttyS3 are com1, com2, com3, com4 (but Windows still has com5, etc.). These devices are serial console, they are also COM ports, they are also serial ports.

With the usual means of Windows, you cannot create a file or folder named con or nul, etc. But you can fake: mkdir \\.\c:\con .

There is a comic version of why in Windows it is difficult to create a file named con. Namely: Bill Gates had a nickname con, that is, a nerd. For details, see, for example, here: vk.com/wall-31439745_2474 (of course, this information cannot be trusted).

Learn more about these devices here: en.wikipedia.org/wiki/Device_file#MS-DOS .

/ dev, / dev / sda1, ...


After reading the previous section, you might have a question: what, everything? /dev/null , /dev/tty and a few more devices? Where are /dev/sda1 and other countless devices? Indeed, in GNU / Linux, the / dev folder can contain hundreds of devices!

The answer is: yes, in Windows there is a whole folder with such devices. This is \Device . It is difficult to see through the usual user interface. There are devices like \Device\HarddiskVolume1 - this is an analogue of GNU / Linux /dev/sda1 .

These devices appeared in NT-shnyh versions of Windows and did not drag from DOS times, unlike devices from the previous section.

Paypa, they are conveyors


cmd1 | cmd2 cmd1 | cmd2 , same as UNIX.

/ etc / fstab


Its counterpart is the HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices in the Windows registry (yes, yes, in Windows, as in UNIX, there is a mount concept). By the way, if you previously had problems transferring the installed Windows from one partition to another, or renaming disks in Windows or something similar, then this is the solution!

Now there will be a story for those who decided to move the Windows from one partition to another. The rest can skip it and go to the next section.

If, say, you moved Windows from one partition to another, then you need to do two steps: deal with the bootloader and its configs (about this in the next section) and edit this registry key (in general, just like on GNU / Linux: GRUB + fstab). If the key is not edited, the Windows will not be able to boot, so it will start loading from the new partition (that is, the one to which we copied the Windows), and continue - from the old one (that is, the one from which we copied the Windows). All system items will be downloaded from the new, and all sorts of Kaspersky - from the old. So, you need to delete from this branch all entries except the entry "(Default)". Then the Windows will forget everything that they knew about the partitions and the next time they start, they will consider the partition they started from as their partition (which is what we need). In short, automatically identify sections.

True, the question then arises: how to edit the registry of some Windows without loading into it? Hmm, I used Windows PE for this, which was part of the pirated build of Windows, so I won't tell you the details. (See other ways in this thread: geektimes.ru/post/156749/#comment_5355987 from EndUser .)

A few more options:


And finally: using different programs to copy / backup partitions (such as Acronis) is useless: they all do not change the registry (most likely), so they will not work in this situation (copying Windows within the same computer).

And another thing: all the material in this section was checked only on Windows XP.

/boot/grub/grub.cfg


In Windows XP, its counterpart is the boot.ini file in the root of the Windows partition, and in Windows 7 it is the Windows Boot Manager.

Powerful command interpreter


In the standard interpreter (that is, in cmd.exe) there are many different types of branching, as well as in UNIX interpreters. There is even a debag output (a la set -x ), namely echo on , although it is enabled by default and usually the first action in the script turns it off ( echo off , of course).

In general, echo's Windows command is extremely inconsistent: its action in a rather strange way depends on the argument:

cmd.exebash
echo fooecho foo
echo onset -x
echo offset + x
echoshopt -o xtrace


After seeing this table, all the Unixoids, of course, grabbed their heads and said: “What idiots did they design the Windows?” However, in UNIX, too, everything is not going smoothly: why set -x set +x turns on debag, and set +x turns it off ??? The same applies to the other modes enabled by the set command.

Oh yeah, I promised a powerful command interpreter. :) cmd.exe is not, of course. But PowerShell is. One of the features of PowerShell is that it "understands" ordinary UNIX commands, for example, ls. True, in fact, this command is similar to UNIX only by its name and idea, the options for this ls are still completely different. ls is just one of the aliases for Get-ChildItem. And in Windows there is a full-fledged IDE for writing PowerShell scripts with a visual debugger! Can you imagine such a thing in UNIX? Yes, there is of course bashdb , but the visual debugger, of course, is more convenient.

Thanks to Evgeny_Shiryaev , 6opoDuJIo , amdf , easyman , EndUser , risik , NoOne and aik for valuable comments and bug fixes.

Source: https://habr.com/ru/post/156749/


All Articles