generating new line for each loop bash - bash

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

Related

Bash for loop printing the statement

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

How to display only once echo command in a Bash loop

My problem is pretty simple. I have :
a=$(echo "lol")
for i in {1..3};
do
echo $a && echo $i ;
done
I get :
lol
1
lol
2
lol
3
I would like to print only once the variable a at the beginning of the output , to get :
lol
1
2
3
Any idea?
You don't need a loop at all
a=$(echo "lol") # Not sure why poster wrote this rater than a=lol
printf %s\\n "$a" {1..3}
I suggest:
#!/bin/bash
a="lol"$'\n' # append newline
for i in {1..3}
do
echo -e "$a$i" # -e: enable interpretation of escape sequences
unset a
done
Or replace in your question
echo $a && echo $i ;
with
[[ "$i" == "1" ]] && echo "$a"
echo "$i"
See: help echo and help unset
Move the echo outside of the for loop
a=$(echo "lol")
echo $a
for i in {1..3}; do
echo $i;
done
or:
echo "lol"
for i in {1..3}; do
echo $i;
done
test run in shell

cannot create triangle in shell script with for loop

I am quite new in shell script and trying to make some practice. What I want to do is to get an input from the user and create a triangle by using for loop. For example; if the user types 4 as input then the targeted triangle will be;
1
2
2
3
3
3
4
4
4
4
Here is my code:
num=4 ##Assume num is given by user##
for ((i=1; i<=$num; i++))
do
for ((j=1; j<=i; j++))
do
echo $i
done
echo " "
done
Output:
1
22
333
4444
Is it something related with 'new line' thing? How can I fix it?
By the way, I am using an online shell terminal.
Thank you very much.
echo $i prints the value behind the i variable and a newline. You want to print a newline after all the numbers. Use -n switch.
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
echo -n "$i "
done
echo
echo
done
Or printf:
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
printf "%s " "$i"
done
printf "\n\n"
done
Remember to qoute your variables (echo "$i" not echo $i)
No need to use $num inside (( .. )). All names are expanded automagically.
#edit
On revisiting the question I came up with the following oneliner with the same functionality:
num=4
seq 1 "$num" | xargs -n1 seq -s ' ' 1
seq 1 "$num" generates numbers 1 2 .... $num separated by newlines
Then for each number xargs runs seq -s ' ' 1 <number> generating 1 .. number for each number separated by spaces
You can insert double newlines with | sed 's/$/\n/' if needed.

Shell script to extract line by line data and provide in variable to use further

File 1 ->
hostname1
hostname2
hostname3
hostname4
.
.
.
.
I want to write a bash script to extract these hostnames and save them in a variable.
Something like below but this does not work
count=3
i=1
j=1
count=`expr $count + 1`
while [ $i -lt $count ]
do
echo The counter is $i
$j=`sed -n "$i,$i p;$i q" file1.txt`
echo $i
i=`expr $i + 1`
j=`expr $j + 1`
echo $j << this should return hostname1 then hostname2
done
Try this :
i=1
while read line ;
do
Var$i=$line
i= ` expr $i + 1 `
done < inputFile
This should create var1, var2 ... and assigns each line to a variable
Else an array can also be used
i = 0
while read line ;
do
arr[$i] = $line
i = `expr $i + 1`
done < inputFile
The expr syntax is not picking up the back ticks on my answer, please check when you use it
i=1
while read line ;
do
i=$line
echo $i
i=i+1
done < file.txt
Above code has resolved my issue . Thanks

Inputs within double `while` loop are not happening

while read line
do
echo "$line"
i=0;
rm -rf b.txt
while [[ $i -lt $line ]]
do
i=`expr $i + 1`
echo "$i " >> b.txt
done
a=`cat b.txt`
for i in $a;
do
echo "Hari $i \c"
read input
done
done < 5.txt
Say 5.txt has the value:
2
3
This script needs to place the cursor inside the for loop, but the script is continuously executing and ending. Can you please help me over this?
Assuming 5.txt consists of:
2
3
The script outputs:
2
Hari 1 \c
Hari 2 \c
...and quits.
The script won't prompt for input after outputting Hari 1 \c because all the input is coming from 5.txt. On the first pass $input would be set to 3, (the 2nd line of 5.txt). On the second pass, the input would be an EOF, in which case read gives up, much like how this outputs nothing:
read -p "Enter a number" n < /dev/null
This would work:
for i in `cat 5.txt` ; do \
echo $i ; for f in `seq $i` ; do read -p "Hari $f \c: " input ; done ; \
done
Note also that $input is never used for anything.

Resources