-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_command.py
More file actions
executable file
·36 lines (29 loc) · 999 Bytes
/
run_command.py
File metadata and controls
executable file
·36 lines (29 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/python3
import array
import os
import subprocess
def run_command_in_subdirectories_one_level(command):
disallowed = ['rm', 'mv', 'kill', 'shutdown', 'reboot']
for dis in disallowed:
if command.startswith(dis):
print(f'Invalid command: {command}')
quit()
print(f'Command : {command}')
working_dir = os.getcwd()
print(f'Working Directory : {working_dir}')
dirs = list()
for file in os.listdir(working_dir):
if (os.path.isdir(os.path.join(working_dir, file))):
dirs.append(file)
dirs.sort()
for subdir in dirs:
print(f'\n===================================================')
print(f'{subdir} : {command}\n')
os.chdir(subdir)
try:
subprocess.run(command, shell=True, check=True)
except Exception:
print(f'Error')
os.chdir('..')
command = input("Enter the shell command to run: ")
run_command_in_subdirectories_one_level(command)