#!/bin/bash # Shell script to read a number and find whether the number is odd or even # ------------------------------------------------------------------------- # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- 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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
- RSS feed or Weekly email newsletter
- 15 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I don’t understand the part
– eq0
In finding odd and even number
-eq is a symbolof equal and 0 represent rem is zero
how to check the entered number is grater than 10
I want to find even and odd number (1 to n) using loop
echo “enter the number”
read n
2num=$(( $n % 2 ))
if [ $num -eq 0 ]
then
echo “$n number is even”
else
echo “$n number is odd”
fi
read n
n2=`expr $n % 2`
if [ $n2 -eq 0 ];
then
echo “even”
else
echo “odd”
fi
echo “Program to check even or odd number”
echo “Enter a number”
read n
a=`expr $n % 2`
if [ $a -eq 0 ] ; then #Semicolon is most important for Executing if-else
echo “It is an even number”
else
echo “It is an odd number”
fi
if[ $($n % 2) -eq 0]
then
echo “even”
else
echo “odd”
nevermind
odd() { let ${1: -1}%2 && return 0; return 1; }
is_even() {
case $(( 10#$1 % 2 )) in
0) return 0 ;;
*) return 1 ;;
esac
}
correct it as:
if [ $rem -eq ‘0’ ]
@Aakash Alfred: No buddy, that doesn’t work. It displays everything as odd……..
# On the third line: Instead of rem=$(( $n % 2 ))
# You can use ;
rem= `expr $n % 2`
$(( $n % 2 ))
this syntax not work properly in shell script
error in brackets