0

Learning shell script after years of Windows scripting. I have qBittorrent-nox running in a TrueNAS 12.2 jail. qBittorrent provides a feature to run a command after a torrent download completes. I want to call a shell script to run two chown commands on the folder that is passed as a parameter. The folder will have spaces and may have ampersands, and the shell script fails as a result. The command passed according to qBittorrent's log file is this:

bash /mnt/torrents/Live/fixperms.sh "Name of the folder & description of contents"

This is the code I expected to work:

#!/usr/local/bin/bash
chown -R 1001:1006 $(printf "%q" "$1")

The command is correctly formed, but the script fails as it splits the string. The command it creates works if I echo it and execute it manually. I cannot find a way of making any shell (csh, sh, zsh and bash) NOT split the string at the spaces. I've tried it with single quotes, double quotes and backticks.

I have spent a number of hours on this and I have made no progress. I have tried all four shells, zsh splits the double-quoted string when the documentation says it shouldn't, parameter expansion works in bash but still splits the string. I have no python or perl, and no wish to install them if I can avoid it.

What am I missing?

Bash is 5.0.18, zsh is v5.8, tcsh is 6.2.00

  • 1
    Why not simply `chown -R 1001:1006 "$1"`? – oguz ismail Dec 04 '20 at 12:59
  • Apologies if you saw the previous comment, all was not what it seemed. Your suggestion is exactly where I started, and when I run the script manually with `bash /mnt/torrents/Live/fixperms.sh "/mnt/torrents/Live/Name of the folder & description of contents"` I get `: No such file or directoryame of the folder & description of contents`. there is a missing 'N' there – DarthMuppet Dec 04 '20 at 13:43
  • if I look at stderr, I see `chown: /mnt/torrents/Live/Name of the folder & description of contents: No such file or directory`. If I select the text, and put double quotes around it, and try to `cd` to it, it works. – DarthMuppet Dec 04 '20 at 14:11
  • 1
    Weird, did this happen on a Windows environment? I got a hunch that somehow a carriage return gets added to `/mnt/torrents/Live/Name of the folder & description of contents` (hence the missing N), that might be the culprit. – oguz ismail Dec 04 '20 at 14:20
  • 1
    Windows is where my strengths are, and I'm sure I'd have no issues with it. It's basically running on FreeBSD, which is NOT a strong area for me. Stupidly, the original script now works....not sure what to make of this, but I won't lose any sleep over it. – DarthMuppet Dec 04 '20 at 16:32
  • @DarthMuppet From the original error message, I'm pretty sure that your script was in DOS/Windows text format, which has a carriage return at the end of each line (in addition to the linefeed that unix tools expect). This causes a [wide variety of problems](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings). If you ever have trouble again, check the file format with the `file` command. – Gordon Davisson Dec 04 '20 at 18:22

1 Answers1

0

You could do the following

VAR="Name of the folder & description of contents"; bash /mnt/torrents/Live/fixperms.sh "$VAR"

Then the string is handled with the blank spaces in it. If you for example to this in a bash, it works:

    user@server:~$ VAR="test is test"
    user@server:~$ ./test2.sh "$VAR"
    test is test

test2.sh contains just an echo $1 so you are able to work with the entire string afterwards like

#!/usr/local/bin/bash
chown -R 1001:1006 $(printf "%q" "$1")