I need help in unix script programming

Hi.
I am a beginner, and i would need some help in resolving a few 'problems'.
The first one is:
I nned to write a unix script that takes as parameters two random length whole numbers whitout signals, and outputs the two numbers product to the standard output. If the multiplication succeded, then it shoud return 0, else 1 to the status output of the command. (if it is 1 then i should put the corresponding error message to the standard error output. The parameters may contain leading 0-s, but the output may not.
And the script may NOT use the bc and dc commands, also these strings may not be part of the script!
This would be one problem, that i didn't understand how to resolve.
If anyone could help me, I would appreciate it.
Thx. Gogo

Comments

  • : Hi.
    : I am a beginner, and i would need some help in resolving a few 'problems'.
    : The first one is:
    : I nned to write a unix script that takes as parameters two random length whole numbers whitout signals, and outputs the two numbers product to the standard output. If the multiplication succeded, then it shoud return 0, else 1 to the status output of the command. (if it is 1 then i should put the corresponding error message to the standard error output. The parameters may contain leading 0-s, but the output may not.
    : And the script may NOT use the bc and dc commands, also these strings may not be part of the script!
    : This would be one problem, that i didn't understand how to resolve.
    : If anyone could help me, I would appreciate it.
    : Thx. Gogo
    :

    Hi,

    maybe something like this?

    [code]
    #!/bin/bash

    PROD=$(( $1 * $2 ))

    if [ $? -eq 0 ]
    then
    echo $PROD
    exit 0
    else
    exit 1
    fi
    [/code]

    Hope this will help.

    regards
    [black]SaThaRiel[/black]

    [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]

  • : Hi,
    :
    : maybe something like this?
    :
    : [code]
    : #!/bin/bash
    :
    : PROD=$(( $1 * $2 ))
    :
    : if [ $? -eq 0 ]
    : then
    : echo $PROD
    : exit 0
    : else
    : exit 1
    : fi
    : [/code]
    :
    : Hope this will help.
    :
    : regards
    : [black]SaThaRiel[/black]
    :
    : [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]
    :
    :

    The only problem with this is that it works only with 'smal' numbers. What i ned is a script that works for verry big numbers. For example: 43563814756947836597346532495 * 34534876520856407634872630

    regards
    gogo
  • : The only problem with this is that it works only with 'smal' numbers. What i ned is a script that works for verry big numbers. For example: 43563814756947836597346532495 * 34534876520856407634872630
    :
    : regards
    : gogo
    :
    Hi,

    then it seems that You must use bc. Can You do this?

    [code]
    #!/bin/bash

    PROD=$(echo "$1 * $2" | bc)

    if [ $? -eq 0 ]
    then
    echo $PROD
    exit 0
    else
    exit 1
    fi
    [/code]
    [black]SaThaRiel[/black]

    [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]

  • : : The only problem with this is that it works only with 'smal' numbers. What i ned is a script that works for verry big numbers. For example: 43563814756947836597346532495 * 34534876520856407634872630
    : :
    : : regards
    : : gogo
    : :
    : Hi,
    :
    : then it seems that You must use bc. Can You do this?
    :
    : [code]
    : #!/bin/bash
    :
    : PROD=$(echo "$1 * $2" | bc)
    :
    : if [ $? -eq 0 ]
    : then
    : echo $PROD
    : exit 0
    : else
    : exit 1
    : fi
    : [/code]
    : [black]SaThaRiel[/black]
    :
    : [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]
    :

    I am not allowed to use bc!
    I think that the solution would be a awk code, that manages big numbers, but 'i'm not at home' yet in awk :-
    can anyone help my?

    thx. gogo

  • : I am not allowed to use bc!
    : I think that the solution would be a awk code, that manages big numbers, but 'i'm not at home' yet in awk :-
    : can anyone help my?
    :
    : thx. gogo
    :
    :
    OK, here it is in awk. You only have to find out how to display the numbers in the correct way (it prints them with exponential notation. Maybe you should use printf instead of print.)

    [code]
    #!/bin/bash

    PROD=$(echo "$1 $2" | awk '{ NUM=$1 * $2 } END { print NUM }')

    if [ $? -eq 0 ]
    then
    echo $PROD
    exit 0
    else
    exit 1
    fi
    [/code]
    [black]SaThaRiel[/black]

    [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]

  • Well thenx, but this i have figured out myself to. But this is not what i need. What a need is some help in writing an own multipliing program for big numbers...

    : OK, here it is in awk. You only have to find out how to display the numbers in the correct way (it prints them with exponential notation. Maybe you should use printf instead of print.)
    :
    : [code]
    : #!/bin/bash
    :
    : PROD=$(echo "$1 $2" | awk '{ NUM=$1 * $2 } END { print NUM }')
    :
    : if [ $? -eq 0 ]
    : then
    : echo $PROD
    : exit 0
    : else
    : exit 1
    : fi
    : [/code]
    : [black]SaThaRiel[/black]
    :
    : [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]
    :
    :

  • : Well thenx, but this i have figured out myself to. But this is not what i need. What a need is some help in writing an own multipliing program for big numbers...
    :
    Finally, Ive found this on the internet:

    http://www.weberdev.com/get_example.php3?count=3851&mode=text

    Its a php-function. Maybe it helps You to solve the problem (maybe using Perl instead).
    [black]SaThaRiel[/black]

    [size=1][blue]Any time things appear to be going better, you have overlooked something.[/blue][/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories