Frage

I have a small C++ program that reads arguments from the bash. Let's say that I have a folder with some files with two different extensions. Example: file1.ext1 file1.ext2 file2.ext1 file2.ext2 ...

if I execute the program with this command: ./readargs *.ext1 it will read all the files with the ext1.

if I execute ./readargs *.ext1 *.ext2 it will read all the .ext1 files and then all the .ext2 files.

My question is how can I execute the program in a way that reads with this order: file1.ext1 file1.ext2 file2.ext1 file2.ext2 ... Can I handle that from the command line or I need to handle it in the code?

War es hilfreich?

Lösung

If the names of your files is really in the form file1.ext1 file1.ext2 file2.ext1 file2.ext2, then you can sort it with

echo *.{ext1,ext2}|sort -u

the above gives the output:

$ ll | grep ext
23880775    0 -rw-r--r--  1 user users    0 Apr 29 13:28 file1.ext1
23880789    0 -rw-r--r--  1 user users    0 Apr 29 13:28 file1.ext2
23880787    0 -rw-r--r--  1 user users    0 Apr 29 13:28 file2.ext1
23880784    0 -rw-r--r--  1 user users    0 Apr 29 13:28 file2.ext2
$ echo *.{ext1,ext2} | sort -u
file1.ext1 file2.ext1 file1.ext2 file2.ext2

Then you copy the output and call your program. But if you do in fact need the files with .ext1 before the files of .ext2, then you have to either make sure that the .ext1 is alphabetically inferior to .ext2 or use another sorting criterion.

Optionally you could also adapt your executable to handle command line arguments in the correct order, but if you do already have an executable I'd recommend the first solution as work-around.

edit: this command does also sort lexically:

$echo *.ext[1,2]
$file1.ext1 file1.ext2 file2.ext1 file2.ext2
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top