LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-17-2016, 12:49 PM   #1
beca123456
LQ Newbie
 
Registered: Apr 2016
Posts: 10

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 12:50 PM.
 
Old 04-17-2016, 08:26 PM   #2
mralk3
Slackware Contributor
 
Registered: May 2015
Distribution: Slackware
Posts: 1,900

Rep: Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050
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 08:27 PM.
 
Old 04-17-2016, 10:54 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 10:19 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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



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 12:22 AM
Bash Script passing arguments dimitriauxio Linux - Newbie 8 09-16-2013 09:19 AM
Bash - passing associative arrays as arguments Lucien Lachance Programming 12 08-26-2013 12:43 PM
[SOLVED] BASH - passing arguments Garrett85 Programming 4 01-09-2013 03:03 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM

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

All times are GMT -5. The time now is 07:30 PM.

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