Как вставить результаты команды unix в CLI tcsh?

278
Bryan

Я часто чувствую, что хочу создавать файлы и папки с датой.

Например, я хотел бы иметь возможность создать папку с меткой даты yymmdd под названием «160408-projectA», набрав:

mkdir <ctrl-;>-projectA

Я пытался использовать bindkey -cs '^;' '`date +%y%m%d | perl -pe chomp`'

... но там написано "плохой ключ spec ^;" Даже при переключении на другую клавишу, например ^ o, он выдает ошибку «160408: Команда не найдена».

Это возможно с tcsh?

0
Тьфу. Это звучит возможно; Вы, вероятно, можете понять это. Тем не менее, обратите внимание, что если у вас есть список чисел, например 160408, есть лучший подход, чем полуавтоматика, которую вы ищете. Лучше всего использовать навыки программирования для создания полной автоматизации, читая из списка по мере необходимости. Слишком часто люди бросаются вперед с первым решением, которое они могут увидеть, вместо того, чтобы отступать и думать о том, что было бы более идеальным. Теоретически, насколько это автоматизировано? Если ваша последовательность шагов точно определена, это обычно может быть сделано программой. TOOGAM 8 лет назад 0
Уточнил, что «160408» это отметка даты в ггмдд. @TOOGAM, не уверен, какой "список" вы имеете в виду. И я пытался выяснить это довольно долго, прежде чем спросить здесь. ;) Bryan 8 лет назад 0

1 ответ на вопрос

0
TOOGAM

I would suspect that Ctrl-; isn't possible. Before going into why, let me discuss what is possible.

bindkey -s '^;' '`date +%y%m%d | perl -pe chomp`'

Just take the "c" off of your attempt to use Ctrl-O, and it works fine.

The tcsh command probably does not support Ctrl-;, which is a very sensible design, because tcsh is meant to work with a traditional Unix terminal (or some software which behaves similarly), and traditional Unix terminals don't support Ctrl-;.

So, why don't Unix terminals support Ctrl-;?

The main point for all of these Ctrl sequences was to provide an easy way to type/represent the first 32 ASCII characters because those characters do not have easily type-able characters. The straight-forward equivalent to Ctrl-; isn't all that challenging to type without the need of a Ctrl sequence, which is why no special Ctrl character (like Ctrl-;) is commonly supported.

I will elaborate. First, I'll note that much of this answer was made referencing some information I documented, about Ctrl Sequences, on my website at ][CyberPillar][: Ctrl Keyboard Sequences.

One of the rules for Ctrl characters is that if you hold Ctrl and press a character with an ASCII value of 63 through 95, you'll end up with the character that has 64 less of an ASCII value than the character you pressed. (Yes, 64 is subtracted from 63, if applicable. That gets discussed more later.)

Another rule is that if you hold Ctrl and press a character with an ASCII value of 96 through 122, you'll end up with a character that is 96 less than the character that you typed. As a result, Ctrl-Shift-A (upper-case letter) and Ctrl-a (lowecase letter) will end up with the same character. That's why if you press Ctrl-c, you may see Ctrl-C echoed back. The terminal converts the ASCII 3 to a string representing Ctrl-C when preparing the output message, ignoring the fact that you pressed Ctrl-c to generate the ASCII 3.

; (Semi-colon) is ASCII 59, which is not ASCII 64 through 95 nor ASCII 96 through 122. Therefore, the rules that were just specified (covering ASCII 63 through 122) do not provide any common interpretation for ASCII 59 (to cover Ctrl-;).

One value, which feels like an exception, is the commonly supported rule, which is for Ctrl-?. If Ctrl-; did have a common interpretation, then the common interpretation would probably follow the same pattern as Ctrl-?. The ? character is ASCII 63. So subtracting 64 from 63 yeilds -1, which basically equates to 127 with an underflow condition, which can be ignored. The result is that Ctrl-? keystroke combination effectively ends up adding 64, so 63 + 64 = 127. ASCII 127 often corresponds to the Delete key, which may be challenging to represent since the Delete has a common special behavior (which is to delete text). So ASCII 127 may frequently benefit substantially by having a commonly supported Ctrl sequence. This way, a person can type the ASCII 127 code relatively easily, by using the keyboard sequence.

Following that pattern (of adding 64), Ctrl-; would result in ASCII 123 which is a left curly brace ("{"). People did not support Ctrl-; as a common standard way of typing { since { has its own relatively-easy way to enter it on the keyboard (which is by pressing Shift-[).

Возможно, проблема с терминалом, но с: `` bindkey -s ^ o '`date +% y% m% d | perl -pe chomp`'`` ... когда я набираю "mkdir"это дает мне:` `mkdir` date +% y% m% d | perl -pe chomp` `` (я на OS X, RH6 делает то же самое.) Bryan 8 лет назад 0
Да, это ожидается. Тогда продолжайте печатать. Так `Mkdir-projectA` становится `mkdir` `date +% y% m% d | perl -pe chomp``-projectA`, в результате чего создается папка, которую вы просили. Он не показывает дату в командной строке, но делает именно то, что вы просили. Вы также можете сделать: bindkey -s ^ i '! `Ls -t | tail -1! `'(замените!' на обратные метки, которые, как мне кажется, не могут быть указаны в этом комментарии, как хотелось бы) TOOGAM 8 лет назад 0
О, хорошо, я надеялся, что она выполнит команду, а затем поместит результаты в командную строку, чтобы я мог точно видеть, что будет создано, прежде чем нажать Enter. Опция -c для bindkey, похоже, близка, мне кажется, что мне нужна комбинация двух. Bryan 8 лет назад 0