26

このステートメントでは、バージョン ($var2) がパス /app/$var1 (アプリケーション名) に存在するかどうかを照合しようとしています。

if 
find /app/$var1 -maxdepth 1 -type l -o -type d | grep $var2 #results in a nice list where i can manually get a true match.
# if match is found then execute command.
$realcmd "$@"
    rc=$?
    exit $rc
else
echo "no match, the version you are looking for does not exist"
fi

現在のコード: これにはすべてのコードが含まれます (クリーンアップされていません)。私が実行するコマンド:「./xmodule load firefox/3.6.12」このバージョンは終了します

#!/bin/bash
# hook for some commands

#echo $@  #value of sting that is entered after "xmodule"

cmd=$(basename "$0")
#echo "called as $cmd"

if [[ $cmd = "xmodule" ]]
then
    realcmd='/app/modules/0/bin/modulecmd tcsh'

    # verify parameters
fi
# check if $@ contains value "/" to determine if a specific version is requested.
case "$@" in
*/*)
    echo "has slash"
var1=$(echo "$@" | grep -Eio '\s\w*') # Gets the aplication name and put it into var1
echo $var1   # is not printed should be "firefox"
var2=$(echo "$@" | grep -o '[^/]*$')  # Gets version name and put it into var2 
echo $var2 
# Checking if version number exist in /app/appname/
if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then
    $realcmd "$@"
    exit $?
else
    echo "no match, the version you are looking for does not exist"
    # Should there be an exit here?
fi
    ;;

*)
    echo "doesn't have a slash"
    ;;
esac

出力: mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12 'has slash

3.6.12 一致しません。お探しのバージョンは存在しません

空白がある場所 (3.6.1 以上) には、アプリケーション名が必要です。私は今、これが私の問題であるに違いないことを認識しています。これは、おそらく/ app. しかし、コードのその部分で何かを変更したとは思いません。

4

2 に答える 2

19

マンページからgrep

選択した行が見つかった場合、終了ステータスは 0 になり、見つからなかった場合は 1 になります。エラーが発生した場合、終了ステータスは 2 です。

つまり、 の直後にblah blah | grep $var2、戻り値を確認するだけです。

パイプラインの終了コードは、そのパイプラインの最後のプロセスの終了コードであるため、次のようなものを使用できます。

find /app/$var1 -maxdepth 1 -type l -o -type d | grep $var2 ; greprc=$?
if [[ $greprc -eq 0 ]] ; then
    echo Found
else
    if [[ $greprc -eq 1 ]] ; then
        echo Not found
    else
        echo Some sort of error
    fi
fi
于 2012-10-23T06:45:05.497 に答える