I was trying to print out some numbers from 1 to 20 with an increment of 2
#!/bin/bash
for i in {0..20..2}
do
echo $i
done
and this is what it has printed out
{0..20..2}
what I am doing wrong?
Using the built-in for loops syntax:
#!/bin/bash
for (( i=0; c<=20; c+=2 ))
do
echo $i
done
Related
Following is my for loop
array=( one two three )
for (( i=0;i<${#array[#]};i++ ))
do
echo -e "\t${array[$i]}"
done
with this script I'm getting the output as
#current output
one
two
three
but my requirement is to increase tabs count based on index of for loop.
#required output
one
two
three
can anyone suggest how to achieve this?
Use an inner loop:
for (( i=0; i < ${#array[#]}; ++i )) ; do
for (( j=0; j<=i; ++j )) ; do
printf '\t'
done
echo "${array[$i]}"
done
or accumulate the tabs in a variable:
for (( i=0; i < ${#array[#]}; ++i )) ; do
t+=$'\t'
echo "$t${array[$i]}"
done
Alternatively, use Perl which has the repetition operator:
perl -E 'say "\t" x ++$i, $_ for #ARGV' "${array[#]}"
try
#!/bin/bash
array=( one two three )
for (( i=0;i<${#array[#]};i++ ))
do
printf %"${i}"s |tr " " "\t"
echo -e "\t${array[$i]}"
done
Demo :
$./test.ksh
one
two
three
$cat test.ksh
#!/bin/bash
array=( one two three )
for (( i=0;i<${#array[#]};i++ ))
do
printf %"${i}"s |tr " " "\t"
echo -e "\t${array[$i]}"
done
$
Explanation :
printf %"${i}"s --> will print space i times.
I have this problem about bash output i need to write code that would output:
1
22
333
using loops so far i have this code:
First loop....
for ((i=1; i<=1; i=i+1))
do
echo $i
done
#Second loop....
for ((i=1; i<=2; i++))
do
for ((j=2; j<=2; j++))
do
echo -n $j
done
done
#Third loop.....
for ((i=1; i<=2; i++))
do
for ((j=2; j<=2; j++))
do
echo -e $j
done
done
Problem is it outputs third loop inside the second like
1
223
33
Thanks for any help.
A simple nested for loop is be sufficient to do what you want:
CODE:
for ((i=1; i<=5; i++))
do
for((j=1; j<=$i; j++))
do
echo -n $i
done
echo
done
OUTPUT:
1
22
333
4444
55555
Just output the newline after the first loop ends:
for ((i=1; i<=3; i++)) ; do
for ((j=1; j<=i; j++)) ; do
echo -n $i
done
echo
done
Note that in Perl, you can hide the inner loop in the repetition operator x:
perl -le 'print $_ x $_ for 1 .. 3'
Try two nested for loops with interactive start and finish values read from STDIN like so:
# To make it more interactive
echo -n 'Enter start value: '
read START
echo -n 'Enter finish value: '
read FINISH
# Two nested for loops
for ((I=$START; I<=FINISH; I++))
do
for ((J=$START; J<=$I; J++))
do
echo -n $I
done
echo
done
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
#!/bin/bash
n=1
while (( $n <= 5 ))
do
num$n=`echo "$n"`
n=$(( n+1 ))
done
echo "$num1"
ok so what I am trying to do is create a while loop that will create variables and just put something into it in this case its just the value of n but i cant get it to do this!
so basically it will create num1, num2, num3 and so on
echo "$num1"
echo "$num2"
echo "$num3"
should display
1
2
3
but i keep getting an error am i missing something here cause it shouldnt be anything crazy to do this...
Try with
#!/bin/bash
n=1
while (( $n <= 5 ))
do
eval num$n=`echo "$n"`
n=$(( n+1 ))
done
echo "$num1"
echo "$num2"
echo "$num3"
The problem here is that bash is trying to evaluate num$n as a command, which does not exist, so the error.
Don't dynamically create numbered variable names like this; use an array.
n=1
while (( $n <= 5 )); do
nums[$n]=$n # No need to execute $(echo ...)
n=$((n+1))
done
echo "${num[1]}"
echo "${num[2]}"
echo "${num[3]}"
I'm new in shell programming ... basically I'm novice at all but I need a simple script to do while loop and execute a php script . I've tried the following :
!/bin/bash
i=0
while[ i < 13 ]
do
php /var/www/html/pos.php &
(( i++ ))
done
but for some reasons the syntax is not good ... I'm getting error line 4: syntax error near unexpected token `do'
You need to have a space between while and the left bracket [, and you need to put the do on a separate line or use a semicolon (both of those are fairly common mistakes when writing loops). Additionally, the left bracket [ is equivalent to man test which supports -lt but not <:
function doStuff() {
local counter=0
while [ $counter -lt 10 ]
do
echo $counter
let counter=$counter+1
done
}
doStuff
OR
function doStuff() {
local counter=0
while [ $counter -lt 10 ] ; do
echo $counter
let counter=$counter+1
done
}
doStuff
!/bin/bash
i=0
while (( i < 13 ))
do
php /var/www/html/pos.php &
(( i++ ))
done
alternatively, you can use a for loop
for((i=1;i<=13;i++))
do
php /var/www/html/pos.php &
done
since the for loop already creates the counter you, you don't have to declare a counter manually.
can't see your code, but it should be like this
while [ $i -ne 3 ]
do
echo "on number $i of 3"
i=`expr $i + 1`
done
I suppose that you want to do something like:
i=0; while (($i<10)); do i=$((i+1)); echo $i; done