Нет, ssh не поддерживает регулярное выражение в ssh_config (1)
и примером, который Вы дали, не являются регулярные выражения. ssh_config (1)
поддержки PATTERNS
, т.е. можно определить шаблон для дюйм/с, т.е.:
Host 192.168.1.*
ProxyCommand ssh production-server nc %h %p
и необходимо смочь иметь передачу для всего внутреннего дюйм/с. Другое решение состояло бы в том, чтобы добавить записи в /etc/hosts
для определенного дюйм/с, т.е.:
192.168.1.10 pro10
192.168.1.11 pro11
и создайте одну запись в Вашем ~/.ssh/config
файл:
Host pro1?
ProxyCommand ssh production-server nc %h %p
if [[ ! -e cli.tar.gz ]] || ! tar -xvf cli.tar.gz -C ~/ ; then
echo "cli.tar.gz can not be found (or tar failed). Exiting" >&2 && exit 1
fi
или, если Вы хотите избежать if
полностью:
( [[ ! -e cli.tar.gz ]] || ! tar -xvf cli.tar.gz -C ~/ ) && { echo "cli.tar.gz can not be found (or tar failed). Exiting" >&2 ; exit 1 ; }
if [[ -e cli.tar.gz && ( $(tar -xvf cli.tar.gz -C ~/) && $? != 0 ) ]]; then
echo "cli.tar.gz can not be found (or tar failed). Exiting" >&2 && exit 1
fi
tar ...
не условное выражение, и логика является неправильной).
– Uwe
13.11.2013, 12:58
Способ, которым Вы используете &&
и ||
как список, не условное выражение.
Lists
A list is a sequence of one or more pipelines separated by one of the
operators ;, &, &&, or ⎪⎪, and optionally terminated by one of ;, &, or
<newline>.
Of these list operators, && and ⎪⎪ have equal precedence, followed by ;
and &, which have equal precedence.
A sequence of one or more newlines may appear in a list instead of a
semicolon to delimit commands.
If a command is terminated by the control operator &, the shell executes
the command in the background in a subshell. The shell does not wait for
the command to finish, and the return status is 0. Commands
separated by a ; are executed sequentially; the shell waits for each
command to terminate in turn. The return status is the exit status of
the last command executed.
AND and OR lists are sequences of one of more pipelines separated by the
&& and ⎪⎪ control operators, respectively. AND and OR lists are executed
with left associativity. An AND list has the form
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of
zero.
An OR list has the form
command1 ⎪⎪ command2
command2 is executed if and only if command1 returns a non-zero exit
status. The return status of AND and OR lists is the exit status of the
last command executed in the list.
Если Вы смотрите на это ТАК названные Вопросы и ответы: Простые логические операторы в Bash, @Gilles ответ как почти столь краток, как это добирается с точки зрения объяснения, как спорить с если/затем блоки в Bash.
Так пересмотренная версия Вашего if/then
оператор:
[[ -e cli.tar.gz && `tar -xvf cli.tar.gz -C ~/` ||
echo "cli.tar.gz can not be found. Exiting" >&2 && exit 1
был бы похож на это:
if [[ -e cli.tar.gz && ! $(tar xvf cli.tar.gz -C out) ]]; then echo "cli.tar.gz can not be found. Exiting" >&2; exit 1; fi
Или расширенный для удобочитаемости:
if [[ -e cli.tar.gz && ! $(tar xvf cli.tar.gz -C out) ]]; then
echo "cli.tar.gz can not be found. Exiting" >&2
exit 1
fi
Демонстрационные данные. Содержание tarball:
$ tar ztvf cli.tar.gz
drwxrwxr-x saml/saml 0 2013-11-13 08:26 1/
drwxrwxr-x saml/saml 0 2013-11-13 08:26 1/2/
drwxrwxr-x saml/saml 0 2013-11-13 08:26 1/2/3/
drwxrwxr-x saml/saml 0 2013-11-13 08:26 1/2/3/4/
Информация о файле:
$ ls -l cli.tar.gz
-rw-rw-r-- 1 saml saml 146 Nov 13 08:27 cli.tar.gz
$ ls out/
1
Выполнение нашей команды:
$ if [[ -e cli.tar.gz && ! $(tar xvf cli.tar.gz -C out) ]]; then echo "cli.tar.gz can not be found. Exiting" >&2; exit 1; fi
Ничего не произошло. Если мы удаляем выходной dir. out
:
$ mv out out_ORIG
И повторно выполненный:
$ if [[ -e cli.tar.gz && ! $(tar xvf cli.tar.gz -C out) ]]; then echo "cli.tar.gz can not be found. Exiting" >&2; fi
tar: out: Cannot chdir: No such file or directory
tar: Error is not recoverable: exiting now
cli.tar.gz can not be found. Exiting
Что случилось? Для одного Вы обычно не хотите смешивать выполнение команд в если/затем, поскольку Вы пытаетесь сделать. Существует слишком много проблем, которые могут спутаться.
Скорее я структурировал бы свой код как это, так как он дает нам самый легкий путь для того, чтобы заниматься проблемами, как они открываются, вместо того, чтобы попытаться создать некоторый смешной код который даже Вы автор будет изо всех сил пытаться через 6 месяцев понять что Вы делали!
if [[ -e cli.tar.gz ]]; then
cmdOutput=$(tar xvf cli.tar.gz -C out 2>&1);
if [[ $? != 0 ]]; then
echo "cli.tar.gz can not be found. Exiting" >&2
exit 1
fi
fi
$ ls -l |grep -E "cli|out"
-rw-rw-r-- 1 saml saml 146 Nov 13 08:27 cli.tar.gz
drwxrwxr-x 3 saml saml 4096 Nov 13 09:28 out
Теперь выполните нашу команду (ничего не происходит):
$ if [[ -e cli.tar.gz ]]; then cmdOutput=$(tar xvf cli.tar.gz -C out 2>&1); if [[ $? != 0 ]]; then echo "cli.tar.gz can not be found. Exiting" >&2; exit 1;fi;fi
$
Теперь работайте, наша команда (без dir представляют):
$ rm -fr out
$ ls -l |grep -E "cli|out"
-rw-rw-r-- 1 saml saml 146 Nov 13 08:27 cli.tar.gz
$ if [[ -e cli.tar.gz ]]; then cmdOutput=$(tar xvf cli.tar.gz -C out 2>&1); if [[ $? != 0 ]]; then echo "cli.tar.gz can not be found. Exiting" >&2; exit 1;fi;fi
cli.tar.gz can not be found. Exiting
; exit 1;
потому что это закрыло терминал GUI за пределами сценария. Таким образом не было никакого закрытия;
. Все извинения! Некоторый не сознающий js-или perl-ishness, должно быть, заставили меня полагать, что заключительная точка с запятой была не важна... колотят, часто кусает меня, lol. Будет моя бдительность. – goldilocks 13.11.2013, 16:23