を判定機能が存在するbash
質問
現在のとなっている一部のユニットテストを実行bash.ユニットテストで初期化され、実行し、清掃、bashを記述します。このスクリプト硬されinit(),execute()の清掃など)。が必須ではありません.い試験場合には定義されていない。
またこれによりgrepingとsedingの源でも間違っています。があり優雅ない。
編集:以下のsnipletようにな魅力:
fn_exists()
{
LC_ALL=C type $1 | grep -q 'shell function'
}
解決
と思いきっておられる方を求めてい'タイプ'コマンドです。とかなるかどうかがわかものは、機能、機能、外部コマンドで定義されていない。例:
$ LC_ALL=C type foo
bash: type: foo: not found
$ LC_ALL=C type ls
ls is aliased to `ls --color=auto'
$ which type
$ LC_ALL=C type type
type is a shell builtin
$ LC_ALL=C type -t rvm
function
$ if [ -n "$(LC_ALL=C type -t rvm)" ] && [ "$(LC_ALL=C type -t rvm)" = function ]; then echo rvm is a function; else echo rvm is NOT a function; fi
rvm is a function
他のヒント
$ g() { return; }
$ declare -f g > /dev/null; echo $?
0
$ declare -f j > /dev/null; echo $?
1
場合を宣言するのには10倍以上の試験のこと思っているのは何が来るのでしょう。
編集:以下の -f
オプションは不要とBASH、お気軽にお任せます。個人的に、私はトラブルを記憶するオプションはあるので、僕を使用します。 -f 表示機能 -F 表示機能名です。
#!/bin/sh
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
function_exists function_name && echo Exists || echo No such function
のオプション"-F"を宣言するのに原因するだけの名前を返した機能ではなく、全体ます。
べきではないので測定可能な水準に達しな性能刑を/dev/nullの場合で、さらに心配するか:
fname=`declare -f -F $1`
[ -n "$fname" ] && echo Declare -f says $fname exists || echo Declare -f says $1 does not exist
はふたつの組み合わせで、自分の無意味で楽しめます。います。
fname=`declare -f -F $1`
errorlevel=$?
(( ! errorlevel )) && echo Errorlevel says $1 exists || echo Errorlevel says $1 does not exist
[ -n "$fname" ] && echo Declare -f says $fname exists || echo Declare -f says $1 does not exist
からの借り入れその他のソリューションやコメントが浮かび上がったこ
fn_exists() {
# appended double quote is an ugly trick to make sure we do get a string -- if $1 is not a known command, type does not output anything
[ `type -t $1`"" == 'function' ]
}
として使用し...
if ! fn_exists $FN; then
echo "Hey, $FN does not exist ! Duh."
exit 2
fi
されているかどうかを確認し、指定された引数で関数を回避redirectionsその他のgrepping.
浚渫古いポスト...っており、最近では、これも代替案の記述:
test_declare () {
a () { echo 'a' ;}
declare -f a > /dev/null
}
test_type () {
a () { echo 'a' ;}
type a | grep -q 'is a function'
}
echo 'declare'
time for i in $(seq 1 1000); do test_declare; done
echo 'type'
time for i in $(seq 1 100); do test_type; done
この生成された:
real 0m0.064s
user 0m0.040s
sys 0m0.020s
type
real 0m2.769s
user 0m1.620s
sys 0m1.130s
を宣言するのはhelluvalotです。
をあげたかについては"宣言"のいずれかチェックを出力または出口のコードです。
出力形式:
isFunction() { [[ "$(declare -Ff "$1")" ]]; }
使用量:
isFunction some_name && echo yes || echo no
しかし、記憶が正しければ、残念をnullによりはるかに高出力の代替(といえば、どとして日付を`cmd`メソッドは何世紀にもわたり、ハンセンド(cmd)ができるようにしております。) 年を宣言するのを返しますtrue/falseの場合がもたらす見つかりませんで機能を返しの終了コードの最後のコマンドの機能も明確な戻り、通常必要はなく、以降の確認エラーコードにより、プレスチェック文字列値では、空文字列):
終了ステータススタイル:
isFunction() { declare -Ff "$1" >/dev/null; }
ことになるだろうとして簡潔であり、良好などを取得します。
かつ迅速な検査のためのさまざまなソリューション
#!/bin/bash
f () {
echo 'This is a test function.'
echo 'This has more than one command.'
return 0
}
test_declare () {
declare -f f > /dev/null
}
test_declare2 () {
declare -F f > /dev/null
}
test_type () {
type -t f | grep -q 'function'
}
test_type2 () {
local var=$(type -t f)
[[ "${var-}" = function ]]
}
post=
for j in 1 2; do
echo
echo 'declare -f' $post
time for i in $(seq 1 1000); do test_declare; done
echo
echo 'declare -F' $post
time for i in $(seq 1 1000); do test_declare2; done
echo
echo 'type with grep' $post
time for i in $(seq 1 1000); do test_type; done
echo
echo 'type with var' $post
time for i in $(seq 1 1000); do test_type2; done
unset -f f
post='(f unset)'
done
出力例:
を宣言する-f
実0m0.037s ユーザー0m0.024s sys0m0.012s
を宣言する-F
実0m0.030s ユーザー0m0.020s sys0m0.008s
タイプgrep
実0m1.772s ユーザー0m0.084s sys0m0.340s
型var
実0m0.770s ユーザー0m0.096s sys0m0.ン160
を宣言する-f(f設定を解除)
実0m0.031s ユーザー0m0.028s sys0m0.000s
を宣言する-F(f設定を解除)
実0m0.031s ユーザー0m0.020s sys0m0.008s
タイプgrep(f設定を解除)
実0m1.859s ユーザー0m0.100番手双糸 sys0m0.348s
型var(f設定を解除)
実0m0.683s ユーザー0m0.092s sys0m0.ン160
なので declare -F f && echo function f exists. || echo function f does not exist.
よう最善の選択です。
fn_exists()
{
[[ $(type -t $1) == function ]] && return 0
}
更新
isFunc ()
{
[[ $(type -t $1) == function ]]
}
$ isFunc isFunc
$ echo $?
0
$ isFunc dfgjhgljhk
$ echo $?
1
$ isFunc psgrep && echo yay
yay
$
この知らせがある場合はそのなかで機能
fn_exists()
{
type $1 >/dev/null 2>&1;
}
特に良かった気がした溶液から Grégoryジョセフ
私は変更ちょっと克服には、"二重引用符醜トリック":
function is_executable()
{
typeset TYPE_RESULT="`type -t $1`"
if [ "$TYPE_RESULT" == 'function' ]; then
return 0
else
return 1
fi
}
からのコメントを別の回答をまた足に戻ってくるとこちらのページ)
$ fn_exists() { test x$(type -t $1) = xfunction; }
$ fn_exists func1 && echo yes || echo no
no
$ func1() { echo hi from func1; }
$ func1
hi from func1
$ fn_exists func1 && echo yes || echo no
yes
う改善いたしました
fn_exists()
{
type $1 2>/dev/null | grep -q 'is a function'
}
ではこのように:
fn_exists test_function
if [ $? -eq 0 ]; then
echo 'Function exists!'
else
echo 'Function does not exist...'
fi
使用可能で'タイプ'ずに外部コマンドがいるので、いまだ終了までの二倍程度の遅いのを宣言するのに'バージョン:
test_function () {
! type -f $1 >/dev/null 2>&1 && type -t $1 >/dev/null 2>&1
}
プラスこなPOSIX shので、全く役に立た場合を除いてトリビア!