#!/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/
Related
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.
My script is to ask for input 1 upper case at a time and end with 0 invalid input will need to be displayed and display the first valid upper letter.
#! /bin/sh
count=0
until [[ $n =~ 0 ]]; do
echo Inputs:
read n
if [[ $n =~ ^[A-Z]$ ]]; then
count=`expr $count + 1`
echo $n | sort > out.txt
fi
done
echo The total number of valid input letters:
echo $count
echo " "
echo The first valid input:
head -n 1 /filepath/out.txt
Output:
Inputs:
B
Inputs:
A
Inputs:
C
Inputs:
0
The total number of valid input letters:
3
The first valid input:
C
Question: It should result in A.
Any help will be appreciated.
This line:
echo $n | sort > out.txt
always zaps the file out.txt with just the latest input. Maybe you should use:
cp /dev/null out.txt # Before the loop
echo $n | sort -o out.txt out.txt -
The cp command creates an empty file. The sort command reads the existing file out.txt and its standard input (the new line), sorts the result and writes it out over out.txt.
This is OK for short inputs; it isn't very efficient if it needs to scale to thousands of lines.
Also, in Bash, you don't need to use expr for arithmetic:
((count++))
Use the following code.
#! /bin/sh
count=0
>out.txt
until [[ $n =~ 0 ]]; do
read -p 'Inputs: ' n
if [[ $n =~ ^[A-Z]$ ]]; then
count=`expr $count + 1`
echo $n >> out.txt
fi
done
echo The total number of valid input letters:
echo $count
echo " "
echo The first valid input:
sort out.txt |head -n 1
Output:
Inputs: B
Inputs: A
Inputs: C
Inputs: 0
The total number of valid input letters:
3
The first valid input:
A
Since you want only the smallest (in alphabet order) valid input, you don't need sort. Here's an alternative answer not using sort but just keep the smallest valid input:
#!/bin/sh
count=0
until [[ $n =~ 0 ]]; do
echo Inputs:
read n
if [[ $n =~ ^[A-Z]$ ]]; then
((count++))
if [ -z $first ] || [ `expr $n \< $first` -eq 1 ]; then
first=$n
fi
fi
done
echo The total number of valid input letters:
echo $count
echo " "
echo The first valid input:
echo $first
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
Sorry about bits and snippit of information
So I am writing an average shell script program
so if use inputs
echo 1 3, .... | sh get_number
I would have to pull the numbers seperated by spaces from echo to be
var1 = 1, var2= 3, etc.
I tried
#!/bin/sh
sum=0
for i in $*
do
sum=`expr $sum + $i`
done
avg=`expr $sum / $n`
echo Average=$avg
but doesnt work....
do I include a read here?
also how would I do
sh get_number <file1>, <file2>... to grab numbers in them and sum them
in shell script?
Thanks
Sounds like you are looking for the read shell builtin:
% echo "1 2 3 4" | read a b stuff
% echo $b
2
% echo $stuff
3 4
To fix up your code:
for i in $*; do
sum=$(( sum + i ))
n=$(( n + 1 ))
done
echo "Average=$(( sum / n ))"
#!/bin/sh
while [ $# -gt 0 ]; do
(( i++ ))
(( sum += $1 ))
shift
done
echo "Average=$(( sum/i ))"
Note: This fails in dash which is the closest shell I could find to a real sh.
An example of reading values from files passed as command line arguments or from lines read from stdin:
add_to_sum() {
set $*
while [ $# -gt 0 ]; do
I=`expr $I + 1`
SUM=`expr $SUM + $1`
shift
done
}
I=0
SUM=0
if [ $# -gt 0 ]; then
# process any arguments on the command line
while [ $# -gt 0 ]; do
FILE=$1
shift
while read LINE; do
add_to_sum "$LINE"
done < "$FILE"
done
else
# if no arguments on the command line, read from stdin
while read LINE; do
add_to_sum "$LINE"
done
fi
# be sure not to divide by zero
[ $I -gt 0 ] && echo Average=`expr $SUM / $I`
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