I am new to scripting. The script below checks if a number inputted is a prime number or divisible by 2. Why is the third line from the bottom needed (i=expr $i + 1). I commented that line out to see what the script would do and it hangs the script up. Please advise.
#! /bin/bash
echo -n "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "
If you don't increment i, then the test [ $i -lt $num ] will never be false, assuming i starts out less than num.
Related
How can I add a condition that will deal with a negative number input for my Fibonacci series in bash script? This is my code:
clear
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: Program that finds the fibonacci series"
echo "Options:"
echo "-h shows this help information "
echo "-lt the value on the left is less than the value on the right"
echo "expr evaluates a given expression and displays its corresponding output "
exit 1
fi
echo 'This program will find the Fibonnacci Series'
echo "Enter the number of terms you want to be generated"
read n
f1=0
f2=1
i=2
echo "The following is the Fibonacci Series upto $n term:"
echo $f1
echo $f2
while [ $i -lt $n ]
do
i=`expr $i + 1`
f3=`expr $f1 + $f2
echo $f3
f1=$f2
f2=$f3
done
I want to print prime numbers using for loop in shell script. Please provide some suggestions.
actually I could able to print odd numbers from range of 1 to 100, but now I am unable to print prime numbers. Provide some script to print prime numbers
# cat prime.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
output=$(( $i % 2))
if [ $output -ne 0 ]
then
echo "We got odd numbers: $i"
fi
done
The below code returns the list of prime numbers within a specified range.
the below command returns the prime numbers between 10 to 30
./filename.sh 10 30
#!/bin/bash
# Check if a number is prime
# function to return whether prime or not
check_prime() {
current_number=$1
flag=0
i=2
while test $i -le `expr $current_number / 2`
do
if test `expr $current_number % $i` -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then echo $current_number
fi
}
# Assign From and to number
from_number=$1
to_number=$2
for (( number=$from_number; number<=$to_number; number++ ))
do
check_prime $number
done
This code will Print all prime numbers upto given number & also print count of it
hope it helps !!
#!/bin/bash
echo "enter a number upto which you want the prime numbers"
read num
count=0
for (( n=2; n<=$num; n++ ))
do
t=1
if [ $n -lt 2 ]
then echo "Please give other numbers than 0 and 1"
else
#echo $n
for (( i=2; i<$n; i++ ))
do
#echo $n
if (($n%i==0))
then
#echo "$n is not prime number"
t=0
break;
fi
done
#echo $t
if [ $t == 1 ]
then
echo "$n is a prime number"
count=`expr $count + 1`
fi
fi
done
echo "Total prime numbers upto $num are $count "
echo 'Enter no'
read x
n=2
while [ $n -le $x ]
do
i=2
count=1
while [ $i -lt $n ]
do
if [ `expr $n % $i` -eq 0 ]
then
count=0
break
fi
i=`expr $i + 1`
done
if [ $count -eq 1 ]
then
echo "$n is Prime"
fi
n=`expr $n + 1`
done
It is not really a counter, just a flag.
If any modulus operation returned a 0, then the value is not prime. The flag is set so that after the inner loop completes, you can see that the value was not prime.
If the value never was divisible, then the counter flag was never reset to 0, so you know it is prime.
I have this concern, when getting the factors of a number. My code gives me the total numbers of all factors of a number.What I want is it should only display the factors of that number, not the total. For example I input 9 the output should be 1,3 and 9.
My code
echo -n " enter a no. "
read n
i=1
mul=1
until [ $i -gt $n ]
do
mul=`expr $mul \* $i `
i=`expr $i + 1 `
done
echo " factorial of $n is $mul "
Assuming that the user enters the number in its simplest form, e.g. 9 instead of 09.0, this works:
echo -n " enter a no. "
read n
for i in $(seq 1 $n)
do
[ $(expr $n / $i \* $i) == $n ] && echo $i
done
though a bit slowly for large numbers.
check even number - OK
if [ $(( $n % 2 )) -eq 0 ]
then
echo "$n is even number"
fi
how to check odd number ?
if [ $(( $n % ????? )) -eq 0 ]
then
echo "$n is odd number"
fi
Thank
Use "not equal 0":
if [ $(( $n % 2)) -ne 0 ]
then
echo "$n is odd"
fi
See also: http://tldp.org/LDP/abs/html/comparison-ops.html
You can also use "n%2 equals 1" since the remainder of an odd number divided by two is one:
if [ $(( $1 % 2)) -eq 1 ]
then
echo "$1 is odd"
fi
But the former (not equal 0) is the more general case, so I would prefer it.
All the answers above use a single square bracket [ which is outdated in bash (we're talking about bash, right?). The best practice to achieve the determination of an odd or even number n is:
if (( n%2==0 )); then
printf "%d is even\n" $n
else
printf "%d is odd\n" $n
fi
or, as the OP wants it, i.e., check if n is odd:
if (( n%2 )); then
printf "%d is odd\n" $n
fi
echo -n "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]then
echo "$n is even number"
else
echo "$n is odd number"
fi
I prefer the simplicity of:
x=8; ((x%2)) || echo even
x=7; ((x%2)) && echo odd