انجمنهای فارسی اوبونتو
تازه کار => انجمن تازهکاران => نویسنده: fzk007 در 20 امرداد 1394، 02:08 بظ
-
سلام دوستان من میخوام یک برنامه ای بنویسم که یک پارامتر از ورودی به عنوان عملگر بگیره و تابع متناسب با اون رو فراخوانی کنه. ولی نمیدونم اشکال کدم چیه. اگر راهنماییم کنید ممنون میشم.
#!/bin/bash
# using a function in a script
function sum {
echo please enter first num
read num1
echo please enter second num
read num2
let s=" $num2 + $num1 "
echo $s
}
function sub {
echo please enter first num
read num1
echo please enter second num
read num2
let su=" $num2 - $num1 "
echo $su
}
function mult {
echo please enter first num
read num1
echo please enter second num
read num2
let mu=" $num2 * $num1 "
echo $mu
}
function div {
echo please enter first num
read num1
echo please enter second num
read num2
let d=" $num2 / $num1 "
echo $d
}
function rem {
echo please enter your num
read num
let mod=" $num % 10 "
echo $mod
}
function help {
echo "This is helpText"
}
count = 0
echo please enter arguman
read arguman
if [$argmun -eq "+"]
then
sum
elif [$argmun -eq "-"]
then
sub
elif [$argmun -eq "*"]
then
mult
elif [$argmun -eq "/"]
then
div
elif [$argmun -eq "%"]
then
mod
else
help
fi
-
ویرایش : هنوز چند تا اشکال داره ، بعدا خدمت میرسم
کد اصلاح شده :
#!/bin/bash
# using a function in a script
function sum {
echo please enter first num
read num1
echo please enter second num
read num2
let s=" $num2 + $num1 "
echo $s
}
function sub {
echo please enter first num
read num1
echo please enter second num
read num2
let su=" $num2 - $num1 "
echo $su
}
function mult {
echo please enter first num
read num1
echo please enter second num
read num2
let mu=" $num2 * $num1 "
echo $mu
}
function div {
echo please enter first num
read num1
echo please enter second num
read num2
let d=" $num2 / $num1 "
echo $d
}
function rem {
echo please enter your num
read num
let mod=" $num % 10 "
echo $mod
}
function help {
echo "This is helpText"
}
count=0
echo please enter arguman
read argmun
if [ $argmun = "+" ]
then
sum
elif [ $argmun = "-" ]
then
sub
elif [ $argmun = "*" ]
then
mult
elif [ $argmun = "/" ]
then
div
elif [ $argmun -eq "%" ]
then
mod
else
help
fi
-
ویرایش : هنوز چند تا اشکال داره ، بعدا خدمت میرسم
کد اصلاح شده :
#!/bin/bash
# using a function in a script
function sum {
echo please enter first num
read num1
echo please enter second num
read num2
let s=" $num2 + $num1 "
echo $s
}
function sub {
echo please enter first num
read num1
echo please enter second num
read num2
let su=" $num2 - $num1 "
echo $su
}
function mult {
echo please enter first num
read num1
echo please enter second num
read num2
let mu=" $num2 * $num1 "
echo $mu
}
function div {
echo please enter first num
read num1
echo please enter second num
read num2
let d=" $num2 / $num1 "
echo $d
}
function rem {
echo please enter your num
read num
let mod=" $num % 10 "
echo $mod
}
function help {
echo "This is helpText"
}
count=0
echo please enter arguman
read argmun
if [ $argmun = "+" ]
then
sum
elif [ $argmun = "-" ]
then
sub
elif [ $argmun = "*" ]
then
mult
elif [ $argmun = "/" ]
then
div
elif [ $argmun -eq "%" ]
then
mod
else
help
fi
ممنونم خیلی لطف می کنید
-
اصلاح شد :
#!/bin/bash
# using a function in a script
function jam {
echo please enter first num
read num1
echo please enter second num
read num2
let s=" $num2 + $num1 "
echo $s
}
function sub {
echo please enter first num
read num1
echo please enter second num
read num2
let su=" $num1 - $num2 "
echo $su
}
function mult {
echo please enter first num
read num1
echo please enter second num
read num2
let mu=" $num2 * $num1 "
echo $mu
}
function div {
echo please enter first num
read num1
echo please enter second num
read num2
let d=" $num2 / $num1 "
echo $d
}
function rem {
echo please enter your num
read num
let mod=" $num % 10 "
echo $mod
}
function help {
echo "This is helpText"
}
count=0
echo please enter arguman
read argmun
if [ $argmun == "+" ]
then
jam
elif [ $argmun == "-" ]
then
sub
elif [ $argmun == "z" ]
then
mult
elif [ $argmun == "/" ]
then
div
elif [ $argmun == "%" ]
then
rem
else
help
fi
چند نکته :
بین براکت دستور if و کلمات داخلش فاصله بزارید .
متغیر رو اینطوری معرفی نکنید : count = 0 . اینطوری count=0 .
یکجا نام تابع rem بود ولی شما از mod استفاده کردید . و چند جا هم اسم متغیر arguman بود که argumn به کار بردید .
بجای ضرب z گذاشتم . وقتی ستاره بزنید با چیز دیگه ای اشتباه میگیره . فایل های دایرکتوری ای که توش هستین رو میفرسته . تابع sum هم وجو داره از قبل یک اسم دیگه باید انتخاب کنید که من اسمش رو jam گذاشتم .
eq هم برای مقایسه ی اعداد هست . برای رشته ها از = استفاده کنید .
موفق باشید .
-
واقعا ممنونم!
Debug کردن تو لینوکس خیلی سخته، کد به ظاهر ساده بود ولی کلافه ام کرده بود.
بازم تشکر
-
خواهش میکنم :)
-
please enter arguman
+
please enter first num
9-22
please enter second num
0
-13