The answer is quite easy.We run 2 while loops with their syntaxes as used in unix.One increments number from 3 to given range and other loop checks the divisibility of the number with each number from 2 to the value one less than it.
Below is the shell script:
echo enter a range
read rng
echo 2
j=3
while test $j -le $rng
do
i=2
x=`expr $j - 1`
while test $i -le $x
do
if [ `expr $j % $i` -ne 0 ]
then
i=`expr $i + 1`
else
break
fi
done
if [ $i -eq $j ]
then
echo $j
fi
j=`expr $j + 1`
done
Take care of the spaces and syntaxes.....
Happy programming..!!
Above answer run time is very heigh, because loop checks the divisibility of the number with each number from 2 to the value one less than it. Its a bad idea, for finding prime
numbers its enough to check the divisibility of the number with each number from 2 to the value half of the number.
for example to find 17 is a Prime number or not its enough to check the divisibility of the
number with 2 to 8.
Check bellow link for more elegant script.
http://bashscript.blogspot.com/2009/11/shell-script-to-produce-prime-numbers.html
Venu Madhav Reddy
2,3,5,7,9,11,13,17,19,23,29,31,37,39,41,43,47,49
There are several shell programs available for download on the Internet that will generate prime numbers. The best way to find a prime number is through calculation, however.
#!/bin/Bash echo "Enter the two numbers to be Multiplied:" read n1 read n2 answer=`expr $n1 \* $n2` echo $answer
echo enter a range read rng echo 2 j=3 while test $j -le $rng do i=2 x=`expr $j - 1` while test $i -le $x do if [ `expr $j % $i` -ne 0 ] then i=`expr $i + 1` else break fi done if [ $i -eq $j ] then echo $j fi j=`expr $j + 1` done
i=2 rem=1 echo "Enter a number" read num if [ $num -lt 2 ] then echo "$num is not prime" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ] do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ] then echo "$num is prime" else echo "$num is not prime" fi
Yes
shell script for check whether the given no is prime or not??echo "input any number:"read nono=`expr $no`i=`expr 2`while [ $i -lt $no ]doif [ `expr $no % $i` -eq 0];thenecho "$no is not a prime no.."break 2fii=`expr $i +1`doneif [ $i -eq $no ];thenecho "$no is a PRIME no..."fi
Check below link
If the shell script is readable and executable then to execute it just type the name of the shell script file. Otherwise, you can explicity call a shell interpreter to run the file as a shell script, i.e., ksh myfile
2,3,5,7,9,11,13,17,19,23,29,31,37,39,41,43,47,49
No, the shell needs both execute and read permissions to run the script.
There are several shell programs available for download on the Internet that will generate prime numbers. The best way to find a prime number is through calculation, however.
You don't need a shell script for that; use either 'whoami' or 'id'
There are following shell scripts available at the below mentioned url -1. Shell Script for Log4j Log Analysis and exception reporting2. Log Monitoring Shell Script - email upon errorsHope that's what you are looking for.
#!/bin/Bash echo "Enter the two numbers to be Multiplied:" read n1 read n2 answer=`expr $n1 \* $n2` echo $answer
a=10; b=20; c=`expr $a + $b`; printf "$c";
It depends on the script language you are using. In the Korn shell, you can say: echo -n "Enter the first number: " read first echo -n "Enter the second number: " read second let third=$first+$second echo The answer is $third