Skip to main content

how to pass many arguments to for loop? [Resolved]

I'm working in the same directory of the files. I have files with three different extensions. I want to perform each one of the five commands on a file with the specific extension by passing them as arguments to the for loop.

example: I want when I run the code like: $my_code.sh *.zap *.F *.T
I want the script to perform each command in the specific extension and prepare a list of command at the end and append them as output.

When I run the code as is, it will just take the first arguments (which contains files with *.zap files) and will perform all the commands on it, but what I want is apply each command in specific files with extension.

here is my code:

#!/bin/bash                                                                                                                                                                                                                           
frequ=$1                                                                                                                                                                                                                    
tim=$2                                                                                                                                                                                                                        
zap=$3

ls -1 * |                                                                                                                                                                                                                     
for i in "$@"; do                                                                                                                                                                                                       
        echo pav -g \""$frequ"_"avprof.ps/cps"\" -DT $frequ                                                                                                                                                     
        echo pav -g \""$tim"_"fprof.ps/cps"\" -Gd $tim                                                                                                                                                            
        echo pav -g \""$tim"_"ds.ps/cps"\" -j $tim                                                                                                                                                            
        echo pav -g \""$frequ"_"stack.ps/cps"\" -R $frequ                                                                                                                                                                   
        echo psrplot -D \""$zap"_"bp.ps/cps"\" -p  freq+ $zap                                                                                                                                                                              
done >> ps_files.txt 

Question Credit: abubakr yagob
Question Reference
Asked October 15, 2017
Posted Under: Unix Linux
35 views
3 Answers

One way I think that would make this better is if you send the input as just the extension, not the files that match the extension. The problem is that your script would otherwise have to weed out which files belong to which group. The loops then could become less complex as you can do the glob expansion with the extension inside.

The loop you currently have would work better as a while read loop, like so:

ls -1 * | while read i; do ... done >> ps_files.txt

or as a for, like so:

for i in `ls -1 *`; do ... done >> ps_files.txt

credit: Ed Neville
Answered October 15, 2017
 
It is still performing the whole commands on the first argument files(*.frequ) without the other two args. – abubakr yagob 5 hours ago
 CanDoerz  1 month ago
 
@Ed Neville Thank you for the quick response, I hear what you say I'll try it just in a minute. – abubakr yagob 6 hours ago
 CanDoerz  1 month ago
 
Aside from parsing output of ls which is already a great sin, doing for i in 'ls -1 *' is redundant. Just use globbing for i in * There's no need to spawn separate ls process to deal with files in current working directory. If it's a recursive case then yes, you'll need find -print0 | while IFS= read -r -d '' structure, but that's whole lot of different story. Also, for the love of sysadmin's coffee, please don't use backticks to do command-substitution, use $(...) like command1 $( command2 arg1 arg2 ) – Sergiy Kolodyazhnyy 6 hours ago
 CanDoerz  1 month ago
 
Shouldn't use ls mywiki.wooledge.org/ParsingLs – Jesse_b 6 hours ago
 CanDoerz  1 month ago

A simple for f will traverse all values provided as an argument list.

Do:

set -- *

And then, simply:

for f; do ...; done

That bypass completely the (in this case un-needed) call to ls, and will make the loop very simple.


credit: Arrow
Answered October 15, 2017
Your Answer