1

i got this list

commands = ['cd var','cd www','cd html','sudo rm -r folder']

I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?

how to achieve that?, thanks all!!!!

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
Mauro Encinas
  • 59
  • 2
  • 5
  • Possible duplicate of [running multiple bash commands with subprocess](https://stackoverflow.com/questions/17742789/running-multiple-bash-commands-with-subprocess) – l'L'l Nov 15 '18 at 00:46
  • 1
    You could condense the `cd` commands into one command, then use Bash's `&&` (and) operator: `os.system("cd var/www/html/ && sudo rm -r folder")` – Aviv Shai Nov 15 '18 at 01:04
  • What does Python have to do with this? You said "execute as a bash script". – John Gordon Nov 15 '18 at 01:34
  • That's a wacky thing to want to do anyway. Your command is equivalent to `sudo rm -r var/www/html/folder` – tripleee Nov 15 '18 at 05:56
  • 1
    Possible duplicate of [Delete a file or folder](https://stackoverflow.com/questions/6996603/delete-a-file-or-folder) – tripleee Nov 15 '18 at 05:57
  • 1
    Maybe see also https://stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses. – tripleee Nov 15 '18 at 06:23

3 Answers3

5
for command in commands:
    os.system(command)

is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact

NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)

if you need to run them all in one subshell than you need to chain them together with "&&"

os.system(" && ".join(commands)) # would run all of the commands in a single subshell

as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)

really you should reformat your commands to simply

commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file

also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • This won't work because `cd var` will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process. – tripleee Nov 15 '18 at 05:55
  • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour – Joran Beasley Nov 15 '18 at 06:22
  • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses. – tripleee Nov 15 '18 at 06:23
  • Alao you should prefer `subprocess.run()` or `subprocess.check_call()` over `os.system()`; see the post I linked from another comment above. – tripleee Nov 15 '18 at 06:25
  • none of the *compelling* reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require `shell=True` so its pretty extra moot ... that said in general yes... this was just less typing – Joran Beasley Nov 15 '18 at 06:26
3

This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():

from os import chdir
from os import getcwd
from shutil import rmtree

directories = ['var','www','html','folder']

print(getcwd())
# current working directory: $PWD

for directory in directories[:-1]:
    chdir(directory)

print(getcwd())
# current working directory: $PWD/var/www/html

rmtree(directories[-1])

Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
2
declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

## now loop through the above array
for i in "${command[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also
Kalangu
  • 135
  • 10
  • this is awesome :) I think they were asking how to do it in python but this is great! thanks! – Joran Beasley Nov 15 '18 at 01:22
  • A python list can be very easily imported in to bash https://stackoverflow.com/questions/26162394/convert-a-python-data-list-to-a-bash-array – Kalangu Nov 15 '18 at 21:42