User Tools

Site Tools


sysadminery:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sysadminery:bash [2025/04/27 15:47] adamsysadminery:bash [2025/12/05 08:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-=== loop over files by type ===+=== repetition is the key to memory === 
 +the key to memory. the key to memory. the key to memory.
  
 +<code bash>
 +if [ 0 ]
 +then
 + echo "true"
 +else
 + echo "false"
 +fi
 +</code>
 +
 +it makes sense in the context of "if this program runs without error". but i get tripped in every other context.
 +
 +=== loop over files by type ===
 +<code bash>
     for i in in *.webm     for i in in *.webm
     do     do
         echo $i         echo $i
     done     done
 +</code>
  
 will echo those files. be aware that you're probably going to want to enclose ''$i'' in quotes, e.g.: will echo those files. be aware that you're probably going to want to enclose ''$i'' in quotes, e.g.:
  
 +<code bash>
     for i in *.webm     for i in *.webm
     do     do
         ffmpeg -i "$i" "$i.ogg"         ffmpeg -i "$i" "$i.ogg"
     done     done
 +</code>
  
 === mass rename === === mass rename ===
Line 18: Line 35:
  
 so let's say you found a sick-awesome album. so you've done a nice ''yt-dlp -x --audio-format mp3 --split-chapters https://www.youtube.com/watch?v=aJIDqYZ7qM0''. now you got all these things that are titled like, ''this soundtrack caused me to ACTUALLY DIE and i am a GHOST now (not clickbait) link to my onlyfans in bio, music for men women children best music great music - 001 photograph [aJIDqYZ7qM0].webm'', and a ''002'', and so on. so let's say you found a sick-awesome album. so you've done a nice ''yt-dlp -x --audio-format mp3 --split-chapters https://www.youtube.com/watch?v=aJIDqYZ7qM0''. now you got all these things that are titled like, ''this soundtrack caused me to ACTUALLY DIE and i am a GHOST now (not clickbait) link to my onlyfans in bio, music for men women children best music great music - 001 photograph [aJIDqYZ7qM0].webm'', and a ''002'', and so on.
 +<code bash>
     ls | rename -d 's/ \[[^]]*\]\././'     ls | rename -d 's/ \[[^]]*\]\././'
 +</code>
 pipe ''ls'' to ''rename''. -d for "not the directory, only rename the file", and then do a sed-style replacement string.  pipe ''ls'' to ''rename''. -d for "not the directory, only rename the file", and then do a sed-style replacement string. 
  
Line 26: Line 43:
  
 you can use named capture groups, but you have to do ''\1'', etc. And then the console will bitch at you that you should have done ''$1'', but ''$1'' doesn't work. you can use named capture groups, but you have to do ''\1'', etc. And then the console will bitch at you that you should have done ''$1'', but ''$1'' doesn't work.
 +
 +=== achieve transcendant enlightenment ===
 +
 +<code bash>
 +    curl arg.rip/beefhaving
 +</code>
 +
 +"wow, that was perfect for me, now all of my problems have been solved forever" - a customer
 +
 +=== what is going on with my variables ===
 +
 +watch this.
 +<code bash>
 +echo "# phase 5: blah blah blah"
 +
 +goodreads=0
 +badreads=0
 +
 +find "ocr" -iname "*.txt" -print0 | while read -d $'\0' file
 +do
 +
 +    n=$(grep -Poh "(?<=N)[0-9]{2}\\.[0-9]+" "$file")
 +    w=$(grep -Poh "(?<=W)[0-9]{2}\\.[0-9]+" "$file")
 +
 +    if [ -n "$n" ] && [ -n "$w" ]; then
 +        goodreads=$(($goodreads+1))
 +        echo "$filename was OCR'd well; $goodreads good so far"
 +    else
 +        badreads=$(($badreads+1))
 +        echo "$filename is not perfect; $badreads bad so far"
 +    fi
 +done
 +
 +echo "$goodreads good, $badreads bad."
 +</code>
 +
 +it resets them to 0 after the loop. This is because bash uses dyanmic scope. https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scope dynamic scope is stupid, the correct implementation is lexical scope.
 +
 +so how do we force bash to operate correctly, with lexical scope? according to the internet, you use `local`. doesn't work.
 +once you do that, the subscope doesn't just work with its own copy, it annihilates yours! there's also declare, doesn't work. Export is for sub processes, but again that doesn't send back up.
sysadminery/bash.1745768833.txt.gz · Last modified: by adam