-4

I'm trying to write a program that converts number bases. For example, when converting from base 10 to base 2, the input 5 will result in the output 101.

This is my code:

#!/bin/sh
while read line
do
convert_base()
{
    number=$1
    inputbase=$2
    outputbase=$3
    echo "obase=$outputbase;ibase=$inputbase;$number" |bc
}
convert_base $1 $2 $3
echo $line >> $2
done < $1

This gives me a syntax error, though. How can I fix this?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 3
    Post the code here instead of the image – Taha Tariq Dec 04 '17 at 16:36
  • 2
    Put your code in the question, not a link to a screen shot. It makes it easier for somebody to copy/paste the code and tinker with it. – sniperd Dec 04 '17 at 16:37
  • @Cyrus It doesn't helped me. I'm trying to do something, English is not even my motherlanguage trying to write something for my education. But you guys are not welcoming who is just meet the coding. – Mert Karadayi Dec 04 '17 at 16:58
  • 1
    Welcome to Stack Overflow! To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Dec 04 '17 at 16:59
  • I would remove the `loop`, ie. `while read line .. do` and the last line `done<$1`. Also remove the complete line `echo $line >> $2`. Now you have something that should work.`chmod +x myScript.sh` your scriptfile on the command-line. Run as `myScript.sh 5 10 2` and that should work. Now you'll be working for a known place. If need to add more features, try 1 feature and get that to work before adding another. If you still have trouble then copy/paste the exact invocation and error messages into your Q, ELSE the readers have to guess to much about what you mean by "syntax error" Good luck. – shellter Dec 04 '17 at 17:43

1 Answers1

1

This can be much simpler:

while read num inb outb ; do echo "obase=$outb;ibase=$inb;$num" | bc; done
5 10 2
101
17 8 10
15
stark
  • 12,615
  • 3
  • 33
  • 50