shell scripting EXPR syntax error [duplicate] - shell

This question already has answers here:
Multiplication with expr in shell script
(3 answers)
Closed 5 years ago.
I am getting a syntax error in the following code.
I am trying to print the squares of a number n.
#!/bin/sh
echo "Enter a number n to print squares : "
read num
i=1
while [ $i -le $num ]
do
echo $(expr $i * $i)
i=$(expr $i + 1)
done
echo "Done with Script"
Can someone tell me whats wrong with the code?
Thank you guys in advance.

#!/bin/sh
echo "Enter a number n to print squares : "
read num
i=1
while [ $i -le $num ]
do
echo $(expr $i \* $i)
i=$(expr $i + 1)
done
echo "Done with Script"
Shield * symbol.

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.

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.

While loop in bash script for echoing out hexadecimal numbers [duplicate]

This question already has an answer here:
Shell Script: Hexadecimal Loop
(1 answer)
Closed 6 years ago.
I want a bash script to print list of incremental MAC addresses. This is the while loop I'm using:
i=1
j=1
k=1
while [ $i -le 5 ]; do
j=1
while [ $j -le 46 ]; do
k=1
while [ $k -le 44 ]; do
echo "mac=00:00:01:$i:$j:$k"
k=`expr $k + 1`
done
j=`expr $j + 1`
done
i=`expr $i + 1`
done
I want the MACs to print in hexadecimal. So I want this -
00:00:01:01:09:09
to increment to this -
00:00:01:01:09:0a
and NOT to this -
00:00:01:01:09:10
Look at printf builtin bash command, and change your echo with
printf "mac=00:00:01:%2.2x:%2.2x:%2.2x\n" $i $j $k
i=1
j=1
k=1
# using for loop instead of while loop
for i in $(seq 1 5); do
j=1
for j in $(seq 1 46); do
k=1
for k in $(seq 1 44); do
echo "mac=00:00:01:$i:$j:$k"
#using following command for HEX increment.
k=`echo "obase=ibase=16;${k}+1"`
done
j=`echo "obase=ibase=16;${j}+1"`
done
i=`echo "obase=ibase=16;${i}+1"`
done

how to extract numbers from this echo into separate variables?

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`

shell script convert dec to hex

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

Resources