LinuxQuestions.org
Visit Jeremy's Blog.
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 04-17-2016, 01:49 PM   #1
beca123456
LQ Newbie
 
Registered: Apr 2016
Posts: 9

Rep: Reputation: Disabled
bash for loop: passing 2 arguments to a prog


Hi,

in '$HOME/Data/' directory I have a list a input files:
Code:
file1_X.tab
file1_Y.tab
file2_X.tab
file2_Y.tab
The program I am using needs two inputs from the same file number (below an example for 'file1'):
Code:
my_program \
     -I:X file1_X.tab \
     -I:Y file1_Y.tab \
     -O file1_output.tab
I am trying to use a bash 'for loop' to pass a group of file# to the program but I am kind of stuck.
I started something like the following but the problem is to pass 2 files by calling my_program once:
Code:
for A in *.tab
do

B=`basename ${A} _*.tab`

my_program \
     -I:X ${B}_X.tab
     -I:Y ${B}_Y.tab
     -O ${B}_output.tab

done

Last edited by beca123456; 04-17-2016 at 01:50 PM.
 
Old 04-17-2016, 09:26 PM   #2
mralk3
Member
 
Registered: May 2015
Posts: 760

Rep: Reputation: 232Reputation: 232Reputation: 232
I think the simplest form is to make an array of the files in their directory using the ls command.

Code:
[~]$ a=`ls /tmp`; echo $a
Just change the path to the directory.

EDIT: You would need to add a few more lines to grab the specific files you need each run. This is just meant to get you started.

Last edited by mralk3; 04-17-2016 at 09:27 PM.
 
Old 04-17-2016, 11:54 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Whilst mralk3 suggestion to create an array is a good idea, the command supplied does not do this. To be an array the command substitution needs to be inside round brackets, like so:
Code:
a=($(ls /tmp))
I am also not a fan of using ls, so would probably change the above to a glob:
Code:
a=( *.tab )
The negatives for both of these is that they will also include directories if there are any so you would need to test for them.

The benefit of the glob over the command substitution is that the glob will not split on white space.

Your loop would also then change to the more traditional for loop:
Code:
for (( i = 0; i < ${#a[*]}; i+=2 ))
 
Old 04-18-2016, 11:19 AM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Posts: 193

Rep: Reputation: 73
The traditional way saves some memory and maybe some CPU cycles
Code:
cnt=0; for file in *.tab
do
  if [ $((cnt+=1)) -eq 2 ]
  then
    echo "$file and $pfile"
    cnt=0
  else
    pfile=$file
  fi
done
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing input arguments from a file in a loop mstfkpln Linux - Newbie 7 03-11-2016 01:22 AM
Bash Script passing arguments dimitriauxio Linux - Newbie 8 09-16-2013 10:19 AM
Bash - passing associative arrays as arguments Lucien Lachance Programming 12 08-26-2013 01:43 PM
[SOLVED] BASH - passing arguments Garrett85 Programming 4 01-09-2013 04:03 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM


All times are GMT -5. The time now is 10:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration