Bash script error, integer expression expected - linux

I'm getting an error saying
"line 6: [: : integer expression expected"
and I can't figure out what to do to fix it. I'm trying to write a script to print out 200 equations, the equations should be of form "i * j = k" where i is an integer between 1 and 10, j is between 1 and 20, k is the product of i and j.
#!/bin/bash
for i in {1..200..1}
do
if [ "$i" -gt 0 ] && [ "$i" -lt 11 ] && [ "$j" -gt 0 ] && [ "$j" -lt 21 ]
then
i = 1
j = 1
k = $(($i * $j))
echo $i * $j = $k
((i++))
((j++))
fi
done

In your script, both i and j get initialized to 1, which means your entire loop echoes 1 * 1 = 1, 200 times. Furthermore, j is not defined the first time your if statement tests $j, hence you got the error message "line 6: [: : integer expression expected".
One way of printing 200 equations, with combinations of i and j, where i is an int between 1 and 10, and j is between 1 and 20, is as follows:
#!/bin/bash
for (( i = 1; i <= 10; i++ )); do
for (( j = 1; j <= 20; j++ )); do
k=$(( i * j )) # Note no space before/after equal sign
echo "$i * $j = $k" # Note the quotation mark
done
done
Or you can do the same thing in a different format as follows:
#!/bin/bash
for i in {1..10}; do
for j in {1..20}; do
k=$(( i * j ))
echo "$i * $j = $k"
done
done
This way, both i and j get initialized before any statements are executed, and you can set the max and min restrictions on both within the loops.

Related

Hi I'm trying to run shell code in script but facing some errors

So after executing I'm getting error ambigeous redirect at this idf case statement this script is in bash I'm trying resolve it but may be I need some help
mapfile lineno < /home/admin/mqconfig_backup/playground/linesno.txt
mapfile queueno < /home/admin/mqconfig_backup/playground/queueno.txt
echo ${lineno[#]}
echo ${queueno[#]}
declare -a P1
lenoflineno=`echo "${#lineno[#]}"`
lenofqueueno=`echo "${#queueno[#]}"`
for((i= 0 ; i <= $lenoflineno ; i++));
do
for((j= 0 ; j <= $lenoflineno ; j++));
do
echo $lineno[i]
echo $queueno[j]
if [ $qeueno[j] < $lineno[i] < $queueno[j + 1] ];
then
P1+=($queueno[i])
fi
done
done
You made a duplicate question.
Supposing the following input files (I took same numbers as your other question):
linesno.txt contains 1 6 8 10 15 20, one number per line
queueno.txt contains 2 5 8 9 11 16 19, one number per line
You could try :
#!/usr/bin/env bash
# Use "-t" to avoid the '\n' in the array values
mapfile -t lineno < linesno.txt
mapfile -t queueno < queueno.txt
declare -a P1
# simply assign ${#array[#]}, do not use echo
lenoflineno=${#lineno[#]}
lenofqueueno=${#queueno[#]}
# from 0 to (size - 1)
for ((i = 0; i < lenoflineno; i++)); do
# from 0 to (size - 2), as we refer to queueno[j + 1] below
for ((j = 0; j < lenofqueueno - 1; j++)); do
# there are two compare operations, we need to write them separately
(( queueno[j] < lineno[i] && lineno[i] < queueno[j + 1] )) &&
P1+=("${lineno[i]}")
done
done
printf "%s\n" "${P1[*]}"
Output:
6 10 15

Bash script to give n numbers and print even numbers

I'm trying to write a bash script for
"Given a positive integer N greater than 1, make a script to show the
even numbers between 0 and N. Ex .: The number 12 has been read. The
program must have as output: 0, 2, 4, 6, 8, 10 and 12;"
So far I did like this:
ex5.sh
#!/bin/bash
echo "Number :"
read num;
for (( i = 1; i >= $num; i++ ))
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done
When i compile, it doesn't print numbers. I couldn't find where is the problem
I tried using for-in loop as well
#!/bin/bash
echo "Number :"
read num;
for i in $num
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done
This time only print the number that i put as a input. For example, I put 4 in terminal it prints 4 as well
Many thanks
This way can make your code working:
echo -n "Number : "
read num;
for i in $(seq 0 $num)
do
if [ $(expr $i % 2) == 0 ]
then
echo $i
fi
done
bash ex5.sh
Number : 12
0
2
4
6
8
10
12
`
echo -n "Number : "
read num;
for i in $(seq 0 $num)
do
if [ $((num%2)) -eq 0 ]
then
echo $i
fi
done
`
#!/bin/bash
read -p "Number: " num
awk '{for(i=0;i<=$1;i++) if(i%2 == 0) print i}'<<<$num
./printNumbers.sh
Number: 8
0
2
4
6
8
# to get a list use printf
awk '{for(i=0;i<=$1;i++) if(i%2 == 0) printf i","}'<<<$num | sed 's/,$//'
./printNumbers.sh
Number: 8
0,2,4,6,8
#!/usr/bin/env bash
read -p 'Number: ' num
seq 0 2 "$num"
it's <= not >=
#!/bin/bash
echo "Number :"
read num;
for (( i = 1; i <= $num; i++ ))
do
if [ $(($i % 2)) -eq 0 ]
then
echo $i
fi
done

Calculate Fibonacci number iteratively using Bash [duplicate]

This question already has answers here:
Bash Script - Fibonacci
(6 answers)
bash shell script fibonacci not showing value 2 after 0 1 1?
(2 answers)
Closed 3 years ago.
Is it possible to write a script that calculates the nth Fibonacci number - iteratively. I did it recursive (showed below) but could find solution iteratively. Please help.
#!/bin/bash
fib()
{
if [ $1 -le 0 ]
then
echo 0
return 0
fi
if [ $1 -le 2 ]
then
echo 1
else
a=$(fib $[$1-1])
b=$(fib $[$1-2])
echo $(($a+$b))
fi
}
I'm not into bash and had no possibility to check but it should be something like:
fib()
{
if [ $1 -le 0 ]
then
echo 0
return 0
fi
if [ $1 -le 2 ]
then
echo 1
else
a = 1
b = 1
for i in {2..$1}
do
c = $b;
b = $a + $b;
a = $c
done
echo $($b)
fi
}
(there might be some errors in spelling)
for java it would work like:
public int fib(n){
if(n <= 1) {
return n;
}
int fib = 1;
int prevFib = 1;
for(int i=2; i<n; i++) {
int temp = fib;
fib+= prevFib;
prevFib = temp;
}
return fib;
}
Here's the iterative approach:
function fib() {
local n=$1
local x=0
local prev1=0
local prev2=0
local cur=0
for (( x = 1 ; x <= n ; x++ ))
do
if [[ $x == 1 || $x == 2 ]] ; then
prev1=1
prev2=1
cur=1
continue
fi
cur=$(( prev1 + prev2 ))
prev2=$prev1
prev1=$cur
done
echo $cur
}

How to create a script in Linux that makes a pyramid of asterisks with the pattern 1, 3, 5, 7, 9, 11, 13, 15, 17 centered

I'm trying to write a shell script to print a pyramid of asterisks like this:
*
***
*****
*******
*********
***********
*************
***************
*****************
Below is my attempt so far. When I run it, I get some errors that say arithmetic expression required. What am I doing wrong?
for (( i = 1; i <= n, i++ )); do
for (( k = i; k <= n, k++ )); do
echo -ne " "
done
for (( j = 1; j <= 2 * i - 1, j++ )); do
echo -ne "*"
done
echo
done
The syntax of an arithmetic for-loop uses two semicolons, not a semicolon and a comma:
for (( i = 1; i <= n; i++ )); do
(The individual components may contain commas — for example, i++, j++ is an expression that increments both i and j — but that has no specific relevance to the for.)
echo "enter value for n"
read n
for (( i = 1; i <= n; i++ ))
do
for (( k = i; k <= n; k++ ))
do
echo -ne " "
done
for (( j = 1; j <= 2 * i - 1; j++ ))
do
echo -ne "*"
done
echo " "
done
this one is right program with corrected syntax

Read value in matrix in shell script

# reading into array
for(( i=0;i<2;i++)) do
for((j=0;j<2;j++)) do
read a[i][j]
done
done
# printing
for(( i=0;i<2;i++)) do
for((j=0;j<2;j++) do
echo -n ${a[i][j]}" "
done
echo
done
When we read the values in matrix
2 3
4 5
it assigns only the values 3 5
and prints
3 3
5 5
You can simulate 2D arrays with index arithmetic in a couple of ways. Here is a common method:
#!/bin/bash
## read values into a 1D array using nested loops and
## simulated 2D addressing
for ((i = 0; i < 2; i++)); do
for ((j = 0; j < 2; j++)); do
read a[$((i * 2 + j))]
done
done
## Output the values contained in a simulated 2D manner
for ((i = 0; i < 2; i++)); do
for ((j = 0; j < 2; j++)); do
printf " %2d" ${a[$((i * 2 + j))]}
done
echo ""
done
Output
$ bash sim2darray.sh
1
2
3
4
1 2
3 4
With a few additional bits of formatting you can make a nice looking simulated 2D array:
#!/bin/bash
for ((i = 0; i < 5; i++)); do
for ((j = 0; j < 5; j++)); do
a[$((i * 5 + j))]=$((i * 5 + j))
done
done
printf "\nThe simulated 5x5 2D array:\n\n"
for ((i = 0; i < 5; i++)); do
[ "$i" -eq 0 ] && printf "[[" || printf " ["
for ((j = 0; j < 5; j++)); do
printf " %3d" ${a[$((i * 5 + j))]}
done
[ "$i" -eq 4 ] && printf " ]]\n\n" || printf " ]\n"
done
Example Use/Output
$ bash sim2darray.sh
The simulated 5x5 2D array:
[[ 0 1 2 3 4 ]
[ 5 6 7 8 9 ]
[ 10 11 12 13 14 ]
[ 15 16 17 18 19 ]
[ 20 21 22 23 24 ]]

Resources