mirror of
https://github.com/tanrax/bash-folders.git
synced 2025-10-10 18:25:50 +02:00
Small syntax/indentation and other obvious cleanups before rewriting
This commit is contained in:
@@ -1,118 +1,96 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# --
|
||||
# Description: Script that launches other scripts in different battery states.
|
||||
# --
|
||||
# Cron: * * * * * bash-folders-battery-hook.sh --folder [folder path]
|
||||
# --
|
||||
|
||||
# START
|
||||
set -e
|
||||
|
||||
|
||||
# FUNCTIONS
|
||||
|
||||
usage() {
|
||||
if [ "$*" != "" ] ; then
|
||||
echo "Error: $*"
|
||||
fi
|
||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||
|
||||
cat << EOF
|
||||
Usage: $PROGNAME [OPTION]
|
||||
Script that launches other scripts in different battery states.
|
||||
"discharging" When the battery is in use.
|
||||
"charging" When the battery is charging.
|
||||
"low" When it reaches the low percentage.
|
||||
"high" When it reaches the high percentage.
|
||||
"full" When the battery is full.
|
||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||
|
||||
Options:
|
||||
--folder [path] Folder where the different scripts are located.
|
||||
--low [number] Low battery percentage. Default 15.
|
||||
--high [number] High battery percentage. Default 85.
|
||||
--help Display this usage message and exit
|
||||
Launches other scripts in different battery states
|
||||
|
||||
OPTIONS:
|
||||
--help Display this usage message and exit
|
||||
--folder PATH PATH where the different scripts are located
|
||||
--low INT Low battery percentage (default: 15)
|
||||
--high INT High battery percentage (default: 85)
|
||||
|
||||
STATES:
|
||||
discharching When the battery is in use
|
||||
charging When the battery is charging
|
||||
low When the battery reaches the low percentage
|
||||
high When the battery reaches the high percentag
|
||||
full When the battery is full
|
||||
EOF
|
||||
|
||||
exit 1
|
||||
}
|
||||
|
||||
status() {
|
||||
# Possible values: Discharging, Charging and Full
|
||||
cat /sys/class/power_supply/BAT0/status
|
||||
}
|
||||
|
||||
capacity() {
|
||||
# Possible values: 0-100
|
||||
cat /sys/class/power_supply/BAT0/capacity
|
||||
}
|
||||
|
||||
run_discharging() {
|
||||
# Check if discharging script exists
|
||||
if [ ! -f "$PATH_DISCHARGING_SCRIPT" ]; then
|
||||
# If not, create it
|
||||
touch "$PATH_DISCHARGING_SCRIPT"
|
||||
chmod +x "$PATH_DISCHARGING_SCRIPT"
|
||||
if ! [[ -f "$PATH_DISCHARGING_SCRIPT" ]]; then
|
||||
touch "$PATH_DISCHARGING_SCRIPT"
|
||||
chmod +x "$PATH_DISCHARGING_SCRIPT"
|
||||
fi
|
||||
# If status is discharging, run discharging script
|
||||
if [ "$(status)" = "Discharging" ]; then
|
||||
$PATH_DISCHARGING_SCRIPT
|
||||
|
||||
if [[ "$(status)" == "Discharging" ]]; then
|
||||
$PATH_DISCHARGING_SCRIPT
|
||||
fi
|
||||
}
|
||||
|
||||
run_charging() {
|
||||
# Check if charging script exists
|
||||
if [ ! -f "$PATH_CHARGING_SCRIPT" ]; then
|
||||
# If not, create it
|
||||
touch "$PATH_CHARGING_SCRIPT"
|
||||
chmod +x "$PATH_CHARGING_SCRIPT"
|
||||
if ! [[ -f "$PATH_CHARGING_SCRIPT" ]]; then
|
||||
touch "$PATH_CHARGING_SCRIPT"
|
||||
chmod +x "$PATH_CHARGING_SCRIPT"
|
||||
fi
|
||||
# If status is charging, run charging script
|
||||
if [ "$(status)" = "Charging" ]; then
|
||||
$PATH_CHARGING_SCRIPT
|
||||
|
||||
if [[ "$(status)" == "Charging" ]]; then
|
||||
$PATH_CHARGING_SCRIPT
|
||||
fi
|
||||
}
|
||||
|
||||
run_low() {
|
||||
# Check if low script exists
|
||||
if [ ! -f "$PATH_LOW_SCRIPT" ]; then
|
||||
# If not, create it
|
||||
touch "$PATH_LOW_SCRIPT"
|
||||
chmod +x "$PATH_LOW_SCRIPT"
|
||||
if ! [[ -f "$PATH_LOW_SCRIPT" ]]; then
|
||||
touch "$PATH_LOW_SCRIPT"
|
||||
chmod +x "$PATH_LOW_SCRIPT"
|
||||
fi
|
||||
# If status is discharging and battery is low, run low script
|
||||
if [ "$(status)" = "Discharging" ] && [ "$(capacity)" -le "$LOW_BATTERY" ]; then
|
||||
$PATH_LOW_SCRIPT
|
||||
|
||||
if [[ "$(status)" == "Discharging" ]] && [[ "$(capacity)" -le "$LOW_BATTERY" ]]; then
|
||||
$PATH_LOW_SCRIPT
|
||||
fi
|
||||
}
|
||||
|
||||
run_high() {
|
||||
# Check if high script exists
|
||||
if [ ! -f "$PATH_HIGH_SCRIPT" ]; then
|
||||
# If not, create it
|
||||
touch "$PATH_HIGH_SCRIPT"
|
||||
chmod +x "$PATH_HIGH_SCRIPT"
|
||||
if ! [[ -f "$PATH_HIGH_SCRIPT" ]]; then
|
||||
touch "$PATH_HIGH_SCRIPT"
|
||||
chmod +x "$PATH_HIGH_SCRIPT"
|
||||
fi
|
||||
# If status is charging and battery is high, run high script
|
||||
if [ "$(status)" = "Charging" ] && [ "$(capacity)" -ge "$HIGH_BATTERY" ]; then
|
||||
$PATH_HIGH_SCRIPT
|
||||
|
||||
if [[ "$(status)" == "Charging" ]] && [[ "$(capacity)" -ge "$HIGH_BATTERY" ]]; then
|
||||
$PATH_HIGH_SCRIPT
|
||||
fi
|
||||
}
|
||||
|
||||
run_full() {
|
||||
# Check if full script exists
|
||||
if [ ! -f "$PATH_FULL_SCRIPT" ]; then
|
||||
# If not, create it
|
||||
touch "$PATH_FULL_SCRIPT"
|
||||
chmod +x "$PATH_FULL_SCRIPT"
|
||||
if ! [[ -f "$PATH_FULL_SCRIPT" ]]; then
|
||||
touch "$PATH_FULL_SCRIPT"
|
||||
chmod +x "$PATH_FULL_SCRIPT"
|
||||
fi
|
||||
# If status is charging and battery is full, run full script
|
||||
if [ "$(status)" = "Full" ]; then
|
||||
$PATH_FULL_SCRIPT
|
||||
|
||||
if [[ "$(status)" = "Full" ]]; then
|
||||
$PATH_FULL_SCRIPT
|
||||
fi
|
||||
}
|
||||
|
||||
start() {
|
||||
# Run all scripts
|
||||
run_discharging
|
||||
run_charging
|
||||
run_low
|
||||
@@ -120,37 +98,26 @@ start() {
|
||||
run_full
|
||||
}
|
||||
|
||||
# CONTROLE ARGUMENTS
|
||||
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
key="$1"
|
||||
case $key in
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
--low)
|
||||
LOW_BATTERY="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
--high)
|
||||
HIGH_BATTERY="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "${1}" in
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift 2
|
||||
;;
|
||||
--low)
|
||||
LOW_BATTERY="$2"
|
||||
shift 2
|
||||
;;
|
||||
--high)
|
||||
HIGH_BATTERY="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
# VARIABLES
|
||||
PROGNAME=$(basename "$0")
|
||||
LOW_BATTERY=20
|
||||
HIGH_BATTERY=80
|
||||
DISCHARGING_SCRIPT="discharging"
|
||||
@@ -164,9 +131,8 @@ PATH_HIGH_SCRIPT="$FOLDER_ORIGIN/$HIGH_SCRIPT"
|
||||
FULL_SCRIPT="full"
|
||||
PATH_FULL_SCRIPT="$FOLDER_ORIGIN/$FULL_SCRIPT"
|
||||
|
||||
# Check if the required --folder flag is provided
|
||||
if [ -z "$FOLDER_ORIGIN" ]; then
|
||||
echo "Error: The --folder flag is required."
|
||||
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
||||
printf '%s\n' "Error: The --folder flag is required" >&2
|
||||
exit 1
|
||||
else
|
||||
start
|
||||
|
@@ -1,98 +1,67 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# --
|
||||
# Description: Script that watches when new image (PNG or JPEG) are added and transform to WebP format.
|
||||
# --
|
||||
# Requirements: Install webp
|
||||
# Example Debian: $sudo apt install webp
|
||||
# --
|
||||
# Cron: @reboot bash-folders-image-to-webp.sh >/dev/null 2>&1 &
|
||||
# --
|
||||
|
||||
# START
|
||||
set -e
|
||||
|
||||
|
||||
# FUNCTIONS
|
||||
|
||||
usage() {
|
||||
if [ "$*" != "" ] ; then
|
||||
echo "Error: $*"
|
||||
fi
|
||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||
|
||||
cat << EOF
|
||||
Usage: $PROGNAME [OPTION]
|
||||
Watches when new image (PNG or JPEG) are added and transform to WebP format.
|
||||
Options:
|
||||
--folder [path] Folder path where will be monitored.
|
||||
--help Display this usage message and exit
|
||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||
|
||||
Watches when new image (PNG or JPEG) are added and transform to WebP format
|
||||
|
||||
OPTIONS:
|
||||
--help Display this usage message and exit
|
||||
--folder PATH PATH to be monitored
|
||||
EOF
|
||||
|
||||
exit 1
|
||||
}
|
||||
|
||||
start() {
|
||||
# Monitors the selected folder
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
||||
while read -r filename; do
|
||||
# Gets the file extension
|
||||
extension="${filename##*.}"
|
||||
# Checks if the extension is in the extension list
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" = "$extension" ]]; then
|
||||
filename_output="${filename%.*}.webp"
|
||||
# Displays a flat file of information
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
# Converts the image to WebP
|
||||
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
||||
# Remove a flat file of information
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
done
|
||||
done
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||
extension="${filename##*.}"
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" == "$extension" ]]; then
|
||||
filename_output="${filename%.*}.webp"
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
# CONTROLE ARGUMENTS
|
||||
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
key="$1"
|
||||
case $key in
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
--quality)
|
||||
QUALITY="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift 2
|
||||
;;
|
||||
--quality)
|
||||
QUALITY="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# VARIABLES
|
||||
PROGNAME=$(basename "$0")
|
||||
QUALITY="90"
|
||||
MESSAGE_WAITING="converting_please_wait"
|
||||
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
|
||||
|
||||
# CHECKS
|
||||
|
||||
# Check if exists cwebp
|
||||
if ! which cwebp > /dev/null; then
|
||||
echo "Error: You must install the WebP terminal tools."
|
||||
if ! command -v cwebp > /dev/null; then
|
||||
printf '%s\n' "Error: You must install WebP tooling" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the required --folder flag is provided
|
||||
if [ -z "$FOLDER_ORIGIN" ]; then
|
||||
echo "Error: The --folder flag is required."
|
||||
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
||||
printf '%s\n' "Error: The --folder flag is required" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@@ -1,88 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# --
|
||||
# Description: Script that watches when new videos are added to a folder and optimizes them.
|
||||
# --
|
||||
# Requirements: Install inotify-tools and ffmpeg
|
||||
# Example Debian: $sudo apt install ffmpeg
|
||||
# --
|
||||
# Cron: @reboot bash-folders-video-optimizer.sh >/dev/null 2>&1 &
|
||||
# --
|
||||
|
||||
# START
|
||||
set -e
|
||||
|
||||
# VARIABLES
|
||||
PROGNAME=$(basename "$0")
|
||||
FOLDER_ORIGIN="$2"
|
||||
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
|
||||
MESSAGE_WAITING="optimizing_please_wait"
|
||||
|
||||
# FUNCTIONS
|
||||
|
||||
usage() {
|
||||
if [ "$*" != "" ] ; then
|
||||
echo "Error: $*"
|
||||
fi
|
||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||
|
||||
cat << EOF
|
||||
Usage: $PROGNAME [OPTION]
|
||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||
|
||||
Watches when new videos are added to a folder and optimizes them.
|
||||
Options:
|
||||
--folder [path] Folder path where new video will be monitored and optimized
|
||||
--help Display this usage message and exit
|
||||
|
||||
OPTIONS:
|
||||
--help Display this usage message and exit
|
||||
--folder PATH Folder path where new video will be monitored and optimized
|
||||
EOF
|
||||
|
||||
exit 1
|
||||
}
|
||||
|
||||
start() {
|
||||
# Monitors the selected folder
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
||||
while read -r filename; do
|
||||
# Gets the file extension
|
||||
extension="${filename##*.}"
|
||||
# Checks if the extension is in the extension list
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" = "$extension" ]]; then
|
||||
# Check if the file name starts with "optimized"
|
||||
if [[ "$filename" != optimized* ]]; then
|
||||
filename_output="optimized_${filename%.*}.mp4"
|
||||
# Displays a flat file of information
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
# Convert the file to MP4 format using ffmpeg in /tmp/
|
||||
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
|
||||
# When finished move the optimized file
|
||||
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
|
||||
# Remove a flat file of information
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||
extension="${filename##*.}"
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" = "$extension" ]]; then
|
||||
if [[ "$filename" != optimized* ]]; then
|
||||
filename_output="optimized_${filename%.*}.mp4"
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
|
||||
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
# CONTROLE ARGUMENTS
|
||||
isArg=""
|
||||
|
||||
while [ $# -gt 0 ] ; do
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--help)
|
||||
usage
|
||||
;;
|
||||
--folder)
|
||||
isArg="1"
|
||||
if [ $# -eq 2 ]; then
|
||||
start
|
||||
else
|
||||
usage "You need to specify the path of the folder to watch."
|
||||
fi
|
||||
if [ $# -eq 2 ]; then
|
||||
start
|
||||
else
|
||||
usage "You need to specify the path of the folder to watch."
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ -z $isArg ] ; then
|
||||
if [[ -z "${isArg}" ]]; then
|
||||
usage "Not enough arguments"
|
||||
fi
|
||||
|
Reference in New Issue
Block a user