LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2020, 07:43 AM   #1
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Rep: Reputation: Disabled
nested for loop


Hello everyone,

I have a little issue with a nested for loop, and I don't understand what is not working in.

I try to output the following files:

title_one_1.txt
title_one_2.txt
title_one_3.txt
title_two_1.txt
title_two_2.txt
title_two_3.txt
title_three_1.txt
title_three_2.txt
title_three_3.txt

Therefore, I set up a nested for loop to output this list.

array="one two three"
for f in $array
do
for p in {1..3} ;
do
echo title_$f_$p.txt
done ;
done

But it is not working. It seems that variables as $f are not processed with underscores, but with dots.
Would anyone know how to output the list above?
 
Old 03-05-2020, 07:52 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,378
Blog Entries: 3

Rep: Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772
Which shell interpreter have you chosen? If it's the POSIX shell, then you'll need another way of doing the count from 1 to 3. Here is one of several:

Code:
#!/bin/sh

array="one two three"
for f in $array
do
        for p in $(seq 1 3)
        do
                echo title_${f}_${p}.txt
        done
done

exit 0
Also, the shell variables allow underscores in the name so you will need curly braces to limit the name to just the letters you mean. In otherwords, you want ${f} and ${p} rather than ${f_p} as you currently had it.
 
Old 03-05-2020, 08:04 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,486

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
If you are using bash and trying to do anything like this always enclose your variable name with {} so that bash knows what variable you are talking about.

If you use ${f} instead of $f you'll find it should work as you expect.

Without {} bash is actually looking for a variable $f_ in your example rather than just $f

So:
Code:
array="one two three"
for f in $array ; do
  for p in {1..3} ; do
    echo title_${f}_${p}.txt
  done
done
 
Old 03-05-2020, 09:00 AM   #4
Drosera_capensis
Member
 
Registered: Jun 2018
Posts: 153

Original Poster
Rep: Reputation: Disabled
Thank you Turbocapitalist, I am working on BASH shell, but I keep preciously your answer.

Thanks a lot for your command and for the nice explanations TenTenths! I will make some experiments with $f and ${f} in my commands.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
Nested loop with 2 counters for 2 array variables briana.paige Linux - Newbie 2 06-19-2009 07:38 AM
bash scripting problem with nested if statements in while loop error xskycamefalling Programming 4 05-11-2009 03:14 PM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 10:28 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration