What is the downloader option --restrict-filenames for python youtube-dl - python

Hello all I was wondering what the option is for the python based version of youtube-dl for this argument in terminal --restrict-filenames? What does the options in python does the tuple need to have added to it?
Thanks in advance, Ondeckshooting

Per the documentation, that option does not require an argument. So a command
such as this will suffice:
youtube-dl --restrict-filenames 73VCKpU9ZnA
Here is the option detail:
Restrict filenames to only ASCII characters, and avoid "&" and spaces in
filenames
As far as what ASCII is, this script will reveal:
#!/usr/bin/awk -f
BEGIN {
while (z++ < 0x7e) {
$0 = sprintf("%c", z)
if (/[[:graph:]]/) printf $0
}
}
Result
!"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Related

echo not printing \033 correctly in pipeline started by os.system()

In bash (as started by Python), I want to print this string \033[31m so that I can use a pipe | operator after it, followed by a command to copy that string to the clipboard. This means that in practice, I'm trying to run something like:
os.system('echo \\033[31m | xsel -ib')
...but the xsel -ib part is working fine, so this question is focused specifically on the behavior of echo.
Most of my attempts have been similar to:
echo -e \\033[31m
I have tried it with single quotes, double quotes, no quotes, removing the -e flag, etc. The closest I got was:
echo -n "\\ 033[31m"
which prints this string \ 033[31m
I don't want that space between \ and 0
-n flag is used to not append a new line after the printed string
I use Ubuntu 20.04, and xsel is a selection and clipboard manipulation tool for the X11 Window System (which Ubuntu 20.04 uses).
echo is the wrong tool for the job. It's a shell builtin, and one for which the POSIX sh standard explicitly does not guarantee portable behavior for when escape sequences (such as \033) are present. system() starts /bin/sh instead of bash, so POSIX behavior -- not that of your regular interactive shell -- is expected.
Use subprocess.run() instead of os.system(), and you don't need echo in the first place.
If you want to put an escape sequence into the clipboard (so not \033 but instead the ESC key that this gets converted to by an echo with XSI extensions to POSIX):
# to store \033 as a single escape character, use a regular Python bytestring
subprocess.run(['xsel', '-ib'], input=b'\033[31m')
If you want to put the literal text without being interpreted (so there's an actual backslash and an actual zero), use a raw bytestring instead:
# to store \033 as four separate characters, use a raw string
subprocess.run(['xsel', '-ib'], input=rb'\033[31m')
For a more detailed description of why echo causes problems in this context, see the excellent answer by Stephane to the Unix & Linux Stack Exchange question Why is printf better than echo?.
If you for some reason do want to keep using a shell pipeline, switch to printf instead:
# to store \033 as four separate characters, use %s
subprocess.run(r''' printf '%s\n' '\033[31m' | xsel -ib ''', shell=True)
# to store \033 as a single escape character, use %b
subprocess.run(r''' printf '%b\n' '\033[31m' | xsel -ib ''', shell=True)

How to get and compare Bash Function parameters? [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
this should be a simple answer but I haven't been able to find a simple solution. I'm trying to create a bash script that runs the reindent.py tool scrip but I want to be able to get user input on whether or not he wants to write the file back to disk. Basically I want to type reindent_python -n FILE_PATH or reindent_python FILE_PATH. So far I got the following:
function reindent_python () {
if ["$1" = "n"]; then
./reindent.py -n $2
else
./reindent.py $2
fi
}
Since this is my first shell script I'm not sure how to get whether or not the first parameter is -n and is $1 the n parameter and $2 the FILE_PATH? How would I got about creating this script?
Thank you!
First off, what you display is a function, not a script. Your function could be as simple as:
function reindent_python () {
./reindent.py "$#"
}
given the fact that your Python script appear to take the same arguments as your function. You can simply pass them all to the Python script with the above construct.
As #CharlesDuffy mentions in the comment, the function keyword is not necessary:
reindent_python () {
./reindent.py "$#"
}
That aside, the if part should be
[ "$1" = "-n" ];
with a space around the brackets and a dash before the n.
You're very close, just add the -. the string comparison is simplistic and does no other processing, it doesn't remove the hyphen for you.
function reindent_python () {
if [ "$1" = "-n" ]; then
./reindent.py -n "$2"
else
./reindent.py "$2"
fi
}
or remove it when calling the function:
reindent_python n FILE_PATH

perl snmpwalk to string

Normally I work with Python but I have a project in Perl. So: What is the process for directing the results of an snmpwalk to a string? I would like to search the string to see if it contains a smaller string.
Here is what I have so far:
foreach (#list){
chomp($_);
system("snmpwalk -v 2c -c community-string $_ oid-hidden");
if (index($string, $substring) != -1) {
print "'$string' contains '$substring'\n";
}
}
system function doesn't return the function output, use qx// or backticks, so your snmpwalk call line will look like this:
my $output = qx/snmpwalk -v 2c -c community-string $_ oid-hidden/;
And then you do with the output variable what you need, for more info I'd refer you to http://perldoc.perl.org/perlop.html#Quote-Like-Operators
However in more general terms I'd follow the advice in #ThisSuitIsBlackNot's comment...

Taking Linux Command as Raw String in Python

)I have confirmed my Linux command works in the terminal, however when I try to call it from python it breaks.
The command is a bit long and has lots of single quotes, so I wrapped it around three double quotes (""") so python can interpret as a raw string (or so I thought). However, when I run it I am getting
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
but I have double and tripple checked my single and double quotes and I have no idea where to go from here.
See the test script below
import os
os.system("""awk -F ' *[[:alnum:]_]*: *' 'BEGIN {h="insert_job;box_name;command;owner;permission;condition;description;std_out_file;std_err_file;alarm_if_fail"; print h; n=split(h,F,/;/)} function pr() {if(F[1] in A) {for(i=1;i<=n;i++)printf "%s%s",A[F[i]],(i<n)?";":RS}} /insert_job/ {pr(); delete A} {for(i in F){if($0~"^"F[i])A[F[i]]=$2}} END {pr()}' ./output/JILS/B7443_dev_jil_20140306104313.csv > /trvapps/autosys/admin/EPS/output/JILS/testout.txt""")
FYI I am using Python 2.4.3, hence why I am using os instead of subprocess.
For your own sanity, try using pipes.quote (or something similar if that doesn't exist in 2.4), ' '.join(words) and '\n'.join(lines) to be able to build up the command rather than using a single complex string if you have to put it in Python. A better solution would be to call a script like #kojiro suggested.
It looks like you are doing some simple CSV munging. How about checking SO for tips on doing that in Python?
In any case, 400+ characters of awk on a single line is enough to make anyone squirm, and doing it in Python, which already has excellent string handling features, is just passing the pain to the next developer. Which will be very angry.
Cramming the awk script into one huge line is awful, and makes it nearly impossible to read and maintain. Don't do that -- if you really must use awk (a dubious claim), write it out on multiple lines, with proper indentation, like you would any other script.
To fix the bug with sh -c interpreting things wrong, use the subprocess module (passing an argument array and not setting shell=True) instead of os.system().
import subprocess
awk_script = r'''
*[[:alnum:]_]*: *
BEGIN {
h="insert_job;box_name;command;owner;permission;condition;description;std_out_file;std_err_file;alarm_if_fail";
print h;
n=split(h,F,/;/)
}
function pr() {
if(F[1] in A) {
for(i=1;i<=n;i++)
printf "%s%s", A[F[i]], (i<n) ? ";" : RS
}
}
/insert_job/ {
pr();
delete A;
}
{
for(i in F) {
if($0~"^"F[i])
A[F[i]]=$2
}
}
END {pr()}
'''
exit_status = subprocess.call(['awk', '-F', awk_script],
stdin=open('./output/JILS/B7443_dev_jil_20140306104313.csv', 'r'),
stdout=open('/trvapps/autosys/admin/EPS/output/JILS/testout.txt', 'w'))
if exit_status != 0:
raise RuntimeException('awk failed')

symbols in command line argument.. python, bash

I am writing a python script on Linux for twitter post using API, Is it possible to pass symbols like "(" ")" etc in clear text without apostrophes....
% ./twitterupdate this is me #works fine
% ./twitterupdate this is bad :(( #this leaves a error on bash.
Is the only alternative is to enclose the text into --> "" ?? like..
% ./twitterupdate "this is bad :((" #this will reduce the ease of use for the script
Is there any workaround?
Yes, quoting the string is the only way. Bash has its syntax and and some characters have special meaning. Btw, using "" is not enough, use apostrophes instead. Some characters will still get interpretted with normal quotation marks:
$ echo "lots of $$"
lots of 15570
$ echo 'lots of $$'
lots of $$
http://www.gnu.org/software/bash/manual/bashref.html#Quoting

Categories