Skip to content

Instantly share code, notes, and snippets.

@egdoc
egdoc / errtrap-inheritance-example-1.sh
Last active November 13, 2019 09:10
Inheritance of ERR traps
#!/bin/bash
# The -e or --errexit shell option causes the shell to exit immediately when
# a command returns a non-zero status (with some exceptions). An ERR trap gets
# executed before the shell exits
set -o errexit
trap 'echo trapped!' ERR
# This command will have a non-zero exit status, since a standard user has no
# permission to enter the /root directory. Because of the error the trap will
@egdoc
egdoc / bash-dynamic-scope.sh
Created February 8, 2019 11:16
Dynamic vs static/lexical scoping: bash vs python
#!/bin/bash
# Bash has "dynamic" scoping:
# Variable lookups occur in the scope where a function is CALLED
animal="dog"
function get_animal() {
echo "I have a ${animal}"
}
function mypet() {
@egdoc
egdoc / 99-custom-nic.rules
Created February 12, 2019 11:33
Udev rule to set ethernet speed to 100Mbs for the matched device: it doesn't work at full speed due to a bug
'ACTION=="add", ATTRS{device}=="0x0250", ATTRS{vendor}=="0x197b", RUN+="/usr/sbin/ethtool -s ens5f5 speed 100 duplex full autoneg on"'
@egdoc
egdoc / disable-plymouth
Created February 16, 2019 11:19
Boot options to disable plymouth
rd.plymouth=0 plymouth.enable=0
@egdoc
egdoc / lockonsuspend.py
Created February 28, 2019 14:05
Use python and dbus logind API to lock the screen on suspension with i3lock
#!/usr/bin/env python3
# Basic example of how to use i3lock to lock the screen when system goes to sleep
import subprocess
from pydbus import SystemBus
from gi.repository import GLib
def onSuspend(value):
# When suspending or hibernating the system, "value" will be True
if value:
@egdoc
egdoc / download_file_requests.py
Last active April 16, 2019 07:48
Python HTTP requests
#!/usr/bin/env python3
import requests
# steam must be True, otherwise the content will be downloaded all at once
with requests.get("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.7.tar.xz", stream=True) as response:
with open("latest-kernel.tar.xz", "wb") as tarball:
# iter_content method iterates over response content
# the argument is chunk_size
for chunk in response.iter_content(16384):
tarball.write(chunk)
#!/bin/bash
# The command specified in a RETURN trap is executed each time
# a shell function or a script executed with the . or source builtins
# finishes executing. By default, the RETURN trap is not inherited:
sayhi() {
echo "Hi!"
}
main() {
@egdoc
egdoc / konami.js
Created April 9, 2022 13:47
Example of Konami code implementation in Javascript
document.addEventListener('DOMContentLoaded', function() {
'use strict';
const konami_code = [ 'ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
let konami_position = 0;
document.addEventListener('keydown', function (event) {
if (konami_code[konami_position] == event.key) {
konami_position += 1;
if (konami_position == konami_code.length) {
@egdoc
egdoc / molecule.yml
Created November 14, 2022 10:47
Use Docker to test systemd services with molecule
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: geerlingguy/docker-fedora36-ansible:latest
command: /lib/systemd/systemd
volumes:
@egdoc
egdoc / policies.json
Last active December 8, 2023 06:43
firefox-policy
{
"policies": {
"DisableSetDesktopBackground": true,
"DisableTelemetry": true,
"EnableTrackingProtection": {
"Value": true
},
"NoDefaultBookmarks": true,
"OfferToSaveLogins": false,
"Extensions": {