Skip to content
Dusan Lesan edited this page Aug 20, 2024 · 4 revisions

Script Explanations

This repository contains various shell scripts, each designed for specific tasks. Below you'll find explanations and usage instructions for some of included scripts.

Table of Contents

  1. utils
  2. timer
  3. maimpick
  4. record
  5. vpn-connect
  6. wifi-connect
  7. wifi-hotspot
  8. myip
  9. nmtui-connect
  10. pinentry-dmenu
  11. totp
  12. encrypt-file
  13. media-control
  14. autostart
  15. clear-cache
  16. display-select
  17. display-toggle

Overview

This utility script provides common functions that can be sourced into other scripts to enhance maintainability and reduce redundancy. Below is an explanation of the purpose and usage of each function included in this script.

Available functions:

  1. prompt
  2. notify
  3. getpass
  4. getfilename
  5. wait

prompt function

The prompt function provides a way to prompt the user for input using either fzf in a terminal or dmenu in a graphical environment. It ensures that the script can ask for user input in different environments (headless or with a graphical display).

Usage

prompt [title] [optional list]

Parameters:

  • $1: The prompt message to display.
  • $2: Optional. The list of entries to be shown for the picking.

Behavior:

If the script is running in a terminal-only environment (no graphical display), it uses fzf.

If a graphical environment is detected, it uses dmenu for the prompt.

The function handles the special case where a terminal prompt is requested even in a graphical environment via the USE_TERM variable.

Custom fzf keybinds:

  • enter: Submit text in case when list is not provided and prompt is used to pick up text instead list selection
  • ctrl + j: This shortcut will submit the entered text even if the list is set. ctrl + j is used because my terminal outputs that when pressing shift + enter which is impossible shortcut for some terminal apps

Example

prompt 'Destination:' $'Clipboard\nFile\nBoth'

notify function

The notify function sends notifications to the user. It uses notify-send in a graphical environment and falls back to a simple printf in a terminal-only environment.

Usage

notify [title] [message]

getpass function

The getpass securely prompts the user for a password or other sensitive input, hiding the input as it is typed.

Usage

getpass [message]

Parameters:

  • $1: The prompt message to display.

Behavior:

In a terminal-only environment, it uses read -s to hide the input while typing.

In a graphical environment, it uses dmenu with password mode patched in.

Example

password=$(getpass "Enter your password: ")

getfilename function

The getfilename generates a unique filename in a specified directory, avoiding name collisions by appending a number if the filename already exists.

Usage

getfilename [desired file name] [path for the file]

Parameters:

  • $1: The desired filename.
  • $2: The directory in which to check for existing filenames.

Behavior:

If a file with the same name already exists, it increments a number at the end of the filename until a unique name is found.

If the filename begins with a dot (hidden file), the function preserves this.

Example

unique_filename=$(getfilename "example.txt" "/path/to/dir/")

wait function

The wait repeatedly attempts to execute a command until it succeeds or a maximum number of attempts is reached. It is useful for waiting for certain conditions to be met, such as when a network connection is established after manipulating it.

Usage

wait [command]

Parameters:

  • $1: The command to execute

Behavior:

The function tries to execute the command up to 20 times, with a 0.5-second delay between attempts.

If the command succeeds, the function exits the loop early.

Example

wait "ping -c 1 8.8.8.8 -W 1 > /dev/null"

Overview

The Timer Script allows users to set a countdown timer or specify an absolute end time. Upon completion, the script will print a message and send a system notification with "critical" urgency.

Usage

timer [time]

Accepted Input Formats

The script accepts the following input formats:

  1. HH:MM:SS format

    Example: 01:30:00

    Sets a countdown timer for 1 hour, 30 minutes, and 0 seconds.

  2. Seconds (Numeric Value)

    Example: 3600

    Sets a countdown timer for the specified number of seconds (e.g., 3600 seconds equals 1 hour).

    Handles up to 86400 seconds (24 hours).

  3. Absolute Timestamp (Unix Epoch Time)

    Example: 1700000000 Sets the timer to end at a specific Unix timestamp. If the timestamp is in the past, the timer will immediately finish.

Overview

This script simplifies the process of taking screenshots by providing an interactive interface for selecting the capture type and destination. It supports multiple monitors, window selection, and delayed captures with a timer. The screenshots can be saved to a default directory, copied to the clipboard, or both. The script uses import command from ImageMagick for capturing the screenshot and selx command for selecting a screen region

Usage

maimpick [arguments]

Accepted arguments

  • -a: Automatically capture the active window and save the screenshot to a file

Overview

This script provides an interface for recording screen content. It allows user to select between recording a specific region, an entire window, or any active monitor connected to the system. The script leverages ffmpeg for capturing and encoding the video and selx command for selecting a screen region

Overview

This script simplifies the process of connecting to a VPN using OpenVPN configuration files stored in a specific directory (~/.local/share/ovpn). It uses user credentials stored with secret-tool and handles the VPN connection securely by managing temporary files and cleaning up after use.

Usage

vpn-connect [-q]

Accepted arguments

  • $1: If anything is provided as an argument, the script will terminate the active VPN connection

Set-up

It is required to have credentials stored like:

secret-tool store --label "VPN <VPN config name>" user "<OpenVPN config file name without extension>"
secret-tool store --label "VPN <VPN config name>" pass "<OpenVPN config file name without extension>"

Overview

This script provides a simple interface for scanning available Wi-Fi networks and connecting to them using the iwctl command-line tool. It offers an interactive prompt to select a network, enter a password if required, and establish a connection. It also supports disconnecting from the current Wi-Fi network.

Usage

wifi-connect [-q]

Accepted arguments

  • -q: If the -q argument is provided, the script will disconnect the current Wi-Fi connection

Overview

This script is used to manage a Wi-Fi hotspot using the hostapd service.

Usage

wifi-hotspot [-q]

Accepted arguments

  • -q: Stops the Wi-Fi hotspot by terminating the hostapd process.

Overview

This script will fetch and display IP-based location information (country, city, IP address). If provided with additional input arguments it will also show the active VPN name and Wi-Fi SSID. The script does not have a mode to show all active networks but instead relies on nmtui-connect to provide it appropriate args

Usage

myip [options]

Accepted arguments

  • -c: Enables colored output for terminal mode.
  • -h: Displays hardcoded information about an active hotspot.
  • -v: Displays information about active VPN connections.
  • -w: Displays information about the currently connected Wi-Fi network.

Overview

This script provides an interactive menu for managing various network interfaces such as VPN, Wi-Fi, and Wi-Fi hotspots with help of vpn-connect, wifi-connect and wifi-hotspot scripts for the functional part and myip for the preview shown in fzf. Is is created as an alternative for the tool of the same name from the NetworkManager suite (so rename if you have nm)

Overview

This script acts as a custom Polkit agent that handles password prompts by requesting user input via dmenu. It is used as an alternative to polkit-gnome or similar agents used to elevate privileges. It depends on cmd-polkit.

Overview

This script is designed work as a GPG program that will interact with a protocol that requests a passphrase, handles command-based communication, and sets custom prompts. It operates in a loop, responding to specific commands for a predetermined number of iterations. Created to make a consistent interface with my other scripts and because of that it uses dmenu with password patch applied to hide keyboard input.

Set-up

It is required to have pinentry-program set in the ~/.local/share/gnupg/gpg-agent.conf like:

pinentry-program /home/dusan/.local/bin/pinentry-dmenu

Overview

Being a alternative to mobile apps like Google Authenticator or Aegis, this script interacts with encrypted TOTP keys stored in a specific directory, decrypts a selected key using GPG, generates a TOTP based on that key, and copies the TOTP to the clipboard. The TOTP is also echoed to the terminal.

Set-up

$XDG_DATA_HOME/secrets/priv: This file should contain the necessary GPG settings, such as gpgKey and email, used for decryption.

gpgKey="123abc"
email="[email protected]"

$XDG_DATA_HOME/totp/: This directory stores the GPG-encrypted TOTP keys. Aegis stores this key as a "secret". The encrypt-file script can be used to encrypt the key

Overview

This script encrypts a specified file using GPG and deletes the original plaintext file if the encryption is successful.

Usage

encrypt-file [file path]

Overview

This script sends media control commands (e.g., Play, Pause, Next, Previous) to running MPRIS-compatible media players via D-Bus. If no media player is available or the command is not supported, it falls back to controlling MPD (Music Player Daemon). Useful if you want to control a YouTube via keyboard or Bluetooth headphones.

Usage

media-control [command]

Accepted arguments

  • $1: Command to be passed on to the media player e.g. Play, Pause, Next, Previous

Overview

The script runs on startup, but during work hours, it opens several apps and browser tabs, activates a hotspot, and configures three monitors instead of the default two.

Overview

This script prunes VSCode workspaces, clears cache directories, deduplicates history files, and cleans up journal logs and package cache.

Overview

This script manages multiple monitors using xrandr, allowing you to arrange displays interactively based on your selection.

Overview

This script toggles the state of connected displays (on/off) based on the input argument. I am using if from dwm to toggle monitors using keyboard shortcuts

Usage

display-toggle [display index]

Example:

display-toggle 0

Accepted arguments

  • $1: The index of the display to toggle, starting from 0.