Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Gothmog1065
May 14, 2009
I'm having a dumb.

in KSH, if I do a simple cat <file> | grep <search> | sed <strip some stuff> I get the full display of everything from the file. However, I want to mail this from within the script, so I'm trying to foo=$(cat <file> | grep <search> | sed <strip some stuff>), then do a print ${foo}, I only ever get the last line that I want. Why am I dumb?

Adbot
ADBOT LOVES YOU

necrotic
Aug 2, 2005
I owe my brother big time for this!
Try wrapping the whole thing in quotes: foo="$(cmd)". Also, check out shellcheck. It's a great linter for shell scripts.

Gothmog1065
May 14, 2009

necrotic posted:

Try wrapping the whole thing in quotes: foo="$(cmd)". Also, check out shellcheck. It's a great linter for shell scripts.

So going on what I know from other scripts, the double quote is just preventing the expansion of the control characters, and that's what is causing the problem?

necrotic
Aug 2, 2005
I owe my brother big time for this!

Gothmog1065 posted:

So going on what I know from other scripts, the double quote is just preventing the expansion of the control characters, and that's what is causing the problem?

Yeah, I think the newlines are a problem. I haven't really done anything with KSH but wrapping things in quotes is almost always what you want to prevent issues.

Empress Brosephine
Mar 31, 2012

by Jeffrey of YOSPOS
I'm in the US and had to set up a Google business property just the other day and we have to wait for a postcard in the mail from them with a verification code.

The Fool
Oct 16, 2003


Can you think of a better way to verify a valid business address?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
I think that was another anecdote for the "does anyone send physical mail to verify addresses" discussion upthread, not a complaint?

hbag
Feb 13, 2021

I'm working on a few scripts to check when I've been quoted in a thread, by searching the forums when I ask it to, or on a timed basis (every x minutes, I'm not gonna flood the search, don't worry)

Unfortunately, to do that, I need to let the site know that it's actually me via cURL when I send the PUT request to the search page. That gonna be possible at all?

melon cat
Jan 21, 2010

Nap Ghost
Python beginner question. I've noticed that a lot of Python tutorials (like this one which explains how to make a blog with Python) use terminal to install stuff and run commands. This is easy on a Mac since you can run Terminal easily on that IS. But I have no idea how this is done in Windows 10. And every resource I've looked at always shits on Windows 10 for its broken cmd prompt.

Am I better off learning and using and learning Python on Ubuntu? Because I really don't mind dual booting Windows + Ubuntu. I'm just getting kind of irritated that most tutorials assume you're on a Mac/Linux and I think I've spent way too much time trying to figure out how to get these Terminal commands working on Windows.

melon cat fucked around with this message at 23:13 on Feb 15, 2021

hbag
Feb 13, 2021

melon cat posted:

Python beginner question. I've noticed that a lot of Python tutorials (like this one which explains how to make a blog with Python) use terminal to install stuff and run commands. This is easy on a Mac since you can run Terminal easily on that IS. But I have no idea how this is done in Windows 10. And every resources I've looked at always shits on Windows 10 for its broken cmd prompt.

Am I better off learning and using and learning Python on Ubuntu? Because I really don't mind dual booting Windows + Ubuntu. I'm just getting kind of irritated that most tutorials assume you're on a Mac/Linux and I think I've spent way too much time trying to figure out how to get these Terminal commands working on Windows.

Well, while they are right to point and laugh at Windows for its awful, awful """terminal""", I'll help you out:

You can install a WSL (Windows Subsystem for Linux) terminal, I recommend 'Windows Terminal' from the Microsoft store. It's free. After that, you should be able to run most commands just fine. If it's saying something about a python or pip command not being recognized, make sure they're in your PATH.

It'd also probably help if you told us what commands you're struggling with.

Hed
Mar 31, 2004

Fun Shoe

hbag posted:

I'm working on a few scripts to check when I've been quoted in a thread, by searching the forums when I ask it to, or on a timed basis (every x minutes, I'm not gonna flood the search, don't worry)

Unfortunately, to do that, I need to let the site know that it's actually me via cURL when I send the PUT request to the search page. That gonna be possible at all?

Sure, you should be able to send the Cookie header in your request using the one you get from logging in. See man curl (1) --cookie method for this. You can save it off from a browser or just copy it out.

hbag
Feb 13, 2021

Hed posted:

Sure, you should be able to send the Cookie header in your request using the one you get from logging in. See man curl (1) --cookie method for this. You can save it off from a browser or just copy it out.

Right, right, I was thinking it'd be a cookie thing, but I wasn't certain (or, at least, I didn't know if the cookies the site uses are one-time things that'll expire after a while). I'll give that a try.

melon cat
Jan 21, 2010

Nap Ghost

hbag posted:

Well, while they are right to point and laugh at Windows for its awful, awful """terminal""", I'll help you out:

You can install a WSL (Windows Subsystem for Linux) terminal, I recommend 'Windows Terminal' from the Microsoft store. It's free. After that, you should be able to run most commands just fine. If it's saying something about a python or pip command not being recognized, make sure they're in your PATH.

It'd also probably help if you told us what commands you're struggling with.

This... is a helpful start. No tutorials or resources cut through the BS and told me to get Windows Terminal from the Microsoft Store the way you just did. They always just start straight from Terminal and start banging out commands.

My biggest irritation with self-learning Python (esp. through YouTube videos) is that there seems to be two types of tutorials:

A) "Welcome Python noobies! I'm going to show you how to print Hello World! Over and over and over."

B) Advanced tutorials from webdev blogs that are titled like they're for newbies but jump straight into a bunch of intermediate level material and skip over a lot of things.

Because I thought I had Python installed (downloaded and installed the .exe from python.org) . But when I ran "python -m venv env" in Terminal it said I didn't have it installed. Turns out I had to install python through the Microsoft Store. These are the sort of things that even the most basic tutorials don't bother mentioning.

Another example is this one from Django Central. Their instructions for "Windows Users":

code:
cd Desktop
virtualenv django
cd django
Scripts\activate.bat
just doesn't work for me and I get "Cannot find path". I know how basic this stuff is to actual programmers (and you're probably rolling your eyes at how elementary this all is, to you) but none of the tutorials I'm following do a great job showing you how to actually *get started* or what possible problems newcomers might encounter. It's like watching those home improvement videos where they install a water heater in a sparkling clean empty basement and everything just works and fits together perfectly. Weird analogy but hope you understand what I'm trying to say.

melon cat fucked around with this message at 23:57 on Feb 15, 2021

Zoracle Zed
Jul 10, 2001

melon cat posted:

Python beginner question. I've noticed that a lot of Python tutorials (like this one which explains how to make a blog with Python) use terminal to install stuff and run commands. This is easy on a Mac since you can run Terminal easily on that IS. But I have no idea how this is done in Windows 10. And every resource I've looked at always shits on Windows 10 for its broken cmd prompt.

Am I better off learning and using and learning Python on Ubuntu? Because I really don't mind dual booting Windows + Ubuntu. I'm just getting kind of irritated that most tutorials assume you're on a Mac/Linux and I think I've spent way too much time trying to figure out how to get these Terminal commands working on Windows.

An alternative to using WSL like hbag suggested is a console emulater like cmder so you can stay entirely in Windows-land. Generally if you're installing libraries with something like pip or conda there's not really any difference between python development in Windows or Linux, but if you ever find yourself needing to manually install binary dependencies Windows is gonna suck.

hbag
Feb 13, 2021

...now to figure out which cookie I need. none of these seem to really be standing out, but I might just have a smooth brain.

hbag
Feb 13, 2021

so far, i've tried using the bbuserid and bbpassword cookies (together), but it's giving me a 500 error. gonna try throwing sessionid into the mix, too, see if that solves it

edit: no im a dumbass that was actually the current speed not an error code, i am having different issues

redleader
Aug 18, 2005

Engage according to operational parameters

hbag posted:

so far, i've tried using the bbuserid and bbpassword cookies (together), but it's giving me a 500 error. gonna try throwing sessionid into the mix, too, see if that solves it

edit: no im a dumbass that was actually the current speed not an error code, i am having different issues

the existence of a cookie called bbpassword is giving me hives

hbag
Feb 13, 2021

redleader posted:

the existence of a cookie called bbpassword is giving me hives

yeah.
it's hashed but still

xtal
Jan 9, 2011

by Fluffdaddy
1. Open the developer console on the Network tab and make sure it's capturing
2. Make the search request you're interested in and inspect it
3. Use the "copy as curl" option, make sure that works from the terminal, then remove options from it until it stops working.

hbag
Feb 13, 2021

xtal posted:

1. Open the developer console on the Network tab and make sure it's capturing
2. Make the search request you're interested in and inspect it
3. Use the "copy as curl" option, make sure that works from the terminal, then remove options from it until it stops working.

slight issue there
when i try sending that unaltered POST request, i get an error saying "Query ID invalid or search results have expired"

spiritual bypass
Feb 19, 2008

Grimey Drawer

hbag posted:

slight issue there
when i try sending that unaltered POST request, i get an error saying "Query ID invalid or search results have expired"

Sounds search specific? Maybe something gets generated with each search submission like a CSRF token? The search form could be designed to prevent exactly what you're doing

hbag
Feb 13, 2021

rt4 posted:

Sounds search specific? Maybe something gets generated with each search submission like a CSRF token? The search form could be designed to prevent exactly what you're doing

if thats the case whoever designed the search is a loving NERD

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

hbag posted:

if thats the case whoever designed the search is a loving NERD

Paging Jeffrey of YOSPOS

xtal
Jan 9, 2011

by Fluffdaddy

hbag posted:

slight issue there
when i try sending that unaltered POST request, i get an error saying "Query ID invalid or search results have expired"

I haven't checked but there is probably a request earlier than that that does the actual searching? Then uses a query ID to redirect to a page for showing the results.

hbag
Feb 13, 2021

xtal posted:

I haven't checked but there is probably a request earlier than that that does the actual searching? Then uses a query ID to redirect to a page for showing the results.

nope, doesnt seem to be any earlier requests

smackfu
Jun 7, 2004

Yeah, probably need to scrape the page first, then find the search link which includes the search ID. The forums software isn’t exactly cutting edge API stuff.

(I think the search function is resource limited. A lot of old VBulletin sites would have a timeout between searches.)

hbag
Feb 13, 2021

smackfu posted:

Yeah, probably need to scrape the page first, then find the search link which includes the search ID. The forums software isn’t exactly cutting edge API stuff.

(I think the search function is resource limited. A lot of old VBulletin sites would have a timeout between searches.)

You think I get given a cookie, or something, when I navigate to the search page? That could work, but if there is a cookie, I'm having a hard time finding it. Seems to be the same everywhere.
And, yeah, I know about the resource limiting.

necrotic
Aug 2, 2005
I owe my brother big time for this!
It's probably a CSRF token, which will appear as a hidden input on the form and not in a cookie. If that's the mechanism at play you will need to load the search form and pull out the token before you can programmatically send the search post.

edit: just looked, there's no CSRF token. When you send send the POST query the server responds with a 302 Redirect which includes the search ID.

necrotic fucked around with this message at 20:54 on Feb 16, 2021

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

melon cat posted:

This... is a helpful start. No tutorials or resources cut through the BS and told me to get Windows Terminal from the Microsoft Store the way you just did. They always just start straight from Terminal and start banging out commands.

My biggest irritation with self-learning Python (esp. through YouTube videos) is that there seems to be two types of tutorials:

A) "Welcome Python noobies! I'm going to show you how to print Hello World! Over and over and over."

B) Advanced tutorials from webdev blogs that are titled like they're for newbies but jump straight into a bunch of intermediate level material and skip over a lot of things.

Because I thought I had Python installed (downloaded and installed the .exe from python.org) . But when I ran "python -m venv env" in Terminal it said I didn't have it installed. Turns out I had to install python through the Microsoft Store. These are the sort of things that even the most basic tutorials don't bother mentioning.

Another example is this one from Django Central. Their instructions for "Windows Users":

code:
cd Desktop
virtualenv django
cd django
Scripts\activate.bat
just doesn't work for me and I get "Cannot find path". I know how basic this stuff is to actual programmers (and you're probably rolling your eyes at how elementary this all is, to you) but none of the tutorials I'm following do a great job showing you how to actually *get started* or what possible problems newcomers might encounter. It's like watching those home improvement videos where they install a water heater in a sparkling clean empty basement and everything just works and fits together perfectly. Weird analogy but hope you understand what I'm trying to say.

I think the issue here is that these tutorials assume you're familiar with how the terminal works and don't cover that (tbf, this is somewhat beyond the scope of a python tutorial). You can go the route of installing WSL, but you'll still have to learn the basics of how the linux terminal works (granted, there's more/better info online about how to use the linux terminal than the windows one).

The likely issue with "Terminal it said I didn't have it installed" (although I can't be certain without the actual error text) was that it was installed, but it wasn't on the path. When you type "python ...." Windows will search what's called the path environment variable (which is just a list of directories) for a program named "python". If it can't find the program, you'll get an error like "'python' is not recognized as an internal or external command, operable program or batch file." Sometimes programs add their install locations to the path variable, sometimes they don't, sometimes there's a checkbox in the installer for this. The first install you did probably didn't add the directory to the path variable, but the install you did from the Mircrosoft Store did.

Anyway my point is that you need a tutorial on the terminal itself and your python tutorials probably aren't going to help with that.

Plinkey
Aug 4, 2004

by Fluffdaddy
You can also download pycharm for free if you're more comfortable in an IDE

Macichne Leainig
Jul 26, 2012

by VG

Plinkey posted:

You can also download pycharm for free if you're more comfortable in an IDE

I pretty much use pycharm if only for the super-easy virtualenv management. Works great, never really had a problem with it. But in all fairness, a fully-fledged IDE may be a bit much to digest at first as well.

hbag
Feb 13, 2021

necrotic posted:

It's probably a CSRF token, which will appear as a hidden input on the form and not in a cookie. If that's the mechanism at play you will need to load the search form and pull out the token before you can programmatically send the search post.

edit: just looked, there's no CSRF token. When you send send the POST query the server responds with a 302 Redirect which includes the search ID.

oh, motherfucker, literally all i had to do was include the -L flag. now it works fine.

hbag
Feb 13, 2021

...anybody happen to know how i could loop 'sed' 27 times, preferably entirely via a pipe? so far, every method of looping it ive tried acts as if its only ran it once, since i assume it all runs at the same time instead of one after the other

xtal
Jan 9, 2011

by Fluffdaddy
Not sure I understand what that means, what are you working with now?

hbag
Feb 13, 2021

xtal posted:

Not sure I understand what that means, what are you working with now?

code:
#!/usr/bin/env bash

function runx() {
        for ((n=0;n<$1;n++))
        do ${*:2}
        done
}

curl 'https://forums.somethingawful.com/query.php' -L -H 'Cookie: bbuserid=*my user ID*; bbpassword=*haha no*' --data-raw 'q=quoting%3Ahbag&action=query' | html2text | sed -n -e '52,$p' | runx 27 sed '$d'
But the final 'sed', despite the runx function telling it to run 27 times, only seems to run once, as only the last line is removed (instead of the last 27, like i need)

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

hbag posted:

But the final 'sed', despite the runx function telling it to run 27 times, only seems to run once, as only the last line is removed (instead of the last 27, like i need)

If you just want to remove the last n lines, why not just use head or something?

hbag
Feb 13, 2021

Kilson posted:

If you just want to remove the last n lines, why not just use head or something?

because the system im writing this on is cool and uses a cool version of head where apparently negative numbers arent valid

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

hbag posted:

because the system im writing this on is cool and uses a cool version of head where apparently negative numbers arent valid

So use wc to determine how many lines are in it, subtract n, then use head to grab the first len-n lines

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
code:

# delete the last 10 lines of a file
sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2

Adbot
ADBOT LOVES YOU

xtal
Jan 9, 2011

by Fluffdaddy

leper khan posted:

So use wc to determine how many lines are in it, subtract n, then use head to grab the first len-n lines

This will require saving it to a file and assigning variables instead of doing it in one pipeline. Might be time to bust out a scripting language.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply