0

I'm making a little conversion script in Zenity, but have noticed that ctmconv won't allow spaces in a filename. Can I get rid of spaces in the input filename?

#!/bin/bash
cd ~/Desktop
file="$(zenity --title "3D Converter:         Select a File to convert" --file-selection --file-filter=""*.ctm" "*.ply" "*.stl" "*.3ds" "*.dae" "*.obj" "*.lwo" "*.off"")"
filetype=$(zenity --list --title="Select File Type" --text="file types\ selection" --column="Select" --column="File Type" --radiolist false 3ds false ctm false dae false lwo false obj false off false ply true stl false wrl)
szSavePath="$(zenity --file-selection --filename="${file%%."${file##*.}"}"."$filetype" --save --confirm-overwrite)"
# szSavePath="$(zenity --file-selection --filename="out"."$filetype" --save --confirm-overwrite)"
ctmconv "$file" $szSavePath
zenity --info --text "Conversion Completed"

The above script gets the file to be converted, then asks for the filetype to convert to. It then replaces the original filetype with the new one and passes the result to ctmconv. Probably a messy way to do it, but hey ho! I'm new to this.

ctmconv just falls over if "$file" is "model rail", but is fine if it is just "model"

5
  • Does it still fail if you do something like ctmconv model\ rail.obj model\ rail2.ply .I don't have ctmconv or any sample files so it would be hard for me to test. Also, have you read the man page for ctmconv ? Maybe it has suggestions for it Jun 22, 2016 at 22:43
  • Also, "$file" is properly quoted, which is good, but you didn't quote $szSavePath that could be an issue there as well. Jun 22, 2016 at 22:45
  • Now then! Just typing in the command line "ctmconv model\ rail.obj model\ rail2.ply" worked perfectly. So that's a start.... And yes, I shall amend it to "$szSavePath". What should I do next?
    – user256787
    Jun 24, 2016 at 19:54
  • Your --file-filter quoting is a bit off. Why is there a "" at the start?
    – muru
    Jun 24, 2016 at 20:10
  • If I use only one quote at either end, the filter doesn't work for some reason.. I only get the first choice (.ctm) appearing.
    – user256787
    Jun 25, 2016 at 15:37

2 Answers 2

0

For some very odd reason , ctmconv doesn't recognize quoted variables as one entity if their contents contain spaces. (Which may also be a worthwhile to report this behavior as a bug to the developers).

I suggest you use any text-editing tool , such as sed or awk to insert backslash before space. For instance, something like this.

$ echo "with space" | sed 's/\ /\\ /g'                                         
with\ space

In case of your variable, something like this will do:

$ file="with space"
$ file="$( echo $file | sed 's/\ /\\ /g'  )"
$ echo "$file"
with\ space
3
  • So I've got: nfile="$(echo $file | sed 's/\ /\\ /g' )" echo "$file" echo "$nfile" ctmconv "$nfile" "$szSavePath" zenity --info --text "Conversion Completed" and now I get ctmconv saying it can't find Model\ Rail.stl, So I haven't quite got it yet. Nor have I got the Edit comments here right either.. Sorry.
    – user256787
    Jun 25, 2016 at 16:31
  • How about if you leave $nfile in ctmconv ... line unquoted in this case ? Jun 25, 2016 at 19:21
  • Going fron .stl to .ctm, it looks like I now get the "output" as an error. But it's close... Error: Invalid argument: /home/alistair/Desktop/Model Rail.ctm
    – user256787
    Jun 25, 2016 at 22:30
0

I cannot get the hang of commenting. I can't post the code as it's too long, I can't upload it. I can't eveng get a new line on it, so I have to answer my own question

#!/bin/bash

cd ~/Desktop
file="$(zenity --title "3D Converter:         Select a File to convert" --file-selection --file-filter=""*.ctm" "*.ply" "*.stl" "*.3ds" "*.dae" "*.obj" "*.lwo" "*.off"")"
file="$( echo $file | sed 's/\ /\\ /g'  )"
filetype=$(zenity --list --title="Select File Type" --text="file types\ selection" --column="Select" --column="File Type" --radiolist false 3ds false ctm false dae false lwo false obj false off false ply true stl false wrl)
szSavePath="$(zenity --file-selection --filename="${file%%."${file##*.}"}" --save --confirm-overwrite)"
echo ctmconv $file $szSavePath."$filetype"
ctmconv $file $szSavePath."$filetype"
zenity --info --text "Conversion Completed"`

I just put echo ctmconv etc. so you don't need ctmconv to see the output if you execute in a terminal..

ctmconv /home/alistair/Desktop/plastic/Buffers/Model\ Rail.stl /home/alistair/Desktop/Model\ Rail.3ds
Error: Invalid argument: /home/alistair/Desktop/Model\`

ctmconv falls down trying to output the file: the input is accepted.

But if I copy, paste and execute the "echo"ed line, it works perfectly....

What if I could just remove the spaces from the szSaveFile. Could that be done?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.