INTERACT FORUM
Windows => Television => Topic started by: muzicman0 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.
-
bump.
Is it possible to send 2 arguments in the 'Run Command' that is processed after a recording?
-
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?
-
yeah, I thought that too, so I passed hard values, like you suggest (as a test). For reference, the current test script is:
#!/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:
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.
-
Try adding this to the top of the script:
set -- $*
-
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).
-
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
-
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).
-
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.
#!/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):
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.
-
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:
>>> import shlex
>>> shlex.split('"first path" "second path"')
['first path', 'second path']
-
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!
#!/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]))
-
Cool :)
You can make it future proof by joining all args before splitting again:
shlex.split(" ".join(sys.argv[1:]))
edit: remove extra parenthesis
-
Thanks. Just for reference, I think you had an extra ')'. I need to change it to:
shlex.split(" ".join(sys.argv[1:]))
to make it work. Again, thanks!
-
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.
-
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).
-
Thanks. I am headed out of town for a few days. when I get back, I will give it a shot.