I'm trying to code a simple script that uses sed to basic find&replace functionality.I want to have original file untouched and have results in new files called file.n (n is being incremented).My main problem is with numbering new files. This is basic code of mine that has static functionality for n=1 and n=2:#!/bin/bashn=1if [[ -f file.$n ]]then sed -e 's/'$2'/'$3'/g' $1 > file.$((n+1))else sed -e 's/'$2'/'$3'/g' $1 > file.1fiInput in terminal looks like: ./script text_sample.txt word_1 word_2This code works okay for first and second use (if file.1 hasn't existed, it's going to save results here; else it will be saved in file.(n+1) which is file.2).But how do I make it for file.3, file.4 etc? After launching this script every single time, at start it's n=1 and I don't really have more ideas how I can deal with it.I've already tried some export variables, using specific variable as a command (to make this integer more like global); I guess some loops might be needed...Any help would be appreciated.
Getting access to incrementing extension of file (as a integer variable)
bash;shell script;shell;zsh
Try this:#!/bin/bashn=1while [[ -f file.$n ]]; do    n=$((n+1)donesed -e 's/'$2'/'$3'/g' $1 > file.${n}