-1

I have this Fibonacci script

#!/bin/bash
if (test $# -ne 1) then
 echo "Use: 0ドル number"
exit 1 fi
for c in ‘seq 1 1ドル‘
do
 if (($c == 1))
 then
n0=1;
 echo "fibonacci(1)=$n0";
 elif (($c == 2));
 then
n1=1;
 echo "fibonacci(2)=$n1";
 else
 n=$(($n0+$n1));
 echo "fibonacci($c)=$n";
 n0=$n1;
 n1=$n;
fi done
type here

I have to made a substitution of the expression evaluation $(($n0+$n1))with the shell command expr $n0 + $n1.

I Rembember that to assign the output of a command to a varible i have to made command > output.txt but I don’t know how to apply it on this exercise. Thank you so much for your help.

asked Oct 21, 2024 at 12:34
3
  • This is more Bash than C, check you bash syntax here and update your tags. Also if you do a command > output.txt it wont be in a variable but in a file in your current directory. Commented Oct 21, 2024 at 12:41
  • Please edit your question and make clear what problem you want to solve. As already suggeste, paste your script at shellcheck.net and fix all errors/warnings first. Then run it again. Do you get an error message? Wrong output? What do you expect, what do you get? What do you mean with "I have to made a substitution of the expression evaluation $(($n0+$n1))with the shell command expr $n0 + $n1."? The grammar is wrong. Do you mean "I have made a substitution..." or "I have to make a substitution..." Should this be a question? Using Bash arithmetic should be faster than running expr. Commented Oct 21, 2024 at 13:05
  • Sorry , I’m Italian and probably my English is not good. I have this script of Fibonacci, I run it and all it works. But my book tell me : Substitute the instruction for expression evaluation $(($n0+$n1)) with the shell command expr $n0 + $n1 (be carefull: spaces are important!). Rembember how to assign the output of a command to a varible. it’s an exercise that I found on an old book and I don’t have the solution. I started today with bash programming and I’m totally new to this world. Sorry if my question is stupid Commented Oct 21, 2024 at 13:09

1 Answer 1

2

I think it's important to state up front: expr is not needed for arithmetic in virtually any shell. Continue using n=$(( $n0 + $n1 )) in practice.

That said, arithmetic was previously not supported by shells directly, and instead you used the expr command. It accepted an expression as a series of arguments, and wrote the result to standard output. You capture standard output using the $( ... ) construct (similar to arithmetic expressions, but with one pair of parentheses instead of two).

# n=$(($n0 + $n1))
n=$( expr "$n0" + "$n1" )

Note that the number of arguments is important; expr 3 + 5 outputs 8, but expr "3 + 5" outputs 3 + 5.

answered Oct 21, 2024 at 13:18
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you su much , especially Thank you for explaining to me the correct use of this instruction. I wrote wrongly n = expr ( $n0 + $n1 )

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.