2016-04-15 20 views
0

与えられた文字列内の2文字の出現をカウントするスクリプトを作成しています。変数をテスト可能な数値にする方法を理解できません。Bash構文エラー:算術演算で期待されるオペランド

#!/bin/bash 
touch ~/trfindlog.txt ~/trfindt ~/trfindr 
echo $1 > ~/trfindlog.txt 

cat ~/trfindlog.txt | grep -oi r | wc -l > ~/trfindr 

cat ~/trfindlog.txt | grep -oi t | wc -l > ~/trfindt 

varR='/trfindr' 
varT='/trfindt' 

if [[ "${varR}" -eq 0 && "${varT}" -eq 0 ]] 
then 
    echo "This phrase contains no Rs or Ts." 

elif [[ "${varR}" -eq 1 && "${varT}" -eq 1 ]] 
then 
    echo "This phrase contains 1 R and 1 T." 

elif [[ "${varR}" -gt 1 && "${varT}" -eq 1 ]] 
then 
    echo "This phrase contains ${varR} Rs and 1 T." 

elif [[ "${varR}" -eq 1 && "${varT}" -gt 1 ]] 
then 
    echo "This phrase contains 1 R and ${varT} Ts." 

elif [[ "${varR}" -gt 1 && "${varT}" -gt 1 ]] 
then 
    echo "This phrase contains ${varR} Rs and ${varT} Ts." 

fi 

rm ~/trfindlog.txt ~/trfindt ~/trfindr 

exit 

このスクリプトでは、次のエラーが発生しています。

/automount/home/jcampbell/tools/itc/trfind.sh: line 12: [[: /trfindr: syntax error: operand expected (error token is "/trfindr")

/automount/home/jcampbell/tools/itc/trfind.sh: line 16: [[: /trfindr: syntax error: operand expected (error token is "/trfindr")

/automount/home/jcampbell/tools/itc/trfind.sh: line 20: [[: /trfindr: syntax error: operand expected (error token is "/trfindr")

/automount/home/jcampbell/tools/itc/trfind.sh: line 24: [[: /trfindr: syntax error: operand expected (error token is "/trfindr")

/automount/home/jcampbell/tools/itc/trfind.sh: line 28: [[: /trfindr: syntax error: operand expected (error token is "/trfindr")

ここに作業スクリプトがあります。これは自分自身を教育するためのキック&のためです。私は様々な答えを受けることがうれしいです。

#!/bin/bash 
touch ~/trfindlog.txt 
echo $1 > ~/trfindlog.txt 

varR=$(echo $1 | tr -cd r) 
varT=$(echo $1 | tr -cd t) 

if [[ "${#varR}" -eq 0 && "${#varT}" -eq 0 ]] 
then 
    echo "This phrase contains no Rs or Ts." 

elif [[ "${#varR}" -eq 1 && "${#varT}" -eq 1 ]] 
then 
    echo "This phrase contains 1 R and 1 T." 

elif [[ "${#varR}" -gt 1 && "${#varT}" -eq 1 ]] 
then 
    echo "This phrase contains ${#varR} Rs and 1 T." 

elif [[ "${#varR}" -eq 1 && "${#varT}" -gt 1 ]] 
then 
    echo "This phrase contains 1 R and ${#varT} Ts." 

elif [[ "${#varR}" -gt 1 && "${#varT}" -gt 1 ]] 
then 
    echo "This phrase contains ${#varR} Rs and ${#varT} Ts." 

fi 

rm ~/trfindlog.txt 

exit 
+0

すぐに問題になるのは、エラーが発生した変数にその内容ではなくファイル名を割り当てることです。 – tripleee

+0

これは慣れていない場合、 'rcount = $(grep -oi 'r' <<<" $ 1 "| wc -l)のような変数で結果を得ることができます。 – tripleee

答えて

0

あなたはgrepがマッチした行の数ではなく、一致の数をカウントすること

var=$(grep -c r ~/trfindlog.txt) 

ノートのようなものを使用する必要があります。あなたは、同時にスクリプトの2つのインスタンスを実行する場合、静的なファイル名で、壊れる、ファイル内の変数を置く

var=$(echo $1 | tr -cd r) 
echo ${#var} 
3

、面倒な洗練され、かつ:だから、あなたが本当に必要なもののようなより多くの何かがあります。これらのすべては、メモリ内の変数とのより簡潔なものになります。

Bashを使用すると、変数のコピーを作成して単純な置換を実行できます。

Rs=${1//[!R]/} 
Ts=${1//[!T]/} 

ここで、これらの文字列の長さは、探していた文字の出現回数です。

echo "We have ${#Rs} R characters and ${#Ts} T characters." 

sを複数印刷するかどうかを決めるだけで簡単に追加できます。ヒント:最初の文字列が正確にRの場合は、sを抑制します。しかし、柔軟な言葉遣いが必要な場合は、可能性についてcaseというステートメントを使用する方が簡単かもしれません。

case $Rs:$Ts in 
    :) echo "We have none of either";; 
    R:) echo "We have one R and no Ts.";; 
    :T) echo "We have no Rs and one T.";; 
    R:T) echo "We have one of each.";; 
    *:) echo "We have ${#Rs} Rs and no Ts.";; 
    *:T) echo "We have ${#Rs} Rs and one T.";; 
    :*) echo "We have no Rs and ${#Ts} Ts.";; 
    R:*) echo "We have one R and ${#Ts} Ts.";; 
    *:*) echo "We have ${#Rs} and ${#Ts} Ts.";; 
esac 

それでもcornermostケースとして上記:R:Tを処理し、残りの場合にはより小さな断片から文字列を生成するように誘惑されるであろう。

+0

私は大文字を下限の代わりに使いました。それを変更する。 – tripleee

関連する問題