# 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 ]]
Related
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
I am trying to write a bash script that will take in a number of lines and print out each line with an additional number added on to it for each new line up until it reaches the number of lines that the user specified.
#!/bin/bash
printf "%0s\n" "Enter the number of lines"
read x
n=0
while [ "$n" -lt $x ];
do
#
y="$n"
while [ 0 -le "$y" ];
do
printf "$y "
let y--
done
echo
let n++
done
Current Output for x=5:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
This is similar to want I want. I want it in the reverse order.
Wanted output for x = 5:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
I have played around with the logic only to get blanks or an infinite loop of printf errors. I am new to this and would appreciate any help.
You can do it in a simpler way:
#!/bin/bash
read -p "Enter the number of lines: " x
for (( n = 1; n <= x; n++ )); do
for (( y = 0; y < n; y++ )); do
echo -n "$y "
done
echo
done
Output for x=5
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
I suppose the script is self-explanatory but if you have any questions, please let me know.
I managed to arrive at a solution using #markp-fuso's comment. It looks like this.
#!/bin/bash
printf "%0s\n" "Enter the number of lines"
read x
n=0
while [ "$n" -lt $x ];
do
#
y=0
while [ $y -le "$n" ];
do
printf "$y "
let y++
done
echo
let n++
done
Output for x = 5
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
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
}
#!/bin/bash
arrNum=(1 2 3 4)
for ((i = 0; i< 4; i++)) do
echo -n ${arrNum[i]}
done
echo
I have an array like this. 1D array. How can I change it to 2D array?
You can do :
#!/bin/bash
arrNum=(1 2 3 4)
for ((j = 0; j< 4; j++)) do
for ((i = 0; i< 4; i++)) do
echo "${arrNum[i]} - ${arrNum[j]}"
done
echo
done
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.