Whats the error in this script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whats the error in this script ?
# 1  
Old 08-11-2010
Whats the error in this script ?

Can someone help me figure out the error with this simple script:


Code:
#!/bin/sh

fact()
{


        if [ $1 -gt 1 ] ; then

                p=`fact expr $1 - 1`
        else
                echo $1
        fi
        echo `expr p \* $1`

}

echo "Enter a number you wish to calculate factorial of"

read NUM

echo "Factorial of $NUM is : " `fact $NUM`

Thanks for help!

---------- Post updated at 07:12 PM ---------- Previous update was at 05:46 PM ----------

Code:
$ ./factorial.sh 
Enter a number you wish to calculate factorial of
3
./factorial.sh: line 7: [: expr: integer expression expected
expr: non-numeric argument
expr: non-numeric argument
Factorial of 3 is : 
$


Last edited by pludi; 08-12-2010 at 02:01 AM.. Reason: code tags, please...
# 2  
Old 08-11-2010
Two things. First, you need a return after your 'echo 1' or you need to move the last echo into the true branch of the if.

Secondly, you need to first do the math before passing the value to the recursive call to fact.

Code:
#!/bin/sh

fact()
{
   if [ $1 -gt 1 ] ; then
        # eval first to do the expr and pass value to fact.  
        eval p=\`fact `expr $1 - 1`\`   
   else
        echo $1
        return      # needed so that you dont echo two things
   fi
   echo `expr $p \* $1`
}

echo "Enter a number you wish to calculate factorial of"
read NUM

echo "Factorial of $NUM is : " `fact $NUM`

If you were using Ksh or bash, the eval statement becomes a bit less muddled:

Code:
p=$( fact $(( $1 - 1)) )

# 3  
Old 08-12-2010
There's really no need for that eval. A simple command substitution will do (with perhaps a variable to hold the result of expr for clarity's sake).

Code:
arg=`expr $1 - 1`
p=`fact $arg`

Or with any posix-compliant sh:
Code:
p=$(fact $(expr $1 - 1))

Or your example:
Quote:
Originally Posted by agama
If you were using Ksh or bash, the eval statement becomes a bit less muddled:

Code:
p=$( fact $(( $1 - 1)) )

which is also posix-compliant.

Regards,
Alister

Last edited by alister; 08-12-2010 at 12:35 AM..
# 4  
Old 08-12-2010
Still getting an error

I modified the script to:


Code:
#!/bin/sh

fact()
{

        if [ $1 -gt 1 ] ; then

                res=`expr $1 - 1`
                p=`fact $res`
        else
                echo $1
        fi
        echo `expr $p \* $1`

}

echo "Enter a number you wish to calculate factorial of"

read NUM

echo "Factorial of $NUM is : " `fact $NUM`


~~~~~~~~~~~~~~~~~~~~~~~~
Code:
$ ./factorial.sh 
Enter a number you wish to calculate factorial of
4
expr: syntax error 
Factorial of 4 is :  24

WHy am I still getting this : "expr: syntax error"

Last edited by Franklin52; 08-13-2010 at 04:19 AM.. Reason: Please use code tags!
# 5  
Old 08-12-2010
You're getting the error because you forgot to add the return after your first echo.

Code:
if [ $1 -gt 1 ] ; then
   res=`expr $1 - 1`
   p=`fact $res`
else
   echo $1
   return        # missing return needed to avoid second echo
fi
echo `expr $p \* $1`

If you don't add the return, the second echo will execute and p will not be set. An empty variable will result in the error from expr. Another solution would be to move your 'echo `expr $p \* $1`' statement into the true branch of the if (right after the p= statement). If you do that you don't need the return.
# 6  
Old 08-13-2010
Thanks that worked. However I thought "echo" also did the return and an explicit return was not needed, as in this script:
Code:
#!/bin/sh

func()
{

        echo 1

}

echo " The result is :" `func`

~~~~~~~~~
Code:
$ ./echotest.sh 
 The result is : 1


Last edited by Franklin52; 08-13-2010 at 04:21 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sh Script whats wrong?

Hi there, i have a problem i have created followning sh files some years ago but now it dosen`t work anymore i never used it a long time. Can anyone find the Error? Its always runs the stop() block and trying to Killing the Server also if i try to start or creat a new one. #!/bin/sh stop()... (6 Replies)
Discussion started by: NewCannon
6 Replies

2. OS X (Apple)

Whats wrong with this shell script!!!!!

hi guys can you tell me if anything is wrong with this script, seems reasonable to me but somehow never works. Script redacted for being too explicit (2 Replies)
Discussion started by: Freddo
2 Replies

3. Shell Programming and Scripting

Whats wrong with my script?

I am trying to find a value within a properties file and declare it into a variable. Script below. I want the "memSize" to be the branch from the properties file. Right now it always tells me "Not found" What am I doing wrong? #!/bin/sh memsize =''; memSize=`sed '/^\#/d'... (8 Replies)
Discussion started by: vsekvsek
8 Replies

4. Shell Programming and Scripting

whats error in code ??

if `egrep -c "safe_mode" /usr/local/lib/php.ini` - gt 0 && " `egrep -c "safe_mode" /usr/local/lib/php.ini` = "On" " then echo " Good " exit else echo " Not Good "; fi and (4 Replies)
Discussion started by: x-zer0
4 Replies

5. Shell Programming and Scripting

Whats the problem whit my script???

I want to take the even-numbered lines from a file and put them in a separate file and the same thing with the odd-numbered lines. #!/bin/bash file=$1 awk ' { if ( NR % 2 == 0) { (( getline < "$file" ) > "even.txt" )} else { (( getline < "$file" ) > "odd.txt" )} } ' $file (4 Replies)
Discussion started by: cristi2008
4 Replies

6. Shell Programming and Scripting

Whats the error in this script

#!/bin/sh #usblcd clear while true; do tail -1 /root/Myprogs/apurva.log > /root/Download/usr/bin/output.txt cat /root/Download/usr/bin/output.txt | echo `awk '{if ($3 == "Connection"&&$4 == "Established") print"Connection Established "} else if {($3 == "DTNCO"&&$4 ==" Sent" && $5="Packet")... (2 Replies)
Discussion started by: appu1987
2 Replies

7. UNIX for Dummies Questions & Answers

Whats wrong in the script?

if then if then echo "fst argument is $1 " else if then "fst argument is $1" fi fi fi Can anyone tell me. My requirement is tht pass a string .. Check whether it contains "-". If yes then check if it... (1 Reply)
Discussion started by: nehagupta2008
1 Replies

8. UNIX for Advanced & Expert Users

Whats wrong in this Script ???

PATH="/clocal/mqbrkrs/user/mqsiadm/sanjay" MAIL_RECIPIENTS="xyz@abc.com" Subject="File accessed in last minutes:" find $PATH -type f -amin -1 > temp.txt.$$ cat temp.txt.$$ | \ while read line do fuser -uV $line >> tempmail.txt done cat "$tempmail.txt" | mailx -s "$Subject"... (4 Replies)
Discussion started by: varungupta
4 Replies

9. Shell Programming and Scripting

Whats wrong with this script?

Hi all, #!/bin/ksh BIN=/interface/Gunner age=$1 directory="$2" && directory=. cd "$directory" || exit 1 from=`$BIN/today -$age` cd $BIN for i in `cat filestoarchive.txt`;do cd $i find . -mtime 14 | grep -v '.tar$' | $BIN/dttmfilter | awk '$1<="'$from'"{ print;};' | \ done (2 Replies)
Discussion started by: kayarsenal
2 Replies

10. UNIX for Dummies Questions & Answers

whats the purpose of the following script?

whats the purpose of the following script? who could run it? To what is the script refering that exceeds 75%? The mailbox? What does sed 's/%//' do? (1 Reply)
Discussion started by: vrn
1 Replies
Login or Register to Ask a Question