🔒 Closed Basic linux scripts

Status
Not open for further replies.

Pusoy000

Forum Veteran
Guys pwedi patulong paganahing tong scripts po , basic palag eto kaso baguhan palang ako sana may tumulong nasa baba po yung link gusto korin matoto
I need help please

You do not have permission to view the full content of this post. Log in or register now.
 
Assignment ba ito? Anyway, here it goes:

Yung contents ng quiz4:
Bash:
     1  #! /bin/bash
     2  clear
     3  read n
     4  if test $n = 3
     5  then
     6          case $2 in
     7           +) let z=$1+$3;;
     8           -) let z=$1-$3;;
     9           /) let z=$1/$3;;
    10           x|X) let z=$1*$3;;
    11           *) echo "Warning - $2 invalid operator, only +,-,x,/ operator allowed"
    12              exit;;
    13          esac
    14          echo Answer is $z
    15  else
    16          echo "Usage - $0   value1  operator value2"
    17          echo "        Where, value1 and value2 are numeric values"
    18          echo "               operator can be +,-,/,x (For Multiplication)"
    19  fi

First and foremost, remove the 'clear' command at line 2. It will make worst nightmare when debugging.

Issues I've noticed:
  • By the looks of it, and as per line 6 ($2) your script parses the commandline argument 1 for arithmetic operations (+,-,*,/). Pero ano ang value ng argument 1 ($1)??? Any? Bash won't know what's in $2 if there is no $1 to begin with.
  • Arithmetic operators are special characters which are highly discouraged to be used as argument parameters.
  • 'read' command at line 3 in unintuitive. It does not clearly shows what does it want.
  • when asking for inputs, I would suggest use 'read' rather than argument(s).
Suggestions:
- Use menu-based selection of operators instead.
-In Bash, when performing conditional testing, use '[[' and ']]'. Or the builtin 'test' command.
  • when using 'read', try to inform the user about its intention.
  • Read more on Bash.
Eto yung modified menu-based version.
Bash:
     1  #!/bin/bash
     2
     3  NUM1=0
     4  NUM2=0
     5
     6  echo -e "\nProgram that performs arithmetic operations based on two numbers given by the user\n"
     7
     8  function get_num {
     9      read -p "Enter number 1: " NUM1
    10      read -p "Enter number 2: " NUM2
    11  }
    12
    13  echo "ARITHMETIC OPERATION"
    14  PS3="Please select an operator: "
    15  select operator in '+' '-' '*' '/'
    16  do
    17      case $operator in
    18          '+')
    19              echo "[$operator] Performing ADDITION..."
    20              get_num;
    21              echo "The sum of ($NUM1 $operator $NUM2) = $((NUM1+NUM2))"
    22              break;;
    23
    24          '-')
    25              echo "[$operator] Performing SUBTRACTION..."
    26              get_num;
    27              echo "The difference of ($NUM1 $operator $NUM2) = $((NUM1-NUM2))"
    28              break;;
    29          '*')
    30              echo "[$operator] Performing MULTIPLICATION..."
    31              get_num;
    32              echo "The product of ($NUM1 $operator $NUM2) = $((NUM1*NUM2))"
    33              break;;
    34          '/')
    35              echo "[$operator] Performing DIVISION..."
    36              get_num;
    37              QUOTIENT=$(echo "scale=4; $NUM1/$NUM2" | bc -l)
    38              echo "The quotient of ($NUM1 $operator $NUM2) =  $QUOTIENT"
    39              break;;
    40          *)
    41              echo "Invalid operator. Please choose again"
    42      esac
    43  done

NOTE: All operators except DIVISION spit only whole number. Because division is a special case. kelangan ipakita yung decimal places kaya I have to use 'bc'.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 477
    Views
  • 2
    Participants
Last reply from:
homer_simpson

Trending Topics

Online now

Members online
1,006
Guests online
1,176
Total visitors
2,182

Forum statistics

Threads
2,275,655
Posts
28,964,621
Members
1,231,897
Latest member
buriko2026
Back
Top