Find the answer to your Linux question:
Results 1 to 3 of 3
Alright, So I have unix class at school, and honestly, my teacher does not explain much of anything, I have had to go to google just to finish my homework ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    1

    100 % newbie question bash script


    Alright, So I have unix class at school, and honestly, my teacher does not explain much of anything, I have had to go to google just to finish my homework assignments. They have been simple up til now, We ned to make a bash script that adds, multiplies divides or subtracts two numbers. the input needs to be something like ./math 23 p 65 and the output will be the answer. honestly, i have NO idea how to start. he constantly goes about telling us to "figure it out ourselves" so if i go to him with the question, it would be a lost cause. any suggestions on howto just get started? im not asking for it to be done cause i do want to learn it, but i just dont know anything about bash scripts and it was just thrown at us.

    Thanks in advance everyone

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, so you need to tackle two main things for your script:

    1) how to pass arguments to your script (e.g., the "23", "p", and "65" in your example above).

    2) how to do the actual arithmetic

    the first one is easy: look up the positional parameters in the Bash man page. You definitely will want to know how to use man (short for manual) pages. to read the bash man page, in a terminal do:

    Code:
    man bash
    then look for he positional parameters. Hint: you can search in a man page using a forward slash (/) then the string you are looking for, e.g.:

    Code:
    /Positional Parameters
    Hint: the first argument is $1, then second is $2, etc.

    The arithmetic is fairly straight-forward, too. Here is a good write-up on it:

    Arithmetic Expansion

    Hint: If u try to do complicated match (like floating point numbers), bash will barf. instead, echo the arithmetic sentence and pipe the output to the bc command, e.g.:
    Code:
    echo 25.4 / 4| bc -l

  3. #3
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Also you'll need to actually capture the input, which is really easy.

    Code:
    echo -e "What would you like to say? \c"; read answer;
    In this code, the '-e' allows backslash commands to be interpreted whereas without the -e option, the \c would just be printed, and the '\c' produces no further output, meaning what the user types will be printed on the same line as the question.
    Code:
    # What it looks like with \c
    What would you like to say? Answer
    Code:
    # What it looks like without \c
    What would you like to say?
    Answer
    The "read" command tells it to wait for user input and assign it to a variable (in this case the variable is 'answer' but it can be whatever you want).

    Try this.
    Code:
    echo -e "What would you like to say? \c"; read answer; echo "$answer";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •