For in loop - two input variables

When I create a newfile, I am using the filename as a variable to create the new filename. When I ouput it, the filename contains the file extension in the middle of the file
example:
router1.txtshcdpneighbors.txt
router2.logshcdpneighbors.txt
My initial approach was to strip it out, now I would like to strip out .txt or .log. How can I add an or statement to the for in statement?

Here is the code that I have now:

for i in ./*.txts* or ./*.log*
do mv -- "$i" "${i//.txts/-s}"
done

First off: generally, BUT ESPECIALLY IN SHELLSCRIPTING it helps if you tell us which shell you use and ideally which system you use.

There are several things wrong with this:

for i in ./*.txts* or ./*.log*

A for-loop doesn't work in shell like in other languages: it is given a list of distinct values and the loop-variable takes one after the other value in each pass. In light of this: ./*.txts* will be expanded by the shell prior to executing the loop to a list of filenames fitting the pattern. In this case every file in the currecnt directory which name contains the string ".txts". Then it will add "or" to the list. At last it will expand ./*.log* . So finally, after the expansion, you have a statment like:

for i in ./foo.txtsx ./bar.txtsy or ./foo.logx ./whatever.logbla

and these values will be assigned consecutively to "$i". This is presumably not what you want, yes? What you perhaps want is a list of files ending either in ".txts" or in ".log". This would look like this (notice that i took the trailing asterisk out, so now it will match "file.log" but not "file.log.old"):

for i in ./*.txts ./*.log

The next issue is this command:

mv -- "$i" "${i//.txts/-s}"

The expansion ${i//.txts/-s} says: take the content of "$i", replace every occurrence of ".txts" and replace it with "-s", then display the result. So the above-mentioned example "./foo.txtsx" will become "./foo-sx" and the resulting command would be:

mv -- "./foo.txtsx" "./foo-sx"

Furthermore the files named "*.log*" will probably be unchanged at all because they probably will not contain the string ".txts" in their names. If they do a possible result would look like:

mv -- "./foo.txtsx.logbar" "./foo-sx.logbar"

Which, i suppose, is also not what you want.

One last point, but this is not a show-stopper: i suggest you format your for-loops differently so that they are easier to read:

for var in <LIST> ; do
     command "$var"
done

This way the command stands out and it is easy to see where the loop starts and ends.

Baseline is: tell us in clear-text what you want to do and we can probably better correct your code.

I hope this helps.

bakunin

@bakunin, the var// / modifier exists in bash or zsh or ksh93 (and works the same).

But a "wanted output" would clarify things.
I guess you want to delete embedded strings .txt OR .log from the file names.
Unfortunately there is no OR in the variable modifier.
A solution is: one loop for each string. You can consolidate it into a function

cut_from_fnames(){
  text=$1
  subst=$2
  [ -n "$text" ] || exit
  for i in ./*"$text"*.?*
    do mv "$i" "${i/$text/$subst}"
  done
}

And run the function two times

cut_from_fnames ".log" "-"
cut_from_fnames ".txt" "-"

Notes:
In general the mv -- is a good idea if $i starts with a -
but in this case not needed because it starts with ./
The (trailing) ? glob requires one (more) character
The var// / variable modifier is greedy, while the var/ / modifier stops at the first match

1 Like