LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-03-2011, 05:49 PM   #1
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Rep: Reputation: 0
Generating two bash arguments from 'ls'


Dear Forum
Say I have a directory containing the following files.
Code:
1-res-opt-I189N-0001.pdb
1-res-opt-I189N-0002.pdb
1-res-opt-I189N-0003.pdb
1-res-opt-I189N-0004.pdb
1-res-opt-I189N-0005.pdb
1-res-opt-I189N-0006.pdb
1-res-opt-I189N-0007.pdb
1-res-opt-I189N-0008.pdb
1-res-opt-I189N-0009.pdb
1-res-opt-I189N-0010.pdb
3-res-opt-I189N-0001.pdb
3-res-opt-I189N-0002.pdb
3-res-opt-I189N-0003.pdb
3-res-opt-I189N-0004.pdb
3-res-opt-I189N-0005.pdb
3-res-opt-I189N-0006.pdb
3-res-opt-I189N-0007.pdb
3-res-opt-I189N-0008.pdb
3-res-opt-I189N-0009.pdb
3-res-opt-I189N-0010.pdb
What I want is something like:
Code:
for i in *.pdb
do
python my_script.py 1-res-opt-I189N-00{1..10}.pdb 3-res-opt-I189N-00{1..10}.pdb
done
such that always the two files with corresponding index are submitted together to the Python script.
How do I do that?
Thanks for any hints.
 
Old 07-03-2011, 05:56 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you have bash 4 you can use extended brace expansion with zero-padded numbers:
Code:
for i in {0001..10}
do
  python my_script.py 1-res-opt-I189N-$i.pdb 3-res-opt-I189N-$i.pdb
done
Otherwise use printf and command substitution:
Code:
for i in {1..10}
do
  python my_script.py 1-res-opt-I189N-$(printf "%04d" $i).pdb 3-res-opt-I189N-$(printf "%04d" $i).pdb
done
In any case the iteration variable of the loop should be a number, not the whole filename. Hope this helps.
 
Old 07-03-2011, 06:19 PM   #3
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
more than helpful. Thanks.
 
Old 07-04-2011, 01:46 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
For the record, this is what your original code does...
Code:
for i in *.pdb
do
python my_script.py 1-res-opt-I189N-00{1..10}.pdb 3-res-opt-I189N-00{1..10}.pdb
done
"for i in *.pdb" first sets the variable "i" to each individual filename in the directory in turn. But then it's never used anywhere.

Inside the loop you have a line with brace expansions inserted. These get expanded before execution, so you get a single command that looks like this:
Code:
python my_script.py 1-res-opt-I189N-001.pdb 1-res-opt-I189N-002.pdb 1-res-opt-I189N-003.pdb 1-res-opt-I189N-004.pdb 1-res-opt-I189N-005.pdb 1-res-opt-I189N-006.pdb 1-res-opt-I189N-007.pdb 1-res-opt-I189N-008.pdb 1-res-opt-I189N-009.pdb 1-res-opt-I189N-0010.pdb 3-res-opt-I189N-001.pdb 3-res-opt-I189N-002.pdb 3-res-opt-I189N-003.pdb 3-res-opt-I189N-004.pdb 3-res-opt-I189N-005.pdb 3-res-opt-I189N-006.pdb 3-res-opt-I189N-007.pdb 3-res-opt-I189N-008.pdb 3-res-opt-I189N-009.pdb 3-res-opt-I189N-0010.pdb
...which you then execute over and over again, once for each file in the directory.


As colucix demonstrated, generate the list of files you want to use (or in this case a sequence of numbers that can be used inside the loop to match filenames) first, then feed that to the for loop and use the variable set by it to access them one at a time.


By the way, for zero padding in bash 3, you could also simply do something like this:
Code:
for i in 000{1..9} 0010 ; do
See here for more on how to do zero padding:
http://mywiki.wooledge.org/BashFAQ/018

Last edited by David the H.; 07-04-2011 at 01:49 AM. Reason: minor rewording for clarity
 
1 members found this post helpful.
Old 07-06-2011, 10:24 AM   #5
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
@david the h.:
thanks a lot. very informative post.
kind regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Generating a random LETTERS sequence iniuria Programming 3 05-12-2011 01:49 PM
[bash] Generating a table with aligned fields Aveltium Programming 2 04-09-2009 05:58 PM
bash: generating a string containing options and using with kdialog arizonagroovejet Programming 1 06-12-2008 03:12 PM
Generating iteratable arrays in bash d00bid00b Programming 4 08-15-2005 05:54 PM
bash scripting: problem with generating filenames mad_juno Programming 2 07-22-2005 11:57 AM


All times are GMT -5. The time now is 10:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration