How to use expr with echo and $ - bash

#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans= 'expr $input/5'
echo "$ans"
So i want this code to be something like this, a user will enter a number that can divided by 5 (5,10,15,20 etc etc) and then it will echo the answer, i am not sure whats wrong with my code it keeps saying "no such file or directory " for output not sure how to fix that.

I think the spaces were your problem...
#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans=`expr $input / 5`
echo "$ans"

Related

what code function will help to ask for input when i try to execute a code with no previous value in shell script

When I choose Option 3 after opening the file it just terminates.
I was trying to use if else function inside section 3 where it ask for new values if there is none stored so that instead of terminating it will ask for values but cant seem to work it out.
#!/bin/bash
while : #This program demonstrate 4 option below
do
clear
echo "Main Menu"
echo "Select any option of your choice"
echo "[1] Show Todays date/time,Files in current directory,home
directory,user id "
echo "[2] Enter a range "
echo "[3] Highest and lowest of the eight random numbers "
echo "[4] Exit/Stop "
echo "==========="
echo -n "Menu choice [1-4]: "
read -r yourch #Choose option out of 4
case $yourch in
1) echo "Today is";date;
echo "Your home directory is:";home;
echo "Your path is :";PWD;
echo "Current Shell";uname;
echo "Your Student ID $USER ID ";
echo "Press a key...";read -r;;
2) echo "Lower value" #Enter the lower value
read -r s1
echo "Higher value" #Enter the higher value
read -r s2
dif=$((s2-s1))
if [ $dif -ne 100 ]
then
echo "Range should be 100"
else #if the differnce is 100 then programe run otherwise terminates
in=$( ("$s2" - "$s1")) #formula for the range
echo "8 random numbers between $s1 and $s2 are :-"
for i in $(seq 1 8)
do
t=$( ($RANDOM % "$in"))
n=$( ("$t" + "$s1"))
echo "$n" #Here we get the random numbers
done
fi
echo "Press a key..."; read -r;;
3) diff=$((s2 - s1)) #Depicts Highest and lowest numbers of the randoms
RANDOM=$$
min=9999
max=-1
for i in $(seq 8)
do
R=$((((RANDOM%diff))+s1))
if [[ "$R" -gt "$max" ]]
then
max=$R
fi
if [[ "$R" -lt "$min" ]]
then
min=$R
fi
done
echo "Biggest number and smallest numbers are $max and $min" #Prints the highest and lowest numbers
echo "press a key...";read -r;;
4)echo " THANK YOU VERY MUCH $ Good Bye"
exit 0;; #Exit command
*)echo "Opps!!! Please select choice 1,2,3,4";
echo "press a key...";read -r;;
esac
done
I would like for it to ask for new values if there is no previous data stored.
I checked your script, to see the problem. It terminates with a division by zero, because s1 and s2 initially are not set. To resolve this, you can use code like
if [ -z "${s1}" ] ;then
read -p "s1 is empty, please enter a number " s1
fi
if [ -z "${s2}" ] ;then
read -p "s2 is empty, please enter a number " s2
fi
-z "..." is true, if the string is empty. The shell doesn't distinguish data types and because I use the doublequotes it is safe to check for an empty string because if s1 is not set, "$s1" results in an empty string.
Btw. "$s1" is logically equivalent to "${s1}", but it is safer to use the curly braces, because there are no ambiguities this way where the variable ends. For example consider the lines:
year=90
echo "I like the music of the $years"
#
echo "I like the music of the ${year}s"
The first echo outputs "I like the music of the" unless variable "years" was set before, while the second outputs "I like the music of the 90s". Without curly braces this would be a bit more inconvenient. Without curly braces sometimes you might run in such ambiguities, without recognizing it easily.

BASH shell script searching and displaying a pattern

I am trying to create a BASH shell script in which I prompt the user to enter an animal, and return "The $animal has (number I set in case statement) legs"
I am using a case statement for this. My current statement is below:
#!/bin/bash
echo -n "Enter an animal: "
read animal
case $animal in
spider|octopus) echo "The $animal has 8 legs";;
horse|dog) echo "The $animal has 4 legs";;
kangaroo|person) echo "The $animal has 2 legs";;
cat|cow) echo "The $ animal has 4 legs";;
*) echo "The $animal has an unknown number of legs"
esac
For a cat or cow, I need to be able to echo "The (xyz) (cat or cow) has 4 legs" I am thinking of using a grep somewhere but don't know if that is the best option for this. Can anyone help out?
Thanks!
With chepner's suggestion:
#!/bin/bash
echo -n "Enter an animal: "
read -r animal
case $animal in
spider|octopus) n=8;;
horse|dog) n=4;;
kangaroo|person) n=2;;
cat|cow) n=4;;
*) n="an unknown number of"
esac
echo "The $animal has $n legs"
It seems that adding *cat|*cow) instead of cat|cow) worked without any need for grep. Thanks

bash - how to put $RANDOM into value?

newbie to bash:
basically I want to compare the result of $RANDOM to another value which is given by the user through 'read'
code for more info:
echo $RANDOM % 10 + 1 | bc
basically I want an if statement as well to see if the result of that $RANDOM value is equal to something that the user typed in e.g.:
if [ [$RANDOM VALUE] is same as $readinput
#readinput is the thing that was typed before
then
echo "well done you guessed it"
fi
something along the lines of that!!
to summarise
how do i make it so that i can compare a read input value to echo "$RANDOM % 10 + 1 | bc"
think of the program I am making as 'GUESS THE NUMBER!'
all help VERY MUCH APPRECIATED :)
There's no need for bc here -- since you're dealing in integers, native math will do.
printf 'Guess a number: '; read readinput
target=$(( (RANDOM % 10) + 1 )) ## or, less efficiently, target=$(bc <<<"$RANDOM % 10 + 1")
if [ "$readinput" = "$target" ]; then
echo "You correctly guessed $target"
else
echo "Sorry -- you guessed $readinput, but the real value is $target"
fi
The important thing, though, is the test command -- also named [.
test "$readinput" = "$target"
...is exactly the same as...
[ "$readinput" = "$target" ]
...which does the work of comparing two values and exiting with an exit status of 0 (which if will treat as true) should they match, or a nonzero exit status (which if will treat as false) otherwise.
The short answer is to use command substitution to store your randomly generated value, then ask the user for a guess, then compare the two. Here's a very simple example:
#/bin/bash
#Store the random number for comparison later using command substitution IE: $(command) or `command`
random=$(echo "$RANDOM % 10 + 1" | bc)
#Ask the user for their guess and store in variable user_guess
read -r -p "Enter your guess: " user_guess
#Compare the two numbers
if [ "$random" -eq "$user_guess" ]; then
echo "well done you guessed it"
else
echo "sorry, try again"
fi
Perhaps a more robust guessing program would be embedded in a loop so that it would keep asking the user until they got the correct answer. Also you should probably check that the user entered a whole number.
#!/bin/bash
keep_guessing=1
while [ "$keep_guessing" -eq 1 ]; do
#Ask the user for their guess and check that it is a whole number, if not start the loop over.
read -r -p "Enter your guess: " user_guess
[[ ! $user_guess =~ ^[0-9]+$ ]] && { echo "Please enter a number"; continue; }
#Store the random number for comparison later
random=$(echo "$RANDOM % 10 + 1" | bc)
#Compare the two numbers
if [ "$random" -eq "$user_guess" ]; then
echo "well done you guessed it"
keep_guessing=0
else
echo "sorry, try again"
fi
done

Bash shell, Adding a variable number of variables

I am trying to write a short script that accepts a variable number of parameters (also numbers)
then adds those parameters together to get a total of the numbers. Then gets an average for those numbers entered.
This is what i have so far;
#!/bin/bash
count=1
ncount=1
echo
echo "please enter number of parameters: "
read parano
while [ $parano -ge $numbers$count ]
do
echo
echo "Please enter parameter $count: "
read number$ncount
let count=count+1
let ncount=ncount+1
done
Total=$((number$ncounttotal))
Average=$((Total/parano))
echo
echo "You have chosen $parano parameters"
echo
echo "The average is $Average"
echo
Its just the line for calculating the total that I am having issues with and cannot seem to find the code to calculate it. The rest seems to be working great but the average always comes out as 0 because of the total not being calculated.
Anyone have any ideas?
#!/bin/bash
[ $# -eq 0 ] && exit 1
for number in $#; do
sum=$(($sum + $number))
done
average=$(echo "$sum / $#" | bc -l)
echo $average
Then call it like:
./shellscript 1 2 3
I use bc above since bash will only do integer arithmetic and that's not great for the average.
Thank you for the help guys. The answer to what I needed was an array as mentioned.
#!/bin/bash
echo
echo "Please enter number of parameters: "
read parano
count=1
Total=$((0))
while [ $parano -ge $numbers$count ]
do
echo
echo "Please enter parameter $count: "
read number
let count=count+1
Total=$(($Total+number))
done
Average=$((Total/parano))
echo
echo "You have chosen $parano parameters"
echo
echo "The total is $Total "
echo
echo "The average is $Average"
echo
Answer was to make an array as the total and keep adding the variables to the array as they went along. At least that's what I think it is. Either way it works so thanks all!

Allow read to only accept two lengths

I am trying to add to a script I'm writing a restriction to the input where the user can only input 9 or 10 characters I set it up like this but when I run it, it doesn't work the way I want it to. Everything I type in comes back as 10 characters even if I just put one number. What is wrong with my code?
#!/bin/bash
#
echo "Please input numbers only"
read inputline
if read -n10 inputline
then
echo "10 chars"
else
if read -n9 inputline
then
echo "9 chars"
else
echo "invalid input length"
fi
Your script is asking for input three times. I'm assuming that the following is closer to what you intend:
#!/bin/bash
read -p "Please input numbers only " inputline
len=${#inputline}
if (( len == 9 || len == 10 ))
then
echo "$len chars"
else
echo "invalid input length"
fi
The -n option to read limits the input to the specified number of characters but accepts that length without pressing enter. You can enter fewer by pressing Enter though. I've found it to be useful for -n 1 but rarely otherwise.
Use the offset Parameter Expansion to truncate whatever they enter to a max of 10 chars like so:
$ num="123456789012"; echo ${num::10}
1234567890

Resources