Python shell scripts

Looking for help to find python shell script examples. Maybe some sort of templates. I'am best at learning by example, so need massive amounts of different examples how people write python scripts for shell.

Comments

  • I know you're most probably looking for some kind of code bundle to download & experiment with but yeah, StackOverflow is the best place to learn from snippets and scripts since in-depth discussions are usually included within the threads.

  • You could try Copilot. It doesn’t produce perfect code, but it’s excellent for guiding you with the process of getting started with a language, or when exploring an unfamiliar topic.

  • Here's a command line hook from a package I made for work:

    https://github.com/mwt/crowdmark-labeler/blob/master/clabeler/command_line.py

  • On a slightly unrelated note, check out Xonsh, it's a shell where you can directly do Python in. I've used it a few times, didn't really kick off but the experience was pleasant.

    Worth taking a look if you're planning to do significant amount of work on shell scripts since it can get verbose without shell-isms (e.g. reading stdout after spawning a process is already a few lines).

    Thanked by (1)mwt
  • @jmgcaguicla said:
    On a slightly unrelated note, check out Xonsh, it's a shell where you can directly do Python in. I've used it a few times, didn't really kick off but the experience was pleasant.

    Worth taking a look if you're planning to do significant amount of work on shell scripts since it can get verbose without shell-isms (e.g. reading stdout after spawning a process is already a few lines).

    That's very cool. It's not python, but there's a shell I've been interested in called elvish:

    https://elv.sh/

    It also fits into this space between a shell and a traditional programming language.

  • Guys, I know that there is python guru's out here. Please help, because I'am in a corner:

    #!/usr/bin/env python3
    
    from pwd import getpwnam
    import subprocess as sp
    import shlex
    import argparse
    import json
    from configparser import ConfigParser
    from pathlib import Path
    import random
    import string
    import tomli
    import os
    
        argument1 = f'setquota -q {username} {self.options.get("q")}M {self.options.get("q")}M {self.options.get("i")} {self.options.get("i")} /'.split(' ')
    
       set_user_quota = sp.run(argument1, capture_output=True, text=True)
    

    Here I receive this error:

    Traceback (most recent call last):
      File "/root/scripts/./aa.py", line 189, in <module>
        instance.set_folders_and_quota(instance.options["username"])
      File "/root/scripts/./aa.py", line 121, in set_folders_and_quota
        set_user_quota = sp.run(argument1, capture_output=True, text=True)
      File "/usr/lib/python3.9/subprocess.py", line 505, in run
        with Popen(*popenargs, **kwargs) as process:
      File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "/usr/lib/python3.9/subprocess.py", line 1823, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: 'setquota'
    

    What the hell?! Why there is that cryptic error:

    FileNotFoundError: [Errno 2] No such file or directory: 'setquota'
    

    I print argument1 and it is nicely formatted string as er subprocess module req:

    ['setquota', '-u', 'hvkugpalhw', '2048M', '2048M', '20000', '20000', '/']

  • That's not very cryptic; it's telling you it can't find the 'setquota' command you're asking it to run.

    Thanked by (1)legendary
  • @ahnlak said:
    That's not very cryptic; it's telling you it can't find the 'setquota' command you're asking it to run.

    Yes! This is it. But it is so strange. I thought that subprocess looking for file or directory with such name. Thank you my beautiful man.

  • subprocess.run will just spawn a shell for that command, skimming the manual. TL;DR is that if you can run on your command line, subprocess should pick it up. If it's not in your path for some reason, you should be able to specify the full pathname and have it run happily.

    Thanked by (2)legendary akundota
  • edited October 5

    @legendary said:
    What the hell?! Why there is that cryptic error:

    FileNotFoundError: [Errno 2] No such file or directory: 'setquota'

    That's as verbose as you can get, I have no idea how to make that error message better.

    Hint: subprocess.run doesn't use $PATH (apparently it does, since it uses an execvpe-like implementation underneath), also RTFM

    Thanked by (1)legendary
  • @jmgcaguicla said:

    @legendary said:
    What the hell?! Why there is that cryptic error:

    FileNotFoundError: [Errno 2] No such file or directory: 'setquota'

    That's as verbose as you can get, I have no idea how to make that error message better.

    Hint: subprocess.run doesn't use $PATH (apparently it does, indirectly, since it uses execvpe underneath), also RTFM

    Ahh, I saw popen in the callstack and naively assumed that Python would just call the actual OS popen instead of doing something weird :-)

  • edited October 5

    @ahnlak said:
    Ahh, I saw popen in the callstack and naively assumed that Python would just call the actual OS popen instead of doing something weird :-)

    It's just the name of the main class used in the subprocess module, it's all Python at the top (at least for subprocess.run, emulating execvpe PATH search semantics) then fork-exec at the bottom.

  • edited October 7

    Guys, I need some thoughts how to achieve desired functionality.

    1. Class has X methods, methods run like this:

      if __name__ == "__main__":
      
      pid = str(os.getpid())
      pidfile = "/tmp/create.pid"
      
      if os.path.isfile(pidfile):
          print('{} already exists, exiting'.format(pidfile))
          sys.exit()
      
      with open(pidfile, "w") as file:
          file.write(pid)
      
      try:
          instance = Base()
          instance.set_options()
      
          if instance.check_if_site_exists() == True \
                  or instance.create_user() == False \
                  or instance.set_folders_and_quota() == False \
                  or instance.set_db() == False \
                  or instance.get_info():
      
              log_action("options", 0, 0, json.dumps(Base.options))
              print(instance.send_info())
              exit(1)
          else:
              log_action("options", 0, 0, json.dumps(Base.options))
              print(instance.send_info())
              exit(0)
      finally:
          os.unlink(pidfile)
      
    2. I need to track each method status. This achieved with true/false.

    3. If method fails, I need to rollback current executed method and all previous methods.

    My question: IS there any method in python class which ALWAYS run when method in class is executed? Does init always run or just on class instantiation?

  • edited October 7

    @legendary said: Does init always run or just on class instantiation?

    init the constructor only runs once on class instantiation.

    @legendary said: IS there any method in python class which ALWAYS run when method in class is executed?

    You can do something similar using decorators.

    class SomeClass:
        def __init__(self):
            print("In constructor")
    
        def always_run(func):
            def wrapper(*args, **kwargs):
                # Do whatever you want to run before calling the method
                print("About to call", func.__code__.co_name)
                func(*args, **kwargs)
    
                # Do whatever you want to run after returning from the call
                print("Returing after call to", func.__code__.co_name)
            return wrapper
    
        @always_run
        def method1(self):
            """Decorated Method"""
            print("In method1")
    
        def method2(self):
            """Not a decorated Method"""
            print("In method2")
    
    sc = SomeClass()
    sc.method1()
    sc.method2()
    

    Output

    In constructor
    About to call method1
    In method1
    Returing after call to method1
    In method2
    

    Any function decorated with @always_run will always go through the wrapper function first. You can write your logic in the wrapper.

    Thanked by (2)ehab legendary
  • edited October 7

    @legendary said:
    My question: IS there any method in python class which ALWAYS run when method in class is executed?

    The keyword you're looking for is Aspects/Aspect Oriented Programming. Python probably had a pythonic equivalent that I don't know about.

    But aspects are generally for auxiliary tasks (instrumentation, logging, metrics) not critical functionality.

    Seeing you're relatively new to this I implore you to explain in more detail what you're trying to achieve so people can suggest better alternatives.

    Does __init__ always run or just on class instantiation?

    Only on class instantiation.

Sign In or Register to comment.