Skip to main content
added 6 characters in body
Source Link
ilkkachu
  • 148k
  • 16
  • 268
  • 441

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's:

#!/bin/bash
case folder in
  "Testordner")
     search_filename=*_0_*.txt
      ;;
  "Ordner2")
     search_filename=*_65_*fg*.txt
     ;;
  *)
     ;;
esac
for files in $search_filename; do
  echo $files
done

I had expected to get a list of the files fitting to the search criterium in the last echo. Instead I get the search criterium itself.

For testing I tried F="*.txt";for files in $F;do echo $files;doneF="*.txt";for files in $F;do echo $files;done directly in the bash (which worked and I got a list of files) and in the same script (which got me the output *.txt*.txt Already tried different variable expansion stuff (e.g. $(..) ((..)) ${..}$(..) ((..)) ${..}, which all did not work.)

Why is that and how can I get around?

Would be great to find help. Best wishes Caro


using Ubuntu 22.04LTS, KDE/plasma desktop; bash v5.1.16(1). Terminal: konsole v21.12.3

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's:

#!/bin/bash
case folder in
  "Testordner")
     search_filename=*_0_*.txt
      ;;
  "Ordner2")
     search_filename=*_65_*fg*.txt
     ;;
  *)
     ;;
esac
for files in $search_filename; do
  echo $files
done

I had expected to get a list of the files fitting to the search criterium in the last echo. Instead I get the search criterium itself.

For testing I tried F="*.txt";for files in $F;do echo $files;done directly in the bash (which worked and I got a list of files) and in the same script (which got me the output *.txt Already tried different variable expansion stuff (e.g. $(..) ((..)) ${..}, which all did not work.)

Why is that and how can I get around?

Would be great to find help. Best wishes Caro


using Ubuntu 22.04LTS, KDE/plasma desktop; bash v5.1.16(1). Terminal: konsole v21.12.3

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's:

#!/bin/bash
case folder in
  "Testordner")
     search_filename=*_0_*.txt
      ;;
  "Ordner2")
     search_filename=*_65_*fg*.txt
     ;;
  *)
     ;;
esac
for files in $search_filename; do
  echo $files
done

I had expected to get a list of the files fitting to the search criterium in the last echo. Instead I get the search criterium itself.

For testing I tried F="*.txt";for files in $F;do echo $files;done directly in the bash (which worked and I got a list of files) and in the same script (which got me the output *.txt Already tried different variable expansion stuff (e.g. $(..) ((..)) ${..}, which all did not work.)

Why is that and how can I get around?

Would be great to find help. Best wishes Caro


using Ubuntu 22.04LTS, KDE/plasma desktop; bash v5.1.16(1). Terminal: konsole v21.12.3

Source Link
Caro
  • 3
  • 3

bash: different behaviour of script and in terminal (loop over files)

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's:

#!/bin/bash
case folder in
  "Testordner")
     search_filename=*_0_*.txt
      ;;
  "Ordner2")
     search_filename=*_65_*fg*.txt
     ;;
  *)
     ;;
esac
for files in $search_filename; do
  echo $files
done

I had expected to get a list of the files fitting to the search criterium in the last echo. Instead I get the search criterium itself.

For testing I tried F="*.txt";for files in $F;do echo $files;done directly in the bash (which worked and I got a list of files) and in the same script (which got me the output *.txt Already tried different variable expansion stuff (e.g. $(..) ((..)) ${..}, which all did not work.)

Why is that and how can I get around?

Would be great to find help. Best wishes Caro


using Ubuntu 22.04LTS, KDE/plasma desktop; bash v5.1.16(1). Terminal: konsole v21.12.3