SUMMARY I have a shell script to use in Dolphin ServiceMenu, cleaning filenames particularly for cross-platform compatibility, which runs as expected in terminal, but when used in a Dolphin ServiceMenu, it leaves a leading space in filename. STEPS TO REPRODUCE 1. Run this command in terminal: touch " a truly <horrible>|"awful" "$'\n'"name with a newline and *globs*, and even a 'single' quote or two?! .txt" 2. And then this command: rename 's/[*\n<|>"[\]]/ /g; s/\?//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s+/ /g; s/^\s*//; s/\s+\././g; '"s/'//g" * 3. You will see a clean filename if you run this command: ls -N 4. Enter that command in a shell script file, starting with "for filename in "${@}"; do" and ending with " "$filename"; done". 5. Add this shell script file to an appropriate ServiceMenu file. 6. Repeat step 1 and then run this script this time from context menu. OBSERVED RESULT One leading space is left in filename. EXPECTED RESULT No leading space should be left as in terminal. SOFTWARE/OS VERSIONS Windows: macOS: Linux/KDE Plasma: Kubuntu 20.04 (available in About System) KDE Plasma Version: 5.18.5 KDE Frameworks Version: 5.68.0 Qt Version: 5.12.8 ADDITIONAL INFORMATION
Can you attach the shell script as well? I am having trouble getting the rename command to work given your instructions.
Created attachment 132854 [details] servicemenu script
Created attachment 132855 [details] servicemenu .desktop file
Thank you providing those files. I tried your rename command and it returns rename: not enough arguments Then I created a new script that purely uses logger to log the filename in the for loop. I confirmed that the the filename was logged properly. Can you confirm that the rename step from your script works on its own? I am not familiar enough with the rename command, nor your regex to figure out what it is trying to do.
(In reply to David Greengas from comment #4) Unfortunately I don't know much about this rename package I've installed from official Ubuntu repositories. It seems it's a small Perl tool making use of sed to rename files. We can test it in terminal like this: mkdir rename cd touch " a truly file name"$'\n'"with a newline and *globs*, and even a 'single' quote or two .txt" This file can be seen (better than ls) with this command: for f in *; do echo "$f"; done Then we can run this command: for f in *; do rename 's/[*\n<|>"[\]]/ /g; s/\?//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s+/ /g; s/^\s*//; s/\s+\././g; '"s/'//g" "$f"; done This results in a "clean" name, without potential incompatibilities, and no leading space, as different from when this same command is executed via Dolphin ServiceMenu.
This is what I get when I run your suggested for loop rename: not enough arguments Try 'rename --help' for more information. rename: not enough arguments Try 'rename --help' for more information. This returns 1. This is consistent with what I am getting when running as a service from Dolphin. I was able to use a similar service file to the one you suggested and loop through the files correctly and call the file command against them. In order to help with debugging the service definition, I used the logger command to log output the journald. Then I was able to tail the journal via journalctl -f and watch the output from the service being called. $? is a special variable that has the return code from the last command (in ZSH and BASH). I also used logger to log the return code from the rename command.
Created attachment 132906 [details] terminal output and rename package
(In reply to David Greengas from comment #6) Strange in deed. I've added another attachment showing how it works in my terminal as well as the package details from my Kubuntu 20.10 system.
Oh, I've just noticed that recently I was omitting the <horrible>|"awful" part in the filename! Makes no difference though. ;-)
Created attachment 132908 [details] terminal output and rename package
New information was added with recent comments; changing status for inspection.
I was able to reproduce the behavior the reporter mentioned (the `rename` program they used is called `perl-rename` on many distros). Examining the issue further, I noticed that the filename passed to the script is the full path, i.e., something like `/home/user/ a truly <horrible>|"awful" "$'\n'"name with a newline and *globs*, and even a 'single' quote or two?! .txt`. Hence, the white space is no longer at the beginning at the string, and will no longer be removed by the script the reporter provided. As passing in the full path is intended behavior, I am closing this report. As an alternative, the reporter could consider using another script that can handle taking in full paths to files, and intelligently removing spaces in front.
(In reply to fanzhuyifan from comment #12) Oh, thank you so much! I've revised my script as per your advice, and it's working perfectly now: for filename in "${@}"; do source_dir="$(dirname "$filename")" source_file="$(basename "$filename")" cd "$source_dir" rename 's/[*\n<|>[\]]/ /g; s/\?//g; s/!//g; s/"//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s\s*/ /g; s/^\s\s*//; s/\s\s*\./\./g; '"s/'//g" "$source_file" done