-1

Use python os.system to call a bash command, I find that %% has no effect.

This is my python code:

outputpath = "./"
command = "for dir in $(ls -d " + outputpath + '*/' + ");do echo ${dir%%/}/*.jpeg;done"

print(command)
os.system(command);

The running result is:

for dir in $(ls -d ./*/);do echo ${dir%%/}/*.jpeg;done  
./maps//*.jpeg  
./maps//*.jpeg  

The "%%/" is no effect!

When I run for dir in $(ls -d ./*/);do echo ${dir%%/}/*.jpeg;done in the shell, the result is ok.

Why?

Grobu
  • 599
  • 1
  • 11
  • 1
    Since you know every `$dir` has a final `/`, there's no need to remove it just to add it back again: `for dir in ./*/; do echo "$dir"*.jpeg; done` (don't parse the output of `ls`; remember to quote variable expansions). – Toby Speight Aug 17 '23 at 07:20
  • 2
    @jared_mamrot `$SHELL` isn't useful for this; it shows the user's default (interactive) shell, which might be different from what `os.system()` uses. – Gordon Davisson Aug 17 '23 at 07:48
  • Perhaps a different shell is used. Please put a `echo $BASH_VERSION - $BASH_VERSINFO - $0; set -x;` at the start of your command, and tell us what you get as output. However, even in a POSIX shell, I would expect `${dir%%/}` to behave as in bash. – user1934428 Aug 17 '23 at 08:31
  • 1
    It's **definitely** a different shell; `os.system()` uses sh, not bash. – Charles Duffy Aug 17 '23 at 21:36
  • 2
    That said, `for item in $(ls ...)` [is an antipattern anyhow](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29) – Charles Duffy Aug 17 '23 at 21:37
  • Also, your code has serious security problems, as it's passing a directory name into a shell in a context where that name could be parsed as code. If some joker tries to create `/uploads/$(rm -rf ~)/hello.jpg`, you _really_ don't want a script using the above code to try to list the contents of that directory. (A cleverer joker might use `/uploads/$(rm -rf ~)'$(rm -rf ~)'/hello.jpg` instead). – Charles Duffy Aug 17 '23 at 21:39
  • 1
    what's your OS, which version? What's your ultimate objective with this piece of code? Why mix shellscript and Python? – Grobu Aug 18 '23 at 07:31
  • @CharlesDuffy I test: sh -c 'for dir in $(ls -d ./*/);do echo ${dir%%/}/*.jpeg;done', it's ok, same as bash -c 'for dir in $(ls -d ./*/);do echo ${dir%%/}/*.jpeg;done' – user1343509 Aug 18 '23 at 14:41
  • @user1934428 run with os.system, the result is 3.2.57(1)-release - 3 - sh. – user1343509 Aug 18 '23 at 14:53
  • Instead of `print(command)`, does `print(repr(command))` have any surprises? – Charles Duffy Aug 18 '23 at 15:27
  • (btw, what's the point of `$(dir%%/}` instead of just `${dir%/}`? No reason to make a constant-suffix match greedy) – Charles Duffy Aug 18 '23 at 15:28
  • So we see that you don't run "real" bash, but bash by the name of "sh". In general, this is important to know, because this "sh"-bash behaves different from a real bash. However, even if run by sh, the effect of your `%%` expansion should be the same as in bash. Perhaps you could narrow down the problem, by showing a concrete value for `dir`, and what you get as result for `${dir%%/}`? – user1934428 Aug 19 '23 at 11:29

0 Answers0