PDA

View Full Version : Replacing "echo " with "result=" does not work, but I don't understand the error.



s3a
March 8th, 2017, 12:49 AM
Hello, fellow Linux lovers. :)

I'm trying to write the BASH equivalent of the following Java syntax.:


public int factorial(int n) {
if(n>1) {
return n * factorial(n-1);
} else {
return 1;
}
}

int result = factorial(5);

System.out.println(result);


This works (and prints out 120, as intended).:

#!/bin/bash

function f() {
n=$1;

if [[ n -gt 1 ]]; then
echo $(($n*$(f $(($n-1)) )));
else
echo 1;
fi
}

f 5;



This does not work.:

#!/bin/bash

result=-1;

function f() {
n=$1;

if [[ n -gt 1 ]]; then
result=$(($n*$(f $(($n-1)) )));
else
result=1;
fi
}

f 5;

echo $result;

The error it gives is as follows.:

./script
./script: line 9: 2*: syntax error: operand expected (error token is "*")
./script: line 9: 3*: syntax error: operand expected (error token is "*")
./script: line 9: 4*: syntax error: operand expected (error token is "*")
./script: line 9: 5*: syntax error: operand expected (error token is "*")
-1

Could someone please help me find the BASH-equivalent of the Java syntax above?

Any help in getting me to achieve with BASH what the Java syntax above shows would be GREATLY appreciated!

halogen2
March 8th, 2017, 01:54 AM
That code has two problems -

1) You're missing a $ in the "if" line.

2) Your f function outputs nothing, yet you are asking for its output in a mathematical operation. Thus the syntax errors you're seeing.


If you fix those problems...

#!/bin/bash

function f() {
n=$1;

if [[ $n -gt 1 ]]; then
n=$(($n*$(f $(($n-1)) )));
else
n=1;
fi

echo "$n";
}

echo $(f 5);
... does it work?

s3a
March 8th, 2017, 08:09 AM
Oh! Thanks. :)

I have another question, though.:
How come the echo statement within the function in the BASH syntax that worked in my first post actually printed something [like System.out.println() in java], but yours is more like a return statement from Java [which is what I want] [such that, if I comment out the "echo $(f 5);" (without the quotes), the entire script doesn't print anything.]

halogen2
March 8th, 2017, 05:56 PM
How come the echo statement within the function in the BASH syntax that worked in my first post actually printed something [like System.out.println() in java], but yours is more like a return statement from Java [which is what I want]
It's actually equivalent.

bash doesn't really have a concept of "return value". In bash, for every command, you have two things - "exit code" and "output". You are using the latter as a sort of "return value".

That difference between your working code and mine is mostly "cosmetic".


[such that, if I comment out the "echo $(f 5);" (without the quotes), the entire script doesn't print anything.]
Well, that's because you're then only defining a function f, but not calling it. So nothing seems to happen.

kevdog
March 13th, 2017, 04:25 AM
Here a nice article explaining the concept of return value in Bash functions. halogen2 is correct -- there is no such thing as a pure function return value in bash scripts like other languages.http://www.linuxjournal.com/content/return-values-bash-functions