Post ALZfOFYADDGmDXSjui by aegilops@fosstodon.org
 (DIR) More posts by aegilops@fosstodon.org
 (DIR) Post #ALXG9NeIOiTMfuMbNg by nicemicro@fosstodon.org
       2022-07-16T02:43:19Z
       
       0 likes, 1 repeats
       
       Hi, shell scripting expert friends!I am writing a python script, that prints a list of files to standard output via the print command, with the intention to be able to use this as an argument list for a shell command like remove etc.The final syntax would be something like:$ rm $(python script.sh)However, if the returned path contains a space, this does not work.#bash #linux #shell #askfedi #programming
       
 (DIR) Post #ALXGIZtaEbNxGNtrLU by nicemicro@fosstodon.org
       2022-07-16T02:44:59Z
       
       0 likes, 0 repeats
       
       If I set up the python script to print either " or ' at the beginning and the end of each path, it still fails and the rm command interprets the spaces as argument boundaries and fails to find the files.If I replace all space in the path to backslash + space to escape it, it still doesn't work.How is command substitution supposed to handle the space character, and how can I force the shell to handle paths with space in it correctly?Thanks in advance!
       
 (DIR) Post #ALXKrByjY4lr2R4O2K by birnim@fosstodon.org
       2022-07-16T03:36:03Z
       
       0 likes, 0 repeats
       
       @nicemicro I would rather make the python script print one path per line and process it like this in the shell script: https://dpaste.org/ase4E , https://dpaste.org/ymc45As you can see, there's no additional logic for handling whitespaces in filenames. And no risk to overflow the command line for rm my passing it too much.
       
 (DIR) Post #ALXNPc5PBgATlPjP4S by nicemicro@fosstodon.org
       2022-07-16T04:04:41Z
       
       0 likes, 0 repeats
       
       @birnim I see. It is sad that I'll have to use a separate shell script to handle each of the files, but I guess you are right, it is easy to overflow rm if I have too many files.Thank you!
       
 (DIR) Post #ALXNaWy2YjXR4cUa5A by birnim@fosstodon.org
       2022-07-16T04:06:40Z
       
       0 likes, 0 repeats
       
       @nicemicro Why a separate shell script for each file? You don't provide context, but I assume that your example rm call already belongs to a script?
       
 (DIR) Post #ALXNptme0lg8BFUR84 by nicemicro@fosstodon.org
       2022-07-16T04:09:27Z
       
       0 likes, 0 repeats
       
       @birnim The script lists files that conform to certain criteria, so I thought it would be nice if the script just outputs the list, so I can check if I set up my criteria properly, and then only delete the files once the fine tuning is complete.I guess I could just add a --dry_run switch to the script though for debugging, or a --remove switch for the real run.
       
 (DIR) Post #ALXPzZqRCLIV950Uhk by IslandUsurper@fosstodon.org
       2022-07-16T04:33:36Z
       
       0 likes, 0 repeats
       
       @nicemicro @birnim you want xargs. This takes lines of input and runs some other program on each line (or possibly batches of lines). Something like `python script.py | xargs -n 10 -I ‘{}’ rm ‘{}’`. That’s all from memory, so double check me before running stuff from an Internet stranger.
       
 (DIR) Post #ALXQPRc2Qaqd8ubdx2 by aegilops@fosstodon.org
       2022-07-16T04:38:16Z
       
       0 likes, 0 repeats
       
       @nicemicro you can deal with spaces by quoting the variable with double quotes. If you run ShellCheck on the script it should make that suggestion!
       
 (DIR) Post #ALZYSRo3syD9i1xA1Y by nicemicro@fosstodon.org
       2022-07-17T05:17:52Z
       
       0 likes, 0 repeats
       
       @aegilops by double quotes do you mean this character: " or do you mean putting in two quotation characters?because for the " character, I tried, and unfortunately it doesn't do as it should.
       
 (DIR) Post #ALZfOFYADDGmDXSjui by aegilops@fosstodon.org
       2022-07-17T06:35:33Z
       
       0 likes, 0 repeats
       
       @nicemicro like this, using "..." around the variable. That avoids injecting the spaces into the shell, which among other things can prevent security vulnerabilities:```while read -r líne do echo "$line"; done < inputfile```
       
 (DIR) Post #ALa2rGnYwoNvQtTKqG by aegilops@fosstodon.org
       2022-07-17T07:32:27Z
       
       0 likes, 0 repeats
       
       @nicemicro oops, ignore the accent on the variable! 🤣
       
 (DIR) Post #ALa2rHDnNHMukFkHjM by nicemicro@fosstodon.org
       2022-07-17T10:58:30Z
       
       0 likes, 0 repeats
       
       @aegilops Ohh, okay, I think I get it now!So you're talking about using a script! It makes sense now, thanks.