Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Interactive bash script

0 votes
asked Apr 24, 2020 by fred (120 points)
  1. Asks for your last name
  2. Determine the number of characters in your name
  3. Display “Your <lastname> has <number> of characters”
  4. Ask your age
  5. Identify the first digit (ie 2 for 25 or 1 for 18), if less than 10 use 0, if greater than 99 use 10
  6. Mulitply the first digit by 5
  7. If your are older than 25 then add 3 to first digit*5, if you are younger than 25 add 6 to first digit*5
  8. Double your answer
  9. Add the second digit of your age
  10. If you are are older than 25 then subtract 6, if you are younger than 25 then subtract 12
  11. Echo “Your age is <value>”

###################################################################################

THIS IS WHAT I WROTE BUT NOT WORKING. HELP NEEDED

####################################################################################

#!/bin/bash

clear

echo -n "Enter last name: "

read lname

echo $lname

echo ""

echo "your wc -c $lname has wc -c $lname characters."

echo ""

echo -n "Enter age: "

read age

echo $age

first="$age"; echo $(s:0:1)

second="$age"; echo $(s:1:1)

if [ $age -le 0 ]; then

echo "0"

else

if [$age -gt 99 ]; then

echo "10"

fi

fi

echo ""

if [ $age -gt 25 ]; then

y=($first+3)*5

else

if [$age -le 25 ]; then

y=($first+6)*5

fi

fi

echo ""

y=y*2

echo ""

declare -i second

age=$age+second

if [ $age -gt 25 ]; then

age=$age-6

else

if [ $age -le 25 ]; then

age=$age-12

echo "Your age is: $age"

fi

fi

1 Answer

0 votes
answered Jul 12, 2020 by xDELLx (10,500 points)
edited Jul 13, 2020 by xDELLx

#!/bin/bash
clear
#    Asks for your last name
echo -n "Enter last name: "
read lname
echo $lname
echo ""
#Display “Your <lastname> has <number> of characters”
echo "your $lname has `echo $lname | wc -c ` characters." #execute command between ` ` & then substitue its output in the echo string
echo ""
#Ask your age
echo -n "Enter age: "
read age
echo $age
first=${age:0:1}
second=${age:1:1}
if [ $age -le 10 ]; then
    echo "0"
    #age=0
else
    if [ $age -gt 99 ]; then
        echo "10"
    #    age=10
    fi
fi
echo ""
#Mulitply the first digit by 5
first=`echo $first*5 | bc`  
#If your are older than 25 then add 3 to first digit*5, if you are younger than 25 add 6 to first digit*5
if [ $age -gt 25 ]; then
    #y=($first+3)*5
    first=`echo "$first + 3 " | bc`

else
    #if you are younger than 25 add 6 to first digit*5
    #if [ $age -le 25 ]; then # this if is redundant as the comparison was done above
    #    y=($first+6)*5
        first=`echo "$first + 6" | bc`
    #fi
fi
echo ""
#y=y*2
#Double your answer
first=`echo "$first * 2" | bc `
echo ""
#declare -i second
#second=11

#Add the second digit of your age
age=`echo "$first + $second" | bc `

#If you are are older than 25 then subtract 6, if you are younger than 25 then subtract 12
if [ $age -gt 25 ]; then
    age=`echo "$age-6 "| bc`
else
    #if [ $age -le 25 ]; then  # this if is redundant as the comparison was done above
        age=`echo " $age-12" | bc `
    #fi
fi
echo "Your age is: $age"

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...