Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-17-2016, 12:49 PM   #1
LQ Newbie
Registered: Apr 2016
Posts: 10

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


in '$HOME/Data/' directory I have a list a input files:
The program I am using needs two inputs from the same file number (below an example for 'file1'):
my_program \
     -I:X \
     -I:Y \
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:
for A in *.tab

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

my_program \
     -I:X ${B}
     -I:Y ${B}
     -O ${B}


Last edited by beca123456; 04-17-2016 at 12:50 PM.
Old 04-17-2016, 08:26 PM   #2
Senior Member
Registered: May 2015
Location: Utah, USA
Distribution: Slackware 14.2 || Slackware-current && CentOS
Posts: 1,374

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

[~]$ 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
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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:
a=($(ls /tmp))
I am also not a fan of using ls, so would probably change the above to a glob:
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:
for (( i = 0; i < ${#a[*]}; i+=2 ))
Old 04-18-2016, 10:19 AM   #4
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 1,205

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


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 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:17 PM.

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