if grep -q 'pattern' '/path/to/file'; then echo "found" else echo "not found" exit 1 fi
Редактировать
Альтернатива:
grep -q 'pattern' '/path/to/file' grep_ec=$? if [ "$grep_ec" -eq "0" ]; then echo "found" else echo "not found" exit $grep_ec fi
From : help имя-модификатора
:
The file name modifiers can be used after "%", "#", "#n", "<cfile>", "<sfile>",
"<afile>" or "<abuf>". ...
...
:s?pat?sub?
Substitute the first occurrence of "pat" with "sub". This
works like the |:s| command. "pat" is a regular expression.
Any character can be used for '?', but it must not occur in
"pat" or "sub".
After this, the previous modifiers can be used again. For
example ":p", to make a full path after the substitution.
:gs?pat?sub?
Substitute all occurrences of "path" with "sub". Otherwise
this works like ":s".
Итак, вместо того, чтобы просто обрабатывать двойные или одинарные кавычки, давайте просто обратная косая черта escape все необычное:
:!ls -l %:gs/[^0-9a-zA-Z_-]/\\&/
Отлично работает с указанным вами тестовым именем файла.
Чтобы использовать абсолютный путь, который может понадобиться для rsync
, вы можете добавить : p
в конце:
:!ls -l %:gs/[^0-9a-zA-Z_-]/\\&/:p
На самом деле, он также отлично работает, если вы сделаете обратную косую черту -escape буквально каждый символ, и это короче:
:!ls -l %:gs/./\\&/:p
Итак, в вашей команде rsync
, вместо %
, используйте %: gs /. / \\ & /: p
.
Основываясь на ответе подстановочного знака с использованием модификаторов имени файла, :S
дает вам именно то, что вы хотите. Согласно документам (:h %:S
),
:S Escape special characters for use with a shell command (see
|shellescape()|). Must be the last one. Examples:
:!dir <cfile>:S
:call system('chmod +w -- '. expand('%:S'))
Чтобы использовать ваш пример:
$ touch '"I'\''m also a n00b.txt"'
$ ls
"I'm also a n00b.txt"
Затем vim '"I'\''m also a n00b.txt"'
, и вуаля:
:!ls %:S
"I'm also a n00b.txt"
Модификатор имени файла :S
доступен в Vim 7.4 .