I want to print as follows:
Hello 11 abc
Hello 12 def
Hello 13 ghi
Hello 14 ijk
Hello 15 lmn
i.e; Hello $no $ name
where no's are 11, 12 , 13, 14, 15
names are abc, def, ghi, ijk, lmn
using one Hello $no $name I needthe o/p as above.
I tried as
for i in 11,12,13,14,15
do
no=$i
for j in abc,def,ghi,ijk,lmn
do
name=$j
echo " Hello $no $name\n "
done
done
displays o/p as: Hello 11,12,13,14,15 abc,def,ghi,ijk,lmn
I am doing some where mistake.... :wall:
Someone correct me...
Thanks!
for i in 11 12 13 14 15
---------- Post updated at 11:51 AM ---------- Previous update was at 11:50 AM ----------
Also, just echo "Hello $i $j"
, there's no need to assign new variables there. The \n is also pointless, echo doesn't translate \n on most systems and it adds a newline anyway.
corona688:
for i in 11 12 13 14 15
---------- Post updated at 11:51 AM ---------- Previous update was at 11:50 AM ----------
Also, just echo "Hello $i $j"
, there's no need to assign new variables there. The \n is also pointless, echo doesn't translate \n on most systems and it adds a newline anyway.
Wrongly pasted here.....I used 11 12 13 14 15 in code
It was displaying output 25 times..... just a cartesian product...
Hello 11 abc
Hello 11 def
Hello 11 ghi
Hello 11 ijk
Hello 11 lmn
Hello 12 abc
Hello 12 def
Hello 12 ghi
Hello 12 ijk
Hello 12 lmn
Hello 13 abc
Hello 13 def
Hello 13 ghi
Hello 13 ijk
Hello 13 lmn
Hello 14 abc
Hello 14 def
Hello 14 ghi
Hello 14 ijk
Hello 14 lmn
Hello 15 abc
Hello 15 def
Hello 15 ghi
Hello 15 ijk
Hello 15 lmn
Ahah... Working on it.
---------- Post updated at 12:06 PM ---------- Previous update was at 12:01 PM ----------
Sorry, I got distracted by your earlier code and thought you wanted the cartesian product.
Simple way, though may not be what you wanted:
for i in "11 abc" "12 def" "13 ghi" "14 ijk" "15 lmn"
do
echo "hello $i"
done
Another way:
# Sets $1=11, $2=12, $3=13, $4=14, etc.
set -- 11 12 13 14 15
for i in abc def ghi ijk lmn
do
echo "hello $1 $i"
shift # sets $1=$2, $2=$3, ...
done
---------- Post updated at 12:07 PM ---------- Previous update was at 12:06 PM ----------
I haven't tried solutions with arrays because I don't know what your shell is.
corona688:
Ahah... Working on it.
---------- Post updated at 12:06 PM ---------- Previous update was at 12:01 PM ----------
Sorry, I got distracted by your earlier code and thought you wanted the cartesian product.
Simple way, though may not be what you wanted:
for i in "11 abc" "12 def" "13 ghi" "14 ijk" "15 lmn"
do
echo "hello $i"
done
It will not be useful for me, becoz I want to assign both to two different variables.
Mine is Bourne shell.
Try my second solution then, with "set -- ..."
#! /bin/ksh
set -A array1 11 12 13 14 15
set -A array2 abc def ghi ijk lmn
i=0
while [ $i -ne ${#array1
[*]} ]
do
echo " Hello ${array1} ${array2}"
(( i = i + 1 ))
done
Output is
Hello 11 abc
Hello 12 def
Hello 13 ghi
Hello 14 ijk
Hello 15 lmn
2 Likes
no=( 11 12 13 14 15 )
name=( abc def ghi ijk lmn )
for (( i = 0 ; i < ${#name[@]} ; i++ ))
do
echo "Hello ${no} ${name}"
done
karumudi7:
Thanks, its worked....
I even asked what your shell was and you did not say ksh, just bourne. The ksh solution won't work in an ordinary bourne shell.
Did you ever try my second solution, offered before all these others? I works too, in any shell.
corona688:
I even asked what your shell was and you did not say ksh, just bourne. The ksh solution won't work in an ordinary bourne shell.
Did you ever try my second solution, offered before all these others? I works too, in any shell.
I tried your solution too.... I think I replied to you..... Just now I saw, I gave reply to you for ur 1st solution only.... Sorry for forgetting.
Your solution was worked for me....!!!
Thanks....!!!