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
|
#!/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
|
set -e
|
||||||
|
|
||||||
|
|
||||||
# FUNCTIONS
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
if [ "$*" != "" ] ; then
|
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||||
echo "Error: $*"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Usage: $PROGNAME [OPTION]
|
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||||
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.
|
|
||||||
|
|
||||||
Options:
|
Launches other scripts in different battery states
|
||||||
--folder [path] Folder where the different scripts are located.
|
|
||||||
--low [number] Low battery percentage. Default 15.
|
OPTIONS:
|
||||||
--high [number] High battery percentage. Default 85.
|
|
||||||
--help Display this usage message and exit
|
--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
|
EOF
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
status() {
|
status() {
|
||||||
# Possible values: Discharging, Charging and Full
|
|
||||||
cat /sys/class/power_supply/BAT0/status
|
cat /sys/class/power_supply/BAT0/status
|
||||||
}
|
}
|
||||||
|
|
||||||
capacity() {
|
capacity() {
|
||||||
# Possible values: 0-100
|
|
||||||
cat /sys/class/power_supply/BAT0/capacity
|
cat /sys/class/power_supply/BAT0/capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
run_discharging() {
|
run_discharging() {
|
||||||
# Check if discharging script exists
|
if ! [[ -f "$PATH_DISCHARGING_SCRIPT" ]]; then
|
||||||
if [ ! -f "$PATH_DISCHARGING_SCRIPT" ]; then
|
|
||||||
# If not, create it
|
|
||||||
touch "$PATH_DISCHARGING_SCRIPT"
|
touch "$PATH_DISCHARGING_SCRIPT"
|
||||||
chmod +x "$PATH_DISCHARGING_SCRIPT"
|
chmod +x "$PATH_DISCHARGING_SCRIPT"
|
||||||
fi
|
fi
|
||||||
# If status is discharging, run discharging script
|
|
||||||
if [ "$(status)" = "Discharging" ]; then
|
if [[ "$(status)" == "Discharging" ]]; then
|
||||||
$PATH_DISCHARGING_SCRIPT
|
$PATH_DISCHARGING_SCRIPT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
run_charging() {
|
run_charging() {
|
||||||
# Check if charging script exists
|
if ! [[ -f "$PATH_CHARGING_SCRIPT" ]]; then
|
||||||
if [ ! -f "$PATH_CHARGING_SCRIPT" ]; then
|
|
||||||
# If not, create it
|
|
||||||
touch "$PATH_CHARGING_SCRIPT"
|
touch "$PATH_CHARGING_SCRIPT"
|
||||||
chmod +x "$PATH_CHARGING_SCRIPT"
|
chmod +x "$PATH_CHARGING_SCRIPT"
|
||||||
fi
|
fi
|
||||||
# If status is charging, run charging script
|
|
||||||
if [ "$(status)" = "Charging" ]; then
|
if [[ "$(status)" == "Charging" ]]; then
|
||||||
$PATH_CHARGING_SCRIPT
|
$PATH_CHARGING_SCRIPT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
run_low() {
|
run_low() {
|
||||||
# Check if low script exists
|
if ! [[ -f "$PATH_LOW_SCRIPT" ]]; then
|
||||||
if [ ! -f "$PATH_LOW_SCRIPT" ]; then
|
|
||||||
# If not, create it
|
|
||||||
touch "$PATH_LOW_SCRIPT"
|
touch "$PATH_LOW_SCRIPT"
|
||||||
chmod +x "$PATH_LOW_SCRIPT"
|
chmod +x "$PATH_LOW_SCRIPT"
|
||||||
fi
|
fi
|
||||||
# If status is discharging and battery is low, run low script
|
|
||||||
if [ "$(status)" = "Discharging" ] && [ "$(capacity)" -le "$LOW_BATTERY" ]; then
|
if [[ "$(status)" == "Discharging" ]] && [[ "$(capacity)" -le "$LOW_BATTERY" ]]; then
|
||||||
$PATH_LOW_SCRIPT
|
$PATH_LOW_SCRIPT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
run_high() {
|
run_high() {
|
||||||
# Check if high script exists
|
if ! [[ -f "$PATH_HIGH_SCRIPT" ]]; then
|
||||||
if [ ! -f "$PATH_HIGH_SCRIPT" ]; then
|
|
||||||
# If not, create it
|
|
||||||
touch "$PATH_HIGH_SCRIPT"
|
touch "$PATH_HIGH_SCRIPT"
|
||||||
chmod +x "$PATH_HIGH_SCRIPT"
|
chmod +x "$PATH_HIGH_SCRIPT"
|
||||||
fi
|
fi
|
||||||
# If status is charging and battery is high, run high script
|
|
||||||
if [ "$(status)" = "Charging" ] && [ "$(capacity)" -ge "$HIGH_BATTERY" ]; then
|
if [[ "$(status)" == "Charging" ]] && [[ "$(capacity)" -ge "$HIGH_BATTERY" ]]; then
|
||||||
$PATH_HIGH_SCRIPT
|
$PATH_HIGH_SCRIPT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
run_full() {
|
run_full() {
|
||||||
# Check if full script exists
|
if ! [[ -f "$PATH_FULL_SCRIPT" ]]; then
|
||||||
if [ ! -f "$PATH_FULL_SCRIPT" ]; then
|
|
||||||
# If not, create it
|
|
||||||
touch "$PATH_FULL_SCRIPT"
|
touch "$PATH_FULL_SCRIPT"
|
||||||
chmod +x "$PATH_FULL_SCRIPT"
|
chmod +x "$PATH_FULL_SCRIPT"
|
||||||
fi
|
fi
|
||||||
# If status is charging and battery is full, run full script
|
|
||||||
if [ "$(status)" = "Full" ]; then
|
if [[ "$(status)" = "Full" ]]; then
|
||||||
$PATH_FULL_SCRIPT
|
$PATH_FULL_SCRIPT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
# Run all scripts
|
|
||||||
run_discharging
|
run_discharging
|
||||||
run_charging
|
run_charging
|
||||||
run_low
|
run_low
|
||||||
@@ -120,27 +98,19 @@ start() {
|
|||||||
run_full
|
run_full
|
||||||
}
|
}
|
||||||
|
|
||||||
# CONTROLE ARGUMENTS
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "${1}" in
|
||||||
# Parse command line arguments
|
|
||||||
while [[ $# -gt 0 ]]
|
|
||||||
do
|
|
||||||
key="$1"
|
|
||||||
case $key in
|
|
||||||
--folder)
|
--folder)
|
||||||
FOLDER_ORIGIN="$2"
|
FOLDER_ORIGIN="$2"
|
||||||
shift # past argument
|
shift 2
|
||||||
shift # past value
|
|
||||||
;;
|
;;
|
||||||
--low)
|
--low)
|
||||||
LOW_BATTERY="$2"
|
LOW_BATTERY="$2"
|
||||||
shift # past argument
|
shift 2
|
||||||
shift # past value
|
|
||||||
;;
|
;;
|
||||||
--high)
|
--high)
|
||||||
HIGH_BATTERY="$2"
|
HIGH_BATTERY="$2"
|
||||||
shift # past argument
|
shift 2
|
||||||
shift # past value
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
usage "Unknown option: $1"
|
usage "Unknown option: $1"
|
||||||
@@ -148,9 +118,6 @@ do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
# VARIABLES
|
|
||||||
PROGNAME=$(basename "$0")
|
|
||||||
LOW_BATTERY=20
|
LOW_BATTERY=20
|
||||||
HIGH_BATTERY=80
|
HIGH_BATTERY=80
|
||||||
DISCHARGING_SCRIPT="discharging"
|
DISCHARGING_SCRIPT="discharging"
|
||||||
@@ -164,9 +131,8 @@ PATH_HIGH_SCRIPT="$FOLDER_ORIGIN/$HIGH_SCRIPT"
|
|||||||
FULL_SCRIPT="full"
|
FULL_SCRIPT="full"
|
||||||
PATH_FULL_SCRIPT="$FOLDER_ORIGIN/$FULL_SCRIPT"
|
PATH_FULL_SCRIPT="$FOLDER_ORIGIN/$FULL_SCRIPT"
|
||||||
|
|
||||||
# Check if the required --folder flag is provided
|
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
||||||
if [ -z "$FOLDER_ORIGIN" ]; then
|
printf '%s\n' "Error: The --folder flag is required" >&2
|
||||||
echo "Error: The --folder flag is required."
|
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
start
|
start
|
||||||
|
@@ -1,74 +1,48 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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
|
set -e
|
||||||
|
|
||||||
|
|
||||||
# FUNCTIONS
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
if [ "$*" != "" ] ; then
|
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||||
echo "Error: $*"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Usage: $PROGNAME [OPTION]
|
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||||
Watches when new image (PNG or JPEG) are added and transform to WebP format.
|
|
||||||
Options:
|
Watches when new image (PNG or JPEG) are added and transform to WebP format
|
||||||
--folder [path] Folder path where will be monitored.
|
|
||||||
|
OPTIONS:
|
||||||
--help Display this usage message and exit
|
--help Display this usage message and exit
|
||||||
|
--folder PATH PATH to be monitored
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
# Monitors the selected folder
|
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
|
||||||
while read -r filename; do
|
|
||||||
# Gets the file extension
|
|
||||||
extension="${filename##*.}"
|
extension="${filename##*.}"
|
||||||
# Checks if the extension is in the extension list
|
|
||||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||||
if [[ "$ext" = "$extension" ]]; then
|
if [[ "$ext" == "$extension" ]]; then
|
||||||
filename_output="${filename%.*}.webp"
|
filename_output="${filename%.*}.webp"
|
||||||
# Displays a flat file of information
|
|
||||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||||
# Converts the image to WebP
|
|
||||||
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
||||||
# Remove a flat file of information
|
|
||||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# CONTROLE ARGUMENTS
|
|
||||||
|
|
||||||
# Parse command line arguments
|
|
||||||
while [[ $# -gt 0 ]]
|
while [[ $# -gt 0 ]]
|
||||||
do
|
do
|
||||||
key="$1"
|
key="$1"
|
||||||
case $key in
|
case $key in
|
||||||
--folder)
|
--folder)
|
||||||
FOLDER_ORIGIN="$2"
|
FOLDER_ORIGIN="$2"
|
||||||
shift # past argument
|
shift 2
|
||||||
shift # past value
|
|
||||||
;;
|
;;
|
||||||
--quality)
|
--quality)
|
||||||
QUALITY="$2"
|
QUALITY="$2"
|
||||||
shift # past argument
|
shift 2
|
||||||
shift # past value
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
usage "Unknown option: $1"
|
usage "Unknown option: $1"
|
||||||
@@ -76,23 +50,18 @@ do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# VARIABLES
|
|
||||||
PROGNAME=$(basename "$0")
|
|
||||||
QUALITY="90"
|
QUALITY="90"
|
||||||
MESSAGE_WAITING="converting_please_wait"
|
MESSAGE_WAITING="converting_please_wait"
|
||||||
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
|
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
|
||||||
|
|
||||||
# CHECKS
|
if ! command -v cwebp > /dev/null; then
|
||||||
|
printf '%s\n' "Error: You must install WebP tooling" >&2
|
||||||
# Check if exists cwebp
|
|
||||||
if ! which cwebp > /dev/null; then
|
|
||||||
echo "Error: You must install the WebP terminal tools."
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if the required --folder flag is provided
|
# Check if the required --folder flag is provided
|
||||||
if [ -z "$FOLDER_ORIGIN" ]; then
|
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
||||||
echo "Error: The --folder flag is required."
|
printf '%s\n' "Error: The --folder flag is required" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@@ -1,60 +1,37 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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
|
set -e
|
||||||
|
|
||||||
# VARIABLES
|
|
||||||
PROGNAME=$(basename "$0")
|
|
||||||
FOLDER_ORIGIN="$2"
|
FOLDER_ORIGIN="$2"
|
||||||
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
|
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
|
||||||
MESSAGE_WAITING="optimizing_please_wait"
|
MESSAGE_WAITING="optimizing_please_wait"
|
||||||
|
|
||||||
# FUNCTIONS
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
if [ "$*" != "" ] ; then
|
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||||
echo "Error: $*"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Usage: $PROGNAME [OPTION]
|
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||||
|
|
||||||
Watches when new videos are added to a folder and optimizes them.
|
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
|
OPTIONS:
|
||||||
--help Display this usage message and exit
|
--help Display this usage message and exit
|
||||||
|
--folder PATH Folder path where new video will be monitored and optimized
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
# Monitors the selected folder
|
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
|
||||||
while read -r filename; do
|
|
||||||
# Gets the file extension
|
|
||||||
extension="${filename##*.}"
|
extension="${filename##*.}"
|
||||||
# Checks if the extension is in the extension list
|
|
||||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||||
if [[ "$ext" = "$extension" ]]; then
|
if [[ "$ext" = "$extension" ]]; then
|
||||||
# Check if the file name starts with "optimized"
|
|
||||||
if [[ "$filename" != optimized* ]]; then
|
if [[ "$filename" != optimized* ]]; then
|
||||||
filename_output="optimized_${filename%.*}.mp4"
|
filename_output="optimized_${filename%.*}.mp4"
|
||||||
# Displays a flat file of information
|
|
||||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
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"
|
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"
|
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
|
||||||
# Remove a flat file of information
|
|
||||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -62,10 +39,9 @@ start() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# CONTROLE ARGUMENTS
|
|
||||||
isArg=""
|
isArg=""
|
||||||
|
|
||||||
while [ $# -gt 0 ] ; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--help)
|
--help)
|
||||||
usage
|
usage
|
||||||
@@ -83,6 +59,6 @@ while [ $# -gt 0 ] ; do
|
|||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z $isArg ] ; then
|
if [[ -z "${isArg}" ]]; then
|
||||||
usage "Not enough arguments"
|
usage "Not enough arguments"
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user