INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Passing multiple arguments in Post Processing  (Read 2311 times)

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Passing multiple arguments in Post Processing
« on: July 29, 2021, 12:25:37 pm »

I am trying to run a script after recording TV that requires 2 arguments.  This is in Linux if it matters.

the command is something like "/home/muzicman0/SomeScript.sh"

in the arguments, I have tried many things, like "[Filename]" "[Series]" or "[Filename]","[Series]" or  "[Filename]";"[Series]", but no matter what I do, it is passed as a single argument, so the script gets something like:

"/home/muzicman0/recordings/TV/someseries/someshow.ts" " "

I have checked the script by running it manually, and it works as expected.

Is there a way to pass multiple arguments to a bash script?  I even tried adding them to the 'Command' line, but that failed to even run.
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #1 on: August 02, 2021, 03:46:53 pm »

bump.

Is it possible to send 2 arguments in the 'Run Command' that is processed after a recording?
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #2 on: August 02, 2021, 03:50:11 pm »

It looks more like [Series] is still empty when the command is executed. Perhaps MC only populates it after running the command.
Can you try something like "[Filename]" "test" and check if "test" is passed on?
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #3 on: August 02, 2021, 04:14:56 pm »

yeah, I thought that too, so I passed hard values, like you suggest (as a test).  For reference, the current test script is:
Code: [Select]
#!/bin/bash
echo "Filepath: $1" >> /home/muzicman0/MoveRecordings.txt
echo "Series Name: $2" >> /home/muzicman0/MoveRecordings.txt

The output with a hard values (for the 2nd argument) was:
Code: [Select]
Filepath: "/home/muzicman0/recordings/TV/Supernatural/Supernatural - S09E17 - Mother's Little Helper.ts" "test"
Series Name:

I passed "test" as the 2nd variable.  I don't remember the syntax I used, but my guess is "[Filename]" "test".

In any case though, it still took the arguments as 1 argument instead of 2.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #4 on: August 02, 2021, 04:39:06 pm »

Try adding this to the top of the script:
Code: [Select]
set -- $*
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #5 on: August 02, 2021, 04:53:08 pm »

getting closer.  now I get 2 arguments, but as soon as it hits a space in the text, it considers it the next argument (so if the path has a space, then the next few words (until another space) is the 2nd argument). 
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #6 on: August 02, 2021, 04:59:37 pm »

This is a known bash issue  :-\
Some possible solutions in the link below, but maybe you can try a different shell or even using a python script?

https://stackoverflow.com/questions/12821302/split-a-string-only-by-spaces-that-are-outside-quotes
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #7 on: August 02, 2021, 06:10:22 pm »

I suppose I could do Python.  I did just learn it for another project. 

I guess since it was passed as "this is a path" (with the quotes), I would have expected it to parse it as a single argument (OK, 2 arguments in this scenario since I am passing 2 strings, both wrapped in quotes, but also both potentially having spaces).
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #8 on: August 02, 2021, 07:45:42 pm »

So I wrote a test script in Python, but it still has the same problem.  I can't send arguments that have spaces in them.

Code: [Select]
#!/usr/bin/python3

from datetime import datetime
import sys

def WriteLog(sMessage, WriteDate = True, lfName = "/home/muzicman0/log.txt"):
    now = datetime.now()
    dt_string = now.strftime("%m/%d/%Y %I:%M:%S %p")
    f = open(lfName, "a")
    if WriteDate == True:
        f.write(dt_string + ": " + sMessage + "\n")
    else:
        f.write(sMessage + "\n")
    f.close()
   
#print(sys.argv[1])
#print(sys.argv[2])

WriteLog ('File Path is ' + sys.argv[1])
WriteLog ('Series is: ' + sys.argv[2])

EDIT: I'm actually getting errors unless I don't reference Argument 2 in the python script (this is from the logs):
Code: [Select]
this is
Traceback (most recent call last):
  File "/home/muzicman0/Scripts/test_args.py", line 17, in <module>
    print(sys.argv[2])
IndexError: list index out of range

This further implies that I am still only able to pass 1 argument.  Not sure why.  I have tried single quote, double quote, no quote, etc.

Once I changed "WriteLog ('Series is: ' + sys.argv[2])" in the script to "WriteLog ('Series is: ' + sys.argv[1])" it didn't throw an error, but wrote both arguments as a single long argument.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #9 on: August 03, 2021, 03:17:31 am »

From what I'm reading, it looks like MC is executing via an API which does shell-escaping of the args (or MC is explicitly doing that). In effect, all spaces are replaced with '\ ' causing the string to be interpreted as a single arg.

Perhaps someone from MC team can shed some light here? It might be a bug in MC Linux.

However, using python makes it easy to split the args:
Code: [Select]
>>> import shlex
>>> shlex.split('"first path" "second path"')
['first path', 'second path']
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #10 on: August 03, 2021, 09:39:51 am »

Awesome, thanks!  It is working now.  I suppose if it is a bug, and is ever fixed, I will have to re-do it, but that should be easy.  Below is my test script (just for reference).  And again, THANKS so much!  I learned programming on a Vic-20 in the early 80's, and sometimes the modern stuff is a bit hard for me to wrap my head around, but If I can get a direction, I can usually figure it out!

Code: [Select]
#!/usr/bin/python3

from datetime import datetime
import sys
import shlex

def WriteLog(sMessage, WriteDate = True, lfName = "/home/muzicman0/log.txt"):
    now = datetime.now()
    dt_string = now.strftime("%m/%d/%Y %I:%M:%S %p")
    f = open(lfName, "a")
    if WriteDate == True:
        f.write(dt_string + ": " + sMessage + "\n")
    else:
        f.write(sMessage + "\n")
    f.close()

sArgList = shlex.split(sys.argv[1])

WriteLog("First Argument is: " + str(sArgList[0]))
WriteLog("Second Argument is: " + str(sArgList[1]))
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #11 on: August 03, 2021, 09:54:03 am »

Cool :)

You can make it future proof by joining all args before splitting again:
Code: [Select]
shlex.split(" ".join(sys.argv[1:]))
edit: remove extra parenthesis
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #12 on: August 03, 2021, 10:12:18 am »

Thanks.  Just for reference, I think you had an extra ')'.  I need to change it to:

Code: [Select]
shlex.split(" ".join(sys.argv[1:]))
to make it work.  Again, thanks!
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #13 on: August 03, 2021, 02:15:27 pm »

Well...unfortunately, it didn't work as I expected.  I am trying to create a script that will move recordings when they are done over a VPN connection.  Problem seems to be that if recordings end at the same time, even though each is calling the script, only 1 runs.  For example as a test, I started recording 6 shows, that all ended at 12 PM.  Only one actually transferred (and only that same one logged any info at all, so pretty sure the other 5 recordings didn't even call the script).  If I record 6 shows, and cancel them 1 by 1, they all work fine, even if one script is still running, so I really don't know why it doesn't work otherwise.

I want to use an external script to keep the processing off of JRiver.  I don't want to risk a crash while moving files, etc because something goes wrong.

I guess I will leave my current solution in place for now which is a cron job that runs during the night using rsync.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2365
Re: Passing multiple arguments in Post Processing
« Reply #14 on: August 03, 2021, 02:38:13 pm »

Maybe the first script is locking some common resource and the other instances are crashing because of that.
Try logging to "log.{}.txt".format(os.getpid()), and check for other possible conflicts.

You can also check MC log to see if it's calling multiple script instances (not sure if this is logged).
Logged

muzicman0

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1239
Re: Passing multiple arguments in Post Processing
« Reply #15 on: August 03, 2021, 03:14:48 pm »

Thanks.  I am headed out of town for a few days.  when I get back, I will give it a shot.
Logged
Pages: [1]   Go Up