私はこのコードを持っていますが、エラーを指摘することができません。ユーザーが指定した番号の表を印刷する簡単なプログラムです。
コードは次のとおりです。
#a function to calculate the table
call()
{
#to check if it is in function
echo "in function"
#read from user a no whose table is to print
read num
#taking counter
i=0
echo "going in loop"
while [ $i -lt 10 ]
do
echo "in loop"
#incrementing the counter
i=$(( $i + 1 ))
#s= i + sa
s=$(($i * $num))
##printing the value of num
echo "\t$num * $i =$s"
done
return 0
}
while [ 1 ]
do
echo "in main "
#calling the function call()
echo "caliing call"
call()
#asking user to continue or not
echo "COntinue.. or not [0/1] "
read ch
if [ $ch -eq 0 ]
then
{}
else
exit
fi
done
そして出力はこれです
nik-pc@nik:~$ sh cd.sh
in main
caliing call
12
nik-pc@nik:~$
でbash
、関数を呼び出す(参照する)には、関数の名前を使用するだけで、使用し()
ないでください。
したがって、に変更call()
しcall
て、という名前の定義済み関数を参照しcall
ます。
()
関数宣言時間はまだ必要であることに注意してください。
したがって、たとえば、次のように宣言します。
foobar () { .... ;}
参照しながら:
foobar
sh
(dash
)、参照は、call()
サイレントエラーを上げることなく無視され、シェルは、単にスクリプトの次の行に移ります。
また、確信が持てない限りbash
、予期しない事態を避けるために、これらの種類のスクリプトを実行するために使用することをお勧めします。
また、コードで改善できることがたくさんありますが、これはこの質問の範囲外です。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加