Когда я grep нет конкретного файла терминал перестает работать

1103
Doug Fir

Я учусь использовать оболочку. Играя с некоторыми основными командами, я столкнулся с проблемой. Это довольно простой материал, и я прошу прощения, если это расстраивает некоторых людей.

На моем рабочем столе есть файл с именем «myFirstShellScript.txt». В этом файле есть текстовая строка «show system».

Таким образом, если при открытии оболочки я набираю cd desktopи затем grep, "show system" myFirstShellScript.txt"то оболочка возвращает предложение, в котором появляется эта строка. Хорошо.

Но я хотел возиться. Затем я напечатал grep "show system"и нажал Enter. Я задавался вопросом, будет ли grep искать весь рабочий стол, так как это был / это мой pwd.

Но, похоже, произошло то, что терминал перестал работать. Сначала я думал, что это может быть просто поиск, но это было какое-то время. Затем я проверил, работают ли другие команды - pwd - ничего не делает, когда я нажимаю клавишу ввода, курсор просто перемещается на новую строку.

Затем я попытался выйти. Я попытался "выйти", "конец" и "выйти". Ничего такого. Окно терминала не отвечает, как ожидалось.

Я знаю, что могу легко открыть другую вкладку, но хотел спросить, что я сделал с терминалом здесь?

0
Нажмите Ctrl-C. Это не вопрос программирования. Sean Bright 10 лет назад 1

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

3
Kevin

When you run grep, if you include a filename (grep pattern file), it searches that file. When you don't, it searches the standard input (stdin). If you include it in a pipe (foo | grep), it reads from the pipe. When you run just grep pattern, it reads input from the console. This is a very common pattern among Unix and Linux utilities. When you type exit, end, quit, or anything else, it compares it with the pattern and prints it if it matches, just as if you had given it a file. Try entering a line that matches the pattern - you'll see it echoed back to you.

To get out of this, hit Ctrl+D to signal the end of the file. Ctrl+C will kill most programs, but it's less clean than Ctrl+D.

2
Jens

Grep without any arguments reads from the standard input, which is your terminal. It will keep on reading until it encounters the end-of-file character. The end-of-file character is Ctrl-D. Type that, and grep is done.

If that doesn't work, run stty -a and look for eof = ... to find your eof-character.

Gotcha alert: don't type Ctrl-D twice; the second one will be read by your shell. If it's your login shell it will think you're done and exit. Whenever your login shell exits (just like when you type exit) your login session is over and the login: prompt appears again. In other words, you are logged out.

Похожие вопросы