Compare commits
1 Commits
2d77fb0080
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa083b91e0 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
auto-commit.txt
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
# toybox-bash
|
# toybox-bash
|
||||||
Some cool tools to use your Bash scripts!
|
Some cool tools to use your Bash scripts!<br>
|
||||||
|
status: [secondary](https://github.com/synt-xerror/synt-xerror)
|
||||||
|
|
||||||
To use, put toybox.sh on the same directory where you Bash script is, then type:
|
To use, put toybox.sh on the same directory where you Bash script is, then type:
|
||||||
|
|
||||||
|
|||||||
291
toybox.sh
291
toybox.sh
@@ -6,40 +6,62 @@ BLUE='\033[0;34m'
|
|||||||
LBLUE='\033[94m'
|
LBLUE='\033[94m'
|
||||||
NC='\033[0m'
|
NC='\033[0m'
|
||||||
|
|
||||||
player_ok() {
|
cmd_ok() {
|
||||||
"$@" >/dev/null 2>&1
|
"$@" >/dev/null 2>&1 &
|
||||||
return $? # se rodou com sucesso -> ok
|
local pid=$!
|
||||||
|
|
||||||
|
# espera 0.2s para saber se o processo morreu (erro) ou continua (ok)
|
||||||
|
sleep 0.2
|
||||||
|
|
||||||
|
if ! kill -0 "$pid" 2>/dev/null; then
|
||||||
|
return 1 # morreu -> falhou
|
||||||
|
fi
|
||||||
|
|
||||||
|
kill "$pid" 2>/dev/null
|
||||||
|
return 0 # funciona
|
||||||
}
|
}
|
||||||
|
|
||||||
playaudio() {
|
playaudio() {
|
||||||
local file="$1"
|
local file="$1"
|
||||||
|
|
||||||
[[ -f "$file" ]] || return 1
|
[[ -f "$file" ]] || return 1
|
||||||
|
|
||||||
local player_cmds=("aplay" "paplay" "play" "ffplay")
|
# aplay
|
||||||
local cmd
|
if command -v aplay >/dev/null 2>&1; then
|
||||||
|
if cmd_ok aplay "$file"; then
|
||||||
for cmd in "${player_cmds[@]}"; do
|
nohup aplay "$file" >/dev/null 2>&1 &
|
||||||
if command -v "$cmd" >/dev/null 2>&1; then
|
return 0
|
||||||
if [[ "$cmd" == "ffplay" ]]; then
|
|
||||||
ffplay -nodisp -autoexit -loglevel quiet "$file" &
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
# testa se funciona
|
|
||||||
"$cmd" "$file" >/dev/null 2>&1 &
|
|
||||||
local pid=$!
|
|
||||||
sleep 0.2
|
|
||||||
if kill -0 "$pid" 2>/dev/null; then
|
|
||||||
return 0 # já está tocando, não inicia outro
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
done
|
fi
|
||||||
|
|
||||||
return 2
|
# paplay
|
||||||
|
if command -v paplay >/dev/null 2>&1; then
|
||||||
|
if cmd_ok paplay "$file"; then
|
||||||
|
nohup paplay "$file" >/dev/null 2>&1 &
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# play (SoX)
|
||||||
|
if command -v play >/dev/null 2>&1; then
|
||||||
|
if cmd_ok play "$file"; then
|
||||||
|
nohup play "$file" >/dev/null 2>&1 &
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ffplay
|
||||||
|
if command -v ffplay >/dev/null 2>&1; then
|
||||||
|
if cmd_ok ffplay -nodisp -autoexit -loglevel quiet "$file"; then
|
||||||
|
ffplay -nodisp -autoexit -loglevel quiet "$file"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 2 # nenhum player funcional
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
anime() {
|
anime() {
|
||||||
frames=("$@")
|
frames=("$@")
|
||||||
time=${frames[-1]}
|
time=${frames[-1]}
|
||||||
@@ -300,71 +322,198 @@ set_status() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
detect_base() {
|
detect_base() {
|
||||||
|
# Detecta pela pkgbase (se nada mais funcionar)
|
||||||
if command -v apt &> /dev/null; then
|
if command -v apt &> /dev/null; then
|
||||||
BASE="debian"
|
PKG="debian"
|
||||||
elif command -v dnf &> /dev/null; then
|
elif command -v dnf &> /dev/null; then
|
||||||
BASE="fedora"
|
PKG="fedora"
|
||||||
elif command -v pacman &> /dev/null; then
|
elif command -v pacman &> /dev/null; then
|
||||||
BASE="arch"
|
PKG="arch"
|
||||||
elif command -v zypper &> /dev/null; then
|
elif command -v zypper &> /dev/null; then
|
||||||
BASE="opensuse"
|
PKG="suse"
|
||||||
elif command -v apk &> /dev/null; then
|
elif command -v apk &> /dev/null; then
|
||||||
BASE="alpine"
|
PKG="alpine"
|
||||||
else
|
else
|
||||||
BASE=""
|
PKG="none"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Leitura do /etc/os-release
|
# 1) Prioriza os-release
|
||||||
if [[ -f /etc/os-release ]]; then
|
if [ -f /etc/os-release ]; then
|
||||||
. /etc/os-release
|
. /etc/os-release
|
||||||
OS_ID="${ID}"
|
|
||||||
OS_LIKE="${ID_LIKE}"
|
|
||||||
fi
|
|
||||||
OS_LIKE_MAIN=$(echo "$OS_LIKE" | awk '{print $1}')
|
|
||||||
|
|
||||||
if [[ -z "$OS_LIKE_MAIN" && -n "$OS_ID" ]]; then
|
if [ -n "$ID_LIKE" ]; then
|
||||||
OS_LIKE_MAIN="$OS_ID"
|
read -r -a like_arr <<< "$ID_LIKE"
|
||||||
|
BASE="${like_arr[0]}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$ID" ]; then
|
||||||
|
BASE="$ID"
|
||||||
|
return
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
LIKE_BASE=""
|
# 2) Fallback para outros arquivos
|
||||||
case "$OS_LIKE_MAIN" in
|
for file in /etc/*release /etc/*_version /etc/*-release; do
|
||||||
debian|ubuntu) LIKE_BASE="debian" ;;
|
[ -f "$file" ] || continue
|
||||||
rhel|fedora) LIKE_BASE="fedora" ;;
|
. "$file"
|
||||||
arch) LIKE_BASE="arch" ;;
|
|
||||||
suse|opensuse) LIKE_BASE="opensuse" ;;
|
|
||||||
alpine) LIKE_BASE="alpine" ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
if [[ -z "$BASE" && -z "$LIKE_BASE" ]]; then
|
if [ -n "$ID_LIKE" ]; then
|
||||||
echo "Erro: não foi possível determinar a base."
|
read -r -a like_arr <<< "$ID_LIKE"
|
||||||
return 1
|
BASE="${like_arr[0]}"
|
||||||
fi
|
return
|
||||||
|
elif [ -n "$ID" ]; then
|
||||||
if [[ -n "$BASE" && -n "$LIKE_BASE" && "$BASE" != "$LIKE_BASE" ]]; then
|
BASE="$ID"
|
||||||
BASE="$LIKE_BASE"
|
return
|
||||||
fi
|
elif [ -n "$DISTRIB_ID" ]; then
|
||||||
|
BASE="$DISTRIB_ID"
|
||||||
if [[ -z "$BASE" && -n "$LIKE_BASE" ]]; then
|
return
|
||||||
BASE="$LIKE_BASE"
|
else
|
||||||
fi
|
BASE="$(head -n1 "$file")"
|
||||||
}
|
return
|
||||||
|
|
||||||
# echo -e "toybox.sh - detect_base: ${RED}NotFound${NC}: Could not detect distribution base."
|
|
||||||
|
|
||||||
detect_distro() {
|
|
||||||
for f in /etc/os-release /usr/lib/os-release /etc/lsb-release; do
|
|
||||||
if [ -r "$f" ]; then
|
|
||||||
. "$f"
|
|
||||||
DISTRO="${NAME:-$DISTRIB_ID}"
|
|
||||||
return 0
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "toybox.sh - detect_distro: NotFound: Could not detect distribution name."
|
# 3) Ajustes baseados no PKG
|
||||||
|
if [[ "$ID" == "ubuntu" && "$PKG" == "debian" ]]; then
|
||||||
|
BASE="debian"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$ID" != "$PKG" ]]; then
|
||||||
|
BASE="unknown"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$ID" == "$PKG" ]]; then
|
||||||
|
BASE="$ID"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
return 127
|
return 127
|
||||||
}
|
}
|
||||||
|
# Result:
|
||||||
|
#detect_base
|
||||||
|
#echo $BASE
|
||||||
|
#> arch
|
||||||
|
|
||||||
|
detect_distro() {
|
||||||
|
# 1) Prioriza os-release
|
||||||
|
if [ -f /etc/os-release ]; then
|
||||||
|
. /etc/os-release
|
||||||
|
DISTRO="${NAME:-$PRETTY_NAME}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 2) Fallback para arquivos específicos
|
||||||
|
shopt -s nullglob
|
||||||
|
for file in /etc/*release /etc/*_version /etc/*-release; do
|
||||||
|
[ -e "$file" ] || continue
|
||||||
|
|
||||||
|
. "$file"
|
||||||
|
|
||||||
|
if [ -n "${NAME:-}" ]; then
|
||||||
|
DISTRO="$NAME"
|
||||||
|
shopt -u nullglob
|
||||||
|
return
|
||||||
|
|
||||||
|
elif [ -n "${DISTRIB_ID:-}" ]; then
|
||||||
|
DISTRO="$DISTRIB_ID"
|
||||||
|
shopt -u nullglob
|
||||||
|
return
|
||||||
|
|
||||||
|
else
|
||||||
|
DISTRO="$(head -n1 "$file")"
|
||||||
|
shopt -u nullglob
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
shopt -u nullglob
|
||||||
|
|
||||||
|
return 127
|
||||||
|
|
||||||
run() {
|
}
|
||||||
"$@" &
|
# Result
|
||||||
(( $(jobs -r | wc -l) >= 3 )) && wait -n
|
# detect_distro
|
||||||
|
# echo "$NAME"
|
||||||
|
# > Arch Linux
|
||||||
|
|
||||||
|
detect_windows() {
|
||||||
|
# 1) Git Bash / MSYS2 / MinGW
|
||||||
|
case "$(uname -s)" in
|
||||||
|
MINGW*|MSYS*)
|
||||||
|
windows_env="Git Bash / MSYS2"
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# 2) Windows environment variable (Git Bash, MSYS, Cygwin)
|
||||||
|
if [ "$is_windows" = false ] && [ "$OS" = "Windows_NT" ]; then
|
||||||
|
windows_env="Windows_NT (Git Bash/Cygwin/MSYS)"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 3) WSL
|
||||||
|
if [ "$is_windows" = false ] && grep -qi microsoft /proc/version 2>/dev/null; then
|
||||||
|
windows_env="Windows Subsystem for Linux"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4) cmd.exe (fallback)
|
||||||
|
if [ "$is_windows" = false ] && command -v cmd.exe >/dev/null 2>&1; then
|
||||||
|
windows_env="Windows (via cmd.exe)"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# Result
|
||||||
|
# if detect_windows; then
|
||||||
|
# echo "Windows detected ($windows_env)"
|
||||||
|
# else
|
||||||
|
# echo "Isn't windows."
|
||||||
|
# fi
|
||||||
|
}
|
||||||
|
|
||||||
|
detect_android() {
|
||||||
|
# 1) build.prop
|
||||||
|
if grep -qi android /system/build.prop 2>/dev/null; then
|
||||||
|
android_env="build.prop"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 2) getprop
|
||||||
|
if command -v getprop >/dev/null 2>&1; then
|
||||||
|
if [ -n "$(getprop ro.build.version.release 2>/dev/null)" ]; then
|
||||||
|
android_env="getprop"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 3) common directories
|
||||||
|
if [ -d /sdcard ] && [ -d /system ]; then
|
||||||
|
android_env="paths típicos"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4) Termux
|
||||||
|
if [[ "$PREFIX" == /data/data/*com.termux* ]]; then
|
||||||
|
android_env="Termux"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 5) kernel
|
||||||
|
if uname -a | grep -qi android; then
|
||||||
|
android_env="kernel string"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# Result
|
||||||
|
# if detect_android; then
|
||||||
|
# echo "Android detected ($android_env)"
|
||||||
|
# else
|
||||||
|
# echo "Isn't Android"
|
||||||
|
# fi
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user