shell script convert dec to hex - shell

Guys am writing a shell script .Here i want to convert a decimal digit to 2 digit hexadecimal value for example if i give 9 it must giv as 09 .
i have done
for i in {0..255}; do
hexa=$(echo "obase=16;$i" | bc)
done
but for the first 10 vlaues it returns a single digit hexa value
now how am supposed to convert this
Thanks in advance!!!!

You could use printf:
$ printf "%02x\n" 12
0c
See man bash or the manual of your shell of choice.

echo "enter any number"
read num
i=2
while [ $i -lt $num ]
do
p=`expr $num % $ i`
if [ $p -eq 0 ]
then
echo "$num is not prime number"
echo "since $num is divisible of $i"
exit
fi
i=`expr $i + 1`
echo "$num is prime number"
done

for i in {0..255}; do
printf "%X\n" $i
done

Related

Shell Script - Fibonacci

the code below cannot make the fibonacci sequence more than 93 sequences, how can i solve this? I would like you to do with any number
#!/bin/bash
clear
echo "Program to Find Fibonacci Series"
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
You can use the "bc" command (an interactive algebraic language with arbitrary precision) to get past numeric limits of the shell. Here is the re-write of your while loop:
while [[ $i -lt $n ]]
do
i=$(( $i + 1 ))
z=$( bc <<< "$x + $y" )
echo "$z"
x=$y
y=$z
done
On Debian/Ubuntu/RHEL/CentOS systems, install the optional "bc" package.

how to print prime numbers using for loop in shell script

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 "

number palindrome in shell scripting

#!/bin/sh
echo “Enter number:”
read n
num=$n
rev=0
while [ $num –ne 0 ]
do
rem= `expr $num % 10`
rev= `expr $rev \* 10 + $rem`
num= `expr $num / 10`
done
if [ $rev –eq $n ]
then
echo $n “is a palindrome”
else
echo $n “is not a palindrome”
fi
I am getting errors as::
unary operator expected
Please help me in fixing this.
What an unexpected problem! Your dash - character is not the right dash.
The hex representation of your dash – is 0xe28093
Unicode Character 'EN DASH' (U+2013).
But the right dash's hex representation is ASCII 0x2d.
echo -n '–' | xxd # 00000000: e280 93
echo -n '-' | xxd # 00000000: 2d
So you should change it to the correct dash.
#!/bin/bash
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
Source : https://bash.cyberciti.biz/academic/palindrome-shell-script/

Prime Number Script Checker

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.

getting this error "integer expression expected : line 10 and 23" in this bash shell

I'm trying to check a number is palindromic or not. I used a function called pal(). I'm getting this error that says:
./pal.sh: line 10: [: input: integer expression expected
./pal.sh: line 23: [: input: integer expression expected
My code:
#!/bin/bash
pal()
{
num=$1
rnum=$num
add=0
k=0
while [ $num -ne 0 ]
do
lev=1
mod= $num % 10
for((i=0;i<$k;i++))
do
lev=`expr $lev \* 10`
done
mul=`expr $mod \* $lev`
add=`expr $add + $mul`
num=`expr $num / 10`
k=`expr $k + 1`
done
if [ $rnum -eq $add ]
then
echo "pallindrome"
else
echo "not pallindrome"
fi
}
echo "input number"
read input
pal input
You need to pass the value of the variable, not its name:
pal $input
Also, as anubhava points out in a comment, you will need to fix the arithmetic in the function:
mod= $num % 10
should be:
mod=$(($num % 10))
or:
((mod = $num % 10))
You should also generally avoid using expr in bash — there are built-in facilities to handle pretty much anything expr can handle.
You can do this with the rev from "util-linux" like so,
#!/bin/bash
pal()
{
input=$1
if [ $input == $(echo $input | rev) ]; then
echo "pallindrome"
else
echo "not pallindrome"
fi
}
echo "input number"
read input
pal $input
Output
$ ./pal.sh
input number
101
pallindrome
$ ./pal.sh
input number
102
not pallindrome

Resources