~iortega/termscripts

2b4ef6c2a3f2fc5dc15082658b6b2f7c42147502 — iortega 1 year, 10 months ago
Termscripts on its own repo
A  => bench.sh +20 -0
@@ 1,20 @@
#!/usr/bin/env sh

case $1 in
    dash) strace -cf dash -c 'for i in $(seq 1 1000); do dash -c "$(cat \
        ~/termscripts/cosas.sh)"; done' ;;
    mksh) strace -cf dash -c 'set -o posix; for i in $(seq 1 1000); do dash -c "$(cat \
        ~/termscripts/cosas.sh)"; done' ;;
    ksh) strace -cf ksh -c 'for i in $(seq 1 1000); do ksh -c "$(cat \
        ~/termscripts/cosas.sh)"; done' ;;
    bash) strace -cf bash -c 'for i in $(seq 1 1000); do bash -c "$(cat \
        ~/termscripts/cosas.sh)"; done' ;;
    ash) strace -cf busybox ash -c 'for i in $(seq 1 1000); do busybox ash -c \
        "$(cat ~/termscripts/cosas.sh)"; done' ;;
    sh) strace -cf busybox sh -c 'for i in $(seq 1 1000); do busybox sh -c \
        "$(cat ~/termscripts/cosas.sh)"; done' ;;
    zsh) strace -cf zsh -c 'for i in $(seq 1 1000); do zsh -c \
        "$(cat ~/termscripts/cosas.sh)"; done' ;;
    fish) strace -cf fish -c 'for i in (seq 1 1000); do fish -c "(cat \
        ~/termscripts/cosas.sh)"; done' ;;
esac

A  => brightness-software.sh +19 -0
@@ 1,19 @@
#!/usr/bin/env sh

[ -z "$1" ] && exit 1

if [ "$1" = "-dec" ]; then
    operator="-"
elif [ "$1" = "-inc" ]; then
    operator="+"
fi

[ -z "$2" ] && exit 1

[ "$1" != "-set" ] && result="$(printf "%b\n" "$BRIGHTNESS $operator $2" | bc -l)" \
    || result="$2"

[ "$(printf "%b\n" "$result > 1.0" | bc -l)" -eq 1 ] && result="1.0"
[ "$(printf "%b\n" "$result < 0.0" | bc -l)" -eq 1 ] && result="0.0"

xrandr --output LVDS1 --brightness "$result" && export BRIGHTNESS="$result"

A  => channel-videos-after.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/projects/wlvfs/scripts/channel-videos-after.sh
\ No newline at end of file

A  => checkbashismsfolder.sh +3 -0
@@ 1,3 @@
#!/usr/bin/env sh

[ "$#" -eq 1 ] && gawk '/^#!.*( |[/])sh/{printf "%s\0", FILENAME} {nextfile}' "$(readlink -f "$1")/*" | xargs -0 checkbashisms

A  => colon2newline.sh +28 -0
@@ 1,28 @@
#!/usr/bin/env sh

usage() {
    printf "%b\n" "$0 [-r] <Colon separated list>" 1>&2
    printf "%b\n" "[-r]  Adds /.* at the end of each entry as if they were folders" 1>&2
    exit 1
}

# To get options provided to the command
while getopts ":r" option; do
    case "${option}" in
        r)
            recursive="1"
            ;;
        * | h)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

[ -z "$1" ] && exit 1

if [ "$recursive" = "1" ]; then
    printf "%b\n" "$1/.*" | sed 's/:/\/.*\n/g'
else
    printf "%b\n" "$1" | tr ':' '\n'
fi

A  => colon2regex.sh +28 -0
@@ 1,28 @@
#!/usr/bin/env sh

usage() {
    printf "%b\n" "$0 [-r] <Colon separated list>" 1>&2
    printf "%b\n" "[-r]  Adds /.* at the end of each entry as if they were folders" 1>&2
    exit 1
}

# To get options provided to the command
while getopts ":r" option; do
    case "${option}" in
        r)
            recursive="1"
            ;;
        * | h)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

[ -z "$1" ] && exit 1

if [ "$recursive" = "1" ]; then
    printf "%b\n" "\($1/.*\)" | sed 's/:/\/.*\\|/g'
else
    printf "%b\n" "\($1\)" | sed 's/:/\\|/g'
fi

A  => cosas.sh +2 -0
@@ 1,2 @@
printf "%b\n" off
a=$( printf "%b\n" '10.03423/0.003123' | bc -l) ; printf "%b\n" $a | tr '6' '3' | tr '3' 'ñ' | sed 's/4/ka/g'; [ $(shuf -i 1-10 -n 1) -lt 3 ] || printf "%b\n" hi

A  => cpu-freq-hook.sh +18 -0
@@ 1,18 @@
#!/usr/bin/env sh

iter="1"
while [ $iter -eq "1" ];
do
    freq=$(top -bn 2 -d 5 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}')
    iter=$(echo "$freq > 5" | bc -l)
    if [ $iter -eq 0 ]; then
        sleep 5m
        freq=$(top -bn 2 -d 5 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}')
        iter=$(echo "$freq > 5" | bc -l)
    else
        sleep 2m
    fi
done

printf "%b\n" "done"
sleep 10m

A  => dir-copy.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/scripts/dir-copy.sh
\ No newline at end of file

A  => dir-move.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/scripts/dir-move.sh
\ No newline at end of file

A  => download-video.sh +15 -0
@@ 1,15 @@
#!/usr/bin/env sh

[ -z "$1" ] && exit 1

# Get the filename
filename="$(dash $TSCRIPTS/get-youtube-filename.sh "$1")"

if [ ! -f "$filename" ]; then
    # Download the file
    # printf "%b\n" "youtube-dl --merge-output-format mkv $1"
    youtube-dl --merge-output-format mkv "$1"
fi


printf "%b\n" "$filename"

A  => eval.sh +3 -0
@@ 1,3 @@
#!/usr/bin/env sh

dash -c "$1"

A  => file-copy.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/scripts/file-copy.sh
\ No newline at end of file

A  => file-move.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/scripts/file-move.sh
\ No newline at end of file

A  => get-xprop-from-pid.sh +18 -0
@@ 1,18 @@
#!/usr/bin/env sh

findpid=$1

known_windows=$(xwininfo -root -children|sed -e 's/^ *//'|grep -E "^0x"|awk '{ print $1 }')

for id in ${known_windows}
do
    xp=$(xprop -id $id _NET_WM_PID)
    if test $? -eq 0; then
        pid=$(xprop -id $id _NET_WM_PID|cut -d'=' -f2|tr -d ' ')

        if test "x${pid}" = x${findpid}
        then
            echo "Windows Id: $id"
        fi
    fi
done

A  => get-youtube-filename.sh +3 -0
@@ 1,3 @@
#!/usr/bin/env sh

youtube-dl --merge-output-format mkv --get-filename "$1"

A  => handler.sh +95 -0
@@ 1,95 @@
#!/bin/sh
# Default acpi script that takes an entry for all actions

# NOTE: This is a 2.6-centric script.  If you use 2.4.x, you'll have to
#       modify it to not use /sys

minspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
maxspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
setspeed="/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"

set $*

PID=$(pgrep dbus-launch)
export USER=$(ps -o user --no-headers $PID)
USERHOME=$(getent passwd $USER | cut -d: -f6)
export XAUTHORITY="$USERHOME/.Xauthority"
for x in /tmp/.X11-unix/*; do
    displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
    if [ x"$XAUTHORITY" != x"" ]; then
        export DISPLAY=":$displaynum"
    fi
done

case "$1" in
    button/power)
        #echo "PowerButton pressed!">/dev/tty5
        case "$2" in
            PBTN|PWRF)
		    logger "PowerButton pressed: $2, shutting down..."
		    shutdown -P now
		    ;;
            *)      logger "ACPI action undefined: $2" ;;
        esac
        ;;
    button/sleep)
        case "$2" in
            SBTN|SLPB)
		    # suspend-to-ram
		    logger "Sleep Button pressed: $2, suspending..."
		    # zzz
		    ;;
            *)      logger "ACPI action undefined: $2" ;;
        esac
        ;;
    ac_adapter)
        case "$2" in
            AC|ACAD|ADP0)
                case "$4" in
                    00000000)
                        echo -n $minspeed >$setspeed
                        #/etc/laptop-mode/laptop-mode start
                    ;;
                    00000001)
                        echo -n $maxspeed >$setspeed
                        #/etc/laptop-mode/laptop-mode stop
                    ;;
                esac
                ;;
            *)  logger "ACPI action undefined: $2" ;;
        esac
        ;;
    battery)
        case "$2" in
            BAT0)
                case "$4" in
                    00000000)   #echo "offline" >/dev/tty5
                    ;;
                    00000001)   #echo "online"  >/dev/tty5
                    ;;
                esac
                ;;
            CPU0)
                ;;
            *)  logger "ACPI action undefined: $2" ;;
        esac
        ;;
    button/lid)
	case "$3" in
		close)
			# suspend-to-ram
			logger "LID closed, suspending..."
			DISPLAY=:0 xset dpms force off
			# zzz
			;;
		open)	
			logger "LID opened"
			DISPLAY=:0 xset dpms force on
			;;
		*) logger "ACPI action undefined (LID): $2";;
	esac
	;;
    *)
        logger "ACPI group/action undefined: $1 / $2"
        ;;
esac

A  => helloda +1 -0
@@ 1,1 @@
4-wwWbff6Pc

A  => kernel-update.bash +35 -0
@@ 1,35 @@
#!/usr/bin/env bash

update_kernel () {
    command -v update_kernel &&

    emerge -1v --update --deep --with-bdeps=y --newuse \
        sys-kernel/gentoo-sources &&

    next_number="$(($(eselect kernel list | wc -l) - 1))" &&

    echo "New kernel: $(eselect kernel list | tail -n 1)" &&

    eselect kernel set $next_number &&

    cp "$old_config" /usr/src/linux/.config &&

    cd /usr/src/linux &&

    make syncconfig &&
    make modules_prepare &&
    emerge -v @module-rebuild &&
    make -j2 &&
    make modules_install &&
    make install &&

    grub-mkconfig -o /grub/libreboot_grub.cfg
}

old_config="$KERNEL_CONFIGS/kernel-config-$(uname -r)" &&

cp /usr/src/linux/.config "$old_config" &&

export old_config
export -f update_kernel
su -c update_kernel

A  => loop.sh +43 -0
@@ 1,43 @@
#!/usr/bin/env sh

loops="0"
period="5"

while [ -n "$1" ]; do # while loop starts

	case "$1" in

	-l) 
        loops="$2"
        shift
        ;; 

	-p) 
        period="$2"
        shift
        ;;

	--)
		shift # The double dash which separates options from parameters

		break
		;; # Exit the loop using break command

	*) 
        break
        ;;

	esac

	shift

done

[ "$loops" -eq "0" ] && infinite="true"

while [ $loops -gt 0 -o "$infinite" = "true" ]
do
    dash -c "$@"
    [ "$loops" -gt 0 ] && loops=$(( $loops - 1 ))
    sleep "$period"
done

A  => no-response-timeout.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/projects/wlvfs/scripts/no-response-timeout.sh
\ No newline at end of file

A  => play-videos-from-file.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/projects/wlvfs/scripts/play-videos-from-file.sh
\ No newline at end of file

A  => posix-check.sh +26 -0
@@ 1,26 @@
#!/usr/bin/env sh

output="/tmp/posix-check-$(date +%s)"
checkbashisms -f $SCRIPTS/*.sh $TSCRIPTS/*.sh > $output 2>&1 

errorline () {
    [ -n "$(printf "%b\n" "$1" | grep '(unsafe printf "%b\n" with backslash)')" ] && printf "%b\n" "1" && \
        return
    [ -n "$(printf "%b\n" "$1" | grep '($"foo" should be eval_gettext "foo")')" ] && \
        printf "%b\n" "1" && return
    [ -n "$(printf "%b\n" "$1" | grep '(\[^\] should be \[!\])')" ] && printf "%b\n" "1" && return
    [ -n "$(printf "%b\n" "$1" | grep -E "bashism in ($SCRIPTS|$TSCRIPTS)/.*\\.sh")" ] && printf "%b\n" "2" && return
    printf "%b\n" "0"
}

bashism="0"
while read -r line
do
    # printf "%b\n" "$(errorline "$line")"
    case "$(errorline "$line")" in
        0) ;;
        1) bashism="0" ;;
        2) bashism="1" ;;
    esac
    [ "$bashism" = "1" ] && printf "%b\n" "$line"
done < $output

A  => posixfy.sh +30 -0
@@ 1,30 @@
#!/bin/sh

[ -z $1 ] && exit 1

if [ "$#" -gt 1 ]; then
    for i in "$@"
    do
        # echo "$i"
        dash "$0" "$i"
    done
else
    if [ -d "$1" ]; then
        dash "$0" "$1"/*
    elif [ -f "$1" ]; then
        echo "$1"
        first="$(sed -E 's|^#!/bin/sh$|#!/usr/bin/env sh|g' "$1")"
        second="$(echo "$first" | sed 's|mksh|dash|g')"
        third="$(echo "$second" | sed 's|echo|printf "%b\\n"|g')"
        echo "-----------------------------------------"
        new="/tmp/posixfy$(date +%s)"
        echo "$third" > "$new"
        difference="$(diff -u "$1" "$new")"
        [ -z "$difference" ] && exit 0 || echo "$difference"
        dash $TSCRIPTS/yes-or-no.sh "Confirm?" && echo "$third" > "$1"
        echo "-----------------------------------------"
    else
        echo "Unexpected error" 1>&2
        exit 1
    fi
fi

A  => refresh-cpu-usage.sh +5 -0
@@ 1,5 @@
#!/usr/bin/env sh

ret="$(top -bn 2 -d 5 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}')"

printf "%b\n" "$ret" > /tmp/cpu-freq

A  => run-on-terminal.sh +12 -0
@@ 1,12 @@
#!/usr/bin/env sh

# [ -z "${TERMINAL##*termite*}" ] && $TERMINAL -e "$EDITOR \"$file\"" \
#             || $TERMINAL -e $EDITOR "$file"
[ -z $TERMINAL ] && exit 1

pars="dash -c \"$@\""
case $TERMINAL in
    *termite*) $TERMINAL -e "$pars" ;;
    *st*) dash -c "$TERMINAL -e $pars" ;;
    *) dash -c "$TERMINAL -e $pars" ;;
esac

A  => script-edit-fzf.sh +6 -0
@@ 1,6 @@
#!/usr/bin/env sh

folder=$(dirname "$SCRIPTS")
file="$(find -L "$SCRIPTS" "$TSCRIPTS" -regex ".*\.\(ba\)?sh\$" \
	-type f | sed "s|^$folder/||" | fzf -i)"
[ -z "$file" ] || $EDITOR "$folder/$file"

A  => script-run-fzf.sh +18 -0
@@ 1,18 @@
#!/usr/bin/env sh

folder=$(dirname "$SCRIPTS")
files=$(find -L "$SCRIPTS/" "$TSCRIPTS/" -regex ".*\.\(ba\)?sh\$" -type f |
	sed "s|^$folder/||")

# printf "%b\n" "${files}"

file="$(printf "%b\n" "${files}" | fzf -i)"
# printf "%b\n" "${file}"

[ -n "$file" ] && {
    if echo "$file" | grep -q "\.bash\$"; then
        bash "$folder/$file"
    else
        dash "$folder/$file"
    fi
}

A  => st-copyout +14 -0
@@ 1,14 @@
#!/usr/bin/env sh
# Using external pipe with st, give a dmenu prompt of recent commands,
# allowing the user to copy the output of one.
# xclip required for this script.
# By Jaywalker and Luke
tmpfile=$(mktemp /tmp/st-cmd-output.XXXXXX)
trap 'rm "$tmpfile"' 0 1 15
sed -n "w $tmpfile"
ps1="$(grep "\S" "$tmpfile" | tail -n 1 | cut -d' ' -f1)"
chosen="$(grep -F "$ps1" "$tmpfile" | sed '$ d' | tac | \
    dmenu -p "Copy which command's output?" -i -l 10 | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
eps1="$(printf "%b\n" "$ps1" | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
awk "/^$chosen$/{p=1;print;next} p&&/^$eps1/{p=0};p" "$tmpfile" | sed '2q;d' | \
    xclip -i

A  => tar.sh +10 -0
@@ 1,10 @@
#!/usr/bin/env sh

if [ $# -eq 1 ]; then
    name="$1"
else
    name="$(basename "$(pwd)")"
fi

files="$@"
tar -cvzf $name.tar.gz $files

A  => test.sh +6 -0
@@ 1,6 @@
#!/usr/bin/env sh
stty -echo
printf "Password: "
read PASSWORD
stty echo
printf "\n"

A  => to-seconds.sh +20 -0
@@ 1,20 @@
#!/usr/bin/env sh

[ -z "$1" ] && exit 1

columns="$(printf "%b\n" "${1}" | awk -F ":" '{print NF-1}')"

first="$(printf "%b\n" "$1" | cut -d ":" -f 1)"
second="$(printf "%b\n" "$1" | cut -d ":" -f 2)"
third="$(printf "%b\n" "$1" | cut -d ":" -f 3)"

case $columns in
    1)  seconds=$(( $first*60 + $second ))
        ;;
    2)  seconds=$(( $first*60*60 + $second*60 + $third ))
        ;;
    *) exit 1
        ;;
esac

printf "%b\n" "$seconds"

A  => tube.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/scripts/tube.sh
\ No newline at end of file

A  => untar.sh +7 -0
@@ 1,7 @@
#!/usr/bin/env sh

for i in `seq 1 $#`
do
    tar -xvf $1
    shift
done

A  => until-no-change.sh +12 -0
@@ 1,12 @@
#!/usr/bin/env sh

current=$(mktemp '/tmp/until-no-change-currentXXXXXXX')
prev=$(mktemp '/tmp/until-no-change-prevXXXXXXX')
sh -c "$1" > "$current"

while [ -n "$(diff "$current" "$prev")" ]; do
    cp "$current" "$prev"
    sh -c "cat '$prev' | $2 > '$current'"
done

cat "${current}"

A  => video-upload-date.sh +1 -0
@@ 1,1 @@
/home/iortega/.local/share/projects/wlvfs/scripts/video-upload-date.sh
\ No newline at end of file

A  => watch-video-select-format.sh +15 -0
@@ 1,15 @@
#!/usr/bin/env sh

select_format() {
    youtube-dl -F "$1" | awk '/format code/{p=1;next}{if(p){print}}' |
        cut -d ' ' -f 1 | tac | dmenu -i
}

watch="$1"

if [ "$(printf "No\nYes" | dmenu -fn "$DMENUFN" -i -p "Select Format?")" = "Yes" ]; then
    format=$(select_format "$watch")
    [ -n "$format" ] && $VIDEOPLAYER_SELECT_YTDLFORMAT$format "$watch"
else
    $VIDEOPLAYER "$watch"
fi

A  => yes-or-no.sh +10 -0
@@ 1,10 @@
#!/usr/bin/env sh

while true; do
    read -p "$* [Y/n]: " yn
    case $yn in
        "") return 0  ;;
        [Yy]*) return 0  ;;  
        [Nn]*) printf "%b\n" "Aborted" ; return  1 ;;
    esac
done

A  => zip.sh +10 -0
@@ 1,10 @@
#!/usr/bin/env sh

if [ $# -eq 1 ]; then
    name="$1"
else
    name="$(basename "$(pwd)")"
fi

files="$@"
zip $name.zip $files