I want to execute a bash script on a list of text files (using a for loop, I guess), redirecting stdout to a file with the same path as the text file from stdin plus an extension (for example .txt or .sam, or just add a ~ to the end of the stdin path to make it different).
I want to execute a bash script on a list of text files (using
Share
Okay, assuming the list isn’t too long, you can start with
If the list is long enough to risk a too-long command line, there’s a while variant
you can construct the file name using basename(1) to get rid of the old extension suffix; for say .txt to .bak use
you can get the path part using dirname(1)
Since macros are expanded early, you can simply construct the command line in variables and expand them. Where op and np are your path names
There’s a lovely on-line book, the bash Advanced Scripting Guide that covers this and more.