ayan.c
ayan.c

Reputation: 273

How to write a Linux script to run multiple files from a single executable?

I have an executable which works on a file & produces another file. I need to provide the first entry of the file as an argument of the executable. Suppose the executable name is "myexec" and I am trying to run a file "myfile.extension" "myfile.extension" has a format like this:

7 4 9 1 4 11 9 2 33 4 7 1 22 4 55 ...

While running the executable, I have to type the following:

myexec 7 myfile.extension

and it produces a file named myfile.extension.7

My question is that how can I write a script that will do this for a bunch of files in a directory?

Upvotes: 2

Views: 1553

Answers (1)

vinod
vinod

Reputation: 2368

Here's a bash script that you can execute in the directory with the files. It assumes the first word in the file is the argument:

for f in *
do 
   i=$(awk 'NR==1{print $1;exit}' $f)
   myexec $i myfile.extension
done

Edit: Using awk instead of cut | head. Mentioned by brianadams in the comments.

Upvotes: 1

Related Questions