0

I am trying to run .mlx scripts using Meshlabserver through the Git Bash console on Windows. I have the .mlx scripts in a single folder, and I have a file called script_list.txt that contains a list of all the scripts, which looks something like that:

C:/My_Work_Files/Batch_resize_surfaces_test/resize_meshlab_5.mlx
C:/My_Work_Files/Batch_resize_surfaces_test/resize_meshlab_52.mlx
C:/My_Work_Files/Batch_resize_surfaces_test/resize_meshlab_54.mlx
C:/My_Work_Files/Batch_resize_surfaces_test/resize_meshlab_56.mlx
...

For what I want to do, I need to select a random .mlx script from the list and then pass it to Meshlabserver while calling it from Bash. For that purpose I have this code:

mapfile -t myArray < "C:\My_Work_Files\Batch_resize_surfaces_test\script_list.txt"
num=$(shuf -i 1-13 -n 1)
Script=${myArray[$num]}

The idea is that this code will be inside a loop and each time $num will be different which would fetch a random .mlx script from the list.

I call Meshlabserver using this code:

"C:\Program Files\VCG\MeshLab\meshlabserver.exe" -i "$file" -s $Script -o "${file%.*}_deci.ply" -m vn

However, it is telling me that $Script does not exist. When I manually assign the .mlx file to $Script like so:

Script="C:/My_Work_Files/Batch_resize_surfaces_test/resize_meshlab_5.mlx"

Meshlabserver finds the file and it runs without any issues. I cannot for the life of me understand what is the difference between fetching the filename and its path from a .txt file and passing it to a variable, and manually passing the same string to a variable. When I echo the two strings in the console they look identical. It is clearly something to do with Bash, but I can't figure it out.

Edit: for clarification, the complete code is this:

mapfile -t myArray < "C:\My_Work_Files\Batch_resize_surfaces_test\script_list.txt"
num=$(shuf -i 1-13 -n 1)
Script=${myArray[$num]}
"C:\Program Files\VCG\MeshLab\meshlabserver.exe" -i "$file" -s $Script -o "${file%.*}_deci.ply" -m vn
Antsushi
  • 167
  • 10
  • How are your script and the command where you start meshlabserver connected? Is it all the same script? If not, maybe the issue that you forgot to `export` the new variables. – CherryDT Sep 14 '21 at 14:26
  • @CherryDT Yes, sorry, it is the same script. You can imagine that the line that calls Meshlabserver goes right after the code the defines $Script. – Antsushi Sep 14 '21 at 14:28
  • 1
    Run your script with `bash -x` to enable logging, and add a line `declare -p myArray` to display the array's content as-read. – Charles Duffy Sep 14 '21 at 15:12
  • 1
    (I wouldn't be surprised if you had `\r`s at the end of your array elements). – Charles Duffy Sep 14 '21 at 15:12
  • 1
    BTW, it's a lot faster to calculate `$(( (RANDOM % 13) + 1 ))` than to mess with `shuf`. Also more portable, since `RANDOM` works everywhere bash is installed, whereas `shuf` is a separate utility than any given operating system may or may not provide. – Charles Duffy Sep 14 '21 at 15:13
  • 1
    @CharlesDuffy You were right, I had `\r` at the end of my array elements. Running `dos2unix` on `script_list.txt` fixed this issue. Thanks a lot, also appreciate the tip on `RANDOM`. – Antsushi Sep 14 '21 at 15:32

0 Answers0