いくつかのタイプのVCSで使用したいbashスクリプトを作成しています。一般的な info コマンドを実行し、戻りコード、成功、またはエラーを確認して、ディレクトリがシステムストアかどうかをテストしようとします。擬似コードで:
if a svn command succeded;
Then run svn commands
elif a darcs command succeded;
Then run darcs commands
elif a mercurial command succeded;
then run hg commands
else
something else
fi
たとえば、コマンドを実行し
darcs show repo
てそれを使用して$?
戻りコードを取得できます。
私の質問は:これを1行で実行し、戻りコード番号を返すきちんとした方法はありますか?例えば
if [ 0 -eq `darcs show repo`$? ];
それとも関数を定義する必要がありますか?
別の要件は、stderrとstdoutの両方を印刷する必要があることです。
ベストアンサー1
戻りコードが自動的に確認される場合:
if (darcs show repo); then
echo "repo exists"
else
echo "repo does not exist"
fi
コマンドを実行し、後で&&(論理的AND)または||(論理的OR)を使用して成功したかどうかを確認することもできます。
darcs show repo && echo "repo exists"
darcs show repo || echo "repo does not exist"
リダイレクトstdout
してstderr
一度だけ実行できます。exec
exec 6>&1
exec 7>&2
exec >/dev/null 2>&1
if (darcs show repo); then
repo="darcs"
elif (test -d .git); then
repo="git"
fi
# The user won't see this
echo "You can't see my $repo"
exec 1>&6 6>&-
exec 2>&7 7>&-
# The user will see this
echo "You have $repo installed"
最初の 2 つのexec
保存stdin
およびstderr
ファイル記述子、3 番目は両方/dev/null
(または必要に応じて他の場所) にリダイレクトされます。最後の2つはexec
ファイルディスクリプタを復元します。その間のすべての内容はどこにでもリダイレクトされます。
Gilesが提案したものなどの他のストレージチェックを添付してください。