📜 ⬆️ ⬇️

Learning English and the good old CMD

Sensation! Sensation! Batniki will allow you to learn English in 10 days This post is dedicated to writing a script to achieve a tiny personal and purely applied goal, but, as always when interacting with cmd, the goal itself is much less interesting in those strange ways and surprising ways (what a nice euphemism for the expression that suggests it).



Your advertisement could have been here. This article should have been preceded by a disclaimer, but it turned out long enough and boring to scare away the few readers. Those who wish to familiarize themselves with this opus can find it in the last lines of my post.
')
What did I want from life?

Regardless of the methodologies, teachers and preferences, learning English begins with a very important basic process, which subsequently goes into the background, becomes less priority, but does not stop for quite a long time (never, in the ideal case of an infinitely developing person in a vacuum). And this is nothing more than the replenishment of its vocabulary with new words of a foreign language. The memorization of pairs (word / translation) each organizes in its own way, but most often it is based on repetition. The easiest and fairly good way is cards, on one side of which a word is written, and on the other - unexpectedly - its translation.

  While (not the deck is empty && not tired) do
 {
	 Took a random card
	 Looked the word
	 Suggested translation
	 Turned the card
	 Compared the translation with the standard
	 If (translation coincided with the standard) && (the word in the teeth imposed)
		 removed a card from the deck
 } 


Reading the documentation in English, I suddenly found myself thinking that I really needed such cards. For too often, I came across the following scenario: the encountered unknown word is searched for in the dictionary, after which the brain, without bothering itself with unnecessary efforts, stores it in the cache, embeds it in the context of the sentence, reads the resulting sentence, makes some conclusions, after which it keeps the conclusions in the ROM, and everything else erases as unnecessary. After the third entry into the dictionary because of the same word, I realized that it was time to remember the school years and practice writing out / memorizing new words.

In our age, cards are no longer the place, but the scripts for such purposes are the most. To stop my choice on the bat-nicknames made me a trivial desire to have some fun. For with such tools, even the simplest tasks are sometimes very interesting.

Our design is all

I saw the final use of the script as follows: in the console at any time you can type dict [command [params]] , depending on the command:

How to implement a script call at any time and a no-brainer: put dict.bat in any folder specified in the PATH variable. But the rest of the functionality of some people is a little scary. The thing is that since ancient times the batch file is considered to be a branch of hell on earth, where until some time it was necessary to organize banal branching with the help of the unconditional transfer operator (since only one command was initially supported in the then block). But everything is flowing and changing, the possibilities of cmd have long allowed to imitate the writing of functions, which significantly expands our prospects. And this is done like this:

call: myFunction% param1%% param2%% param3% ...
...
: myFunction
echo Hello, Habr!
exit / b


To return the results of the work, you can use the "global" variables, but for fine connoisseurs there is a more interesting solution - to thrust the result into the return code. However, most often it still requires the use of a variable, but it saves from having to remember its name. A small example:

@echo off
call: Answer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything
echo% errorlevel%
exit / b

: Answer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything
set SIX = 1 + 5
set NINE = 8 + 1
set / a ANSWER =% SIX% *% NINE%
exit / b% ANSWER%


Hard and unsightly life of a bad programmer

But back to the original script. The add function in the zero approximation was simple to the point of insanity — it stuffed the resulting string as a parameter into the vocabulary file:

echo% * >> vocabulary || exit / b 1


However, for such an operation we must be in the directory with the script, and not in the directory from which the script was called. There are a couple of important tricks for this: first, you can find out this directory using the zero script parameter (% 0) with the necessary modifiers and quotes in case of unexpected spaces (“% ~ dp0”). But even knowing it, we cannot execute the banal cd “% ~ dp0”, since the command will give an undesirable result if a) the script is located on another hard disk (try to do cd D: \ temp while on C: \, pleasantly surprised), or b) the location of the script on a network resource. Fortunately, both cases are leveled by the street magic of the pushd “% ~ dp0” command.

It would seem that now you can breathe out calmly: the first and simplest function is written, you can move on to its more complex counterparts. But no, the pitfalls still remain. But to explain them, I have to ask a very strange question: do you poorly display the word ON on the console screen without the help of additional temporary files and manual user input? One batch file banal word ON on a black screen? You try, try, I'll wait here.

...

Funny isn't it? I know only one way to do this: the “echo. ", Moving the cursor to the next line, you can call with the parameters. The construction of "echo.ON" still performs this very difficult task for cmd. Actually, what am I doing? Randomly calling dict add without parameters will entertain you with a very strange new record, unless you add a period to echo. However, the file completion added a minimal check for the correctness of the entered string, which dismissed such tricks. But in other areas of this script, this problem and its solution are completely preserved. I advise in all cases of displaying messages to use only echo. as more predictable.

The show_all function is implemented elementarily: using the for loop with the / f key, it runs through the vocabulary and gives each line to the parser, which displays it with the necessary formatting using the same loop. All this is boring. But the output of the record by its number, which is extremely necessary for the most demanded function show_random, was somewhat more interesting to implement. Ripping a text out of a text file using cmd would have seemed difficult. But the solution turned out to be quite simple and affordable, only for its detection it was necessary to know well the capabilities of the findstr utility. Namely, we are interested in the numbering of lines in the search results: we search by the mask “all possible strings” (. *) With the / n key (numbering), we get the actually numbered vocabulary file, shove the result into a for loop, tear out the pairs (line number / the string itself), and then the matter of technology.

Among other things, in the process of digging in this script, I discovered another funny circumstance. Some people are sorely lacking the ability to create comments not line by line, but in blocks (by analogy with / * such a comment * /). But any text written in an area that will never be executed has all the necessary properties. Thus, you can comment on the text, for example, like this:
goto EndofComment
Comment 1
Comment 2
...
: EndofComment


Or use natural areas. But with the many beloved comments :: the necessary text , it turns out, there are serious problems

So, the final version of the script can be found here: text and dict.bat . Do not forget to use the dict add command at least once before using, otherwise the dictionary will simply have nothing to display.

Not sure that the script is useful to many, but still worth noting: its scope is somewhat wider than stated. If desired, he can help memorize any term / explanation pairs (hypoglycemia: a decrease in blood glucose concentration) or, say, the gender of the rule / second half of the rule (Ji-Shi: write through I). Well, in any case, the main goal of this whole event - to receive pleasure - was fulfilled for me personally. I really hope that you shared it with me. On this remarkable note, I will probably finish the article with the promised disclaimer.

Disclaimer

The format and topic of the article are far from new, which makes it possible to take advantage of the statistically based experience of previous authors and immediately answer a dozen or so NU very common questions.

1. Dear author, where did you find arms of sufficient curvature for writing such an article / script? Here <list of unforgivable-and-terrible errors>
Do not shoot the pianist, he plays as best he can. I am always sincerely grateful for constructive criticism, but I can hardly meet the demands of quite sophisticated people. Fasting is a pen attempt, and scripts are a small hobby. Please treat them accordingly.

2. I did not understand anything / I already know everything. Why did you all write this here?
Sadly, the information can not be super-interesting for all members of the human race of habrabotstva due to personal preferences and differences in existing knowledge. If you ask this question, I sincerely apologize for the time spent, the article was not meant for you. Call me a naive optimist, but I believe that many people who do not ask this question are not empty.

3. Well, for whom we found the article. And who needs the script? Its practical use is negligible.
The purpose of this particular script - the expansion of the user's vocabulary - is more a reason to delve into the possibilities of the old cripple CMD and cram the new guy into solving a bunch of problems with these or other crutches. A nice and interesting warm-up for the brain.

4. Do you even know that with the help of my favorite <bash / powershell / etc> such problems are solved in half-lines?
Judging by the prevalence of this issue, without such a holivar, life loses all its sharpness and color. But let's try to avoid it for the sake of diversity. CMD is a great place for concentrated crutches and hacks, it is sometimes they are very lacking in their free time. The goal of such tasks is to get fun. In some cases, in addition to fun'a, something useful is also obtained.

5. It seems to me that this article is not new and relies on a bunch of other sources.
Of course. She has many important ideological inspirers, who brought into her, as well as in my world view, her own piece, and his name is Legion. But one thing I can not help but identify among others, this article is notorious here bataniki lessons .

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


All Articles