Linux.org User-created Helpful Shell Scripts

Hi there,
I wanted to share a script I made that allows the user to navigate a man page, display its table of contents and navigate through the different sections and subsections within the man pages.
Requirements :
bash version >= 4 (due to the use of references)
Usage:
You can run the script with the following command :
bash <(curl -fsSL https://raw.githubusercontent.com/nsainton/man_reader/master/man_reader.sh)
More informations about it on github : https://github.com/nsainton/man_reader
The idea behind this script is to allow the user to read the man pages in a friendlier version and to make the information more easily accessible when reading the pages.
If you have any idea of improvement or any feedback to give, feel free to reach out as I would love to receive some
 


Here's a script to show times in various parts of the world. It's useful in my case for communication with people overseas. The script can be amended to suit any standard time zone ... the time zones are listed here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
One can name the script anything, but here it is called: timezones :)
Code:
#!/bin/sh
format='+%a %b %e %X %z %Y %Z'
echo -n "Local       "; date "$@" "$format"
echo -n "UTC         "; TZ=UTC date "$@" "$format"
echo -n "Sydney      "; TZ=Australia/Melbourne date "$@" "$format"
echo -n "Adelaide    "; TZ=Australia/Adelaide date "$@" "$format"
echo -n "Perth       "; TZ=Australia/Perth date "$@" "$format"
echo -n "Vancouver   "; TZ=America/Vancouver date "$@" "$format"
echo -n "Toronto     "; TZ=America/Toronto date "$@" "$format"
echo -n "New York    "; TZ=America/New_York date "$@" "$format"
echo -n "Chicago     "; TZ=US/Central date "$@" "$format"
echo -n "Los Angeles "; TZ=America/Los_Angeles date "$@" "$format"
echo -n "Hawaii      "; TZ=Pacific/Honolulu date "$@" "$format"
echo -n "London      "; TZ=Europe/London date "$@" "$format"
echo -n "Paris       "; TZ=Europe/Paris date "$@" "$format"
echo -n "Rome        "; TZ=Europe/Rome date "$@" "$format"
echo -n "Shanghai    "; TZ=Asia/Shanghai date "$@" "$format"
echo -n "Malaysia    "; TZ=Asia/Kuala_Lumpur date "$@" "$format"
echo -n "Japan       "; TZ=Japan date "$@" "$format"
echo -n "Auckland    "; TZ=Pacific/Auckland date "$@" "$format"
echo -n "Brazil      "; TZ=America/Sao_Paulo date "$@" "$format"
echo -n "Pitcairn    "; TZ=Pacific/Pitcairn date "$@" "$format"

Output looks like this:
Code:
[tom@min ~]$ timezones
Local       Thu Aug  1 10:20:14 +1000 2024 AEST
UTC         Thu Aug  1 00:20:14 +0000 2024 UTC
Sydney      Thu Aug  1 10:20:14 +1000 2024 AEST
Adelaide    Thu Aug  1 09:50:14 +0930 2024 ACST
Perth       Thu Aug  1 08:20:14 +0800 2024 AWST
Vancouver   Wed Jul 31 17:20:14 -0700 2024 PDT
Toronto     Wed Jul 31 20:20:14 -0400 2024 EDT
New York    Wed Jul 31 20:20:14 -0400 2024 EDT
Chicago     Thu Aug  1 00:20:14 +0000 2024
Los Angeles Wed Jul 31 17:20:14 -0700 2024 PDT
Hawaii      Wed Jul 31 14:20:14 -1000 2024 HST
London      Thu Aug  1 01:20:14 +0100 2024 BST
Paris       Thu Aug  1 02:20:14 +0200 2024 CEST
Rome        Thu Aug  1 02:20:14 +0200 2024 CEST
Shanghai    Thu Aug  1 08:20:14 +0800 2024 CST
Malaysia    Thu Aug  1 08:20:14 +0800 2024 +08
Japan       Thu Aug  1 00:20:14 +0000 2024 Japan
Auckland    Thu Aug  1 12:20:14 +1200 2024 NZST
Brazil      Wed Jul 31 21:20:14 -0300 2024 -03
Pitcairn    Wed Jul 31 16:20:14 -0800 2024 -08
 
Script to find error-frames in videos via ffmpeg decoding done before new encoding.
The output will be immediately written to a txt-file, no further output via terminal, to speed up the analysis.

Output to txt-file :

frame=25415 fps=186 q=48.0 size= 27572kB time=00:16:56.04 bitrate= 222.3kbits/s dup=22 drop=0 speed=7.45x
frame=25492 fps=186 q=47.0 size= 27572kB time=00:16:59.12 bitrate= 221.6kbits/s dup=22 drop=0 speed=7.44x
frame=25571 fps=186 q=49.0 size= 27838kB time=00:17:02.28 bitrate= 223.1kbits/s dup=22 drop=0 speed=7.44x
[mpeg2video @ 0x6512b0f3d8c0] 00 motion_type at 6 32
[mpeg2video @ 0x6512b0f3d8c0] invalid cbp -1 at 0 33
[mpeg2video @ 0x6512b0f3d8c0] invalid cbp -1 at 0 34
[mpeg2video @ 0x6512b0f3d8c0] invalid cbp -1 at 0 35
[mpeg2video @ 0x6512b0f3d8c0] Warning MVs not available
[mpeg2video @ 0x6512b0f3d8c0] concealing 505 DC, 505 AC, 505 MV errors in B frame
./Adams Äpfel.m2ts: corrupt decoded frame in stream 2
[mpegts @ 0x6512b0da6800] PES packet size mismatch
[mpegts @ 0x6512b0da6800] Packet corrupt (stream = 3, dts = 6953438192).
frame=25710 fps=186 q=40.0 size= 27838kB time=00:17:07.84 bitrate= 221.9kbits/s dup=117 drop=0 speed=7.44x
frame=25800 fps=186 q=47.0 size= 28007kB time=00:17:11.44 bitrate= 222.4kbits/s dup=117 drop=0 speed=7.44x
frame=25890 fps=186 q=48.0 size= 28007kB time=00:17:15.04 bitrate= 221.7kbits/s dup=117 drop=0 speed=7.44x
frame=26004 fps=186 q=49.0 size= 28007kB time=00:17:19.60 bitrate= 220.7kbits/s dup=117 drop=0 speed=7.45x
frame= 3114 fps= 49 q=50.0 size= 8104kB time=00:02:04.92 bitrate= 531.4kbits/s speed=1.97x

frame= 3142 fps= 49 q=50.0 size= 8104kB time=00:02:06.05 bitrate= 526.7kbits/s speed=1.97x

frame= 3167 fps= 49 q=50.0 size= 8104kB time=00:02:07.06 bitrate= 522.5kbits/s speed=1.97x

frame= 3192 fps= 49 q=51.0 size= 8104kB time=00:02:08.04 bitrate= 518.5kbits/s speed=1.97x

frame= 3219 fps= 49 q=50.0 size= 8104kB time=00:02:09.13 bitrate= 514.1kbits/s speed=1.97x

frame= 3246 fps= 49 q=50.0 size= 8104kB time=00:02:10.21 bitrate= 509.8kbits/s speed=1.97x

frame= 3272 fps= 49 q=49.0 size= 8784kB time=00:02:11.22 bitrate= 548.3kbits/s speed=1.97x

frame= 3301 fps= 49 q=50.0 size= 8784kB time=00:02:12.41 bitrate= 543.4kbits/s speed=1.98x


Bash:
#!/bin/bash

while IFS= read -r -d $'\0' video
do
    filename=$(basename -- "$video")
    filename="${filename%.*}"
    
    ffmpeg -y -i "$video" -vcodec libx264 -crf 51 -preset ultrafast -acodec copy -f mp4 -movflags frag_keyframe+empty_moov+delay_moov pipe:1 >/dev/null 2>"${filename}.txt" </dev/null
    TxtFileName="${filename}.txt"
    
    
done < <(find -iregex ".*\.\(mp4\|mkv\|m2ts\|mpg\|ts\)" -print0)
#dos2unix -c mac "${TxtFileName}"
#grep -C 5 -i -n 'error\|duplicate\|failure' "${TxtFileName}"
grep -i -n 'error\|duplicate\|failure\|missing\|POCs\|corrupt' "${TxtFileName}"
echo "Ready"
read pause
 
I altered the well-known script for KTorrent. Added popularity column in kt.sh info output, right after the download speed. Popularity is calculated as R / S, where R - ratio, S - seeding time in days. Run ./kt.sh help for usage instruction.


kt.sh
Bash:
#!/usr/bin/env bash
#
# Public domain script by amaurea/amaur on IRC (freenode for example).
#   Modified by trapanator to support download/upload rate setting and
#                          to suspend/resume ktorrent network activity
#
# gary example
#   qdbus org.kde.ktorrent /core startAll


case $1 in
        help)
                echo "kt: A simple console interface for ktorrent.
Usage: In the following \"id\" indicates either a torrent hash or index.
       [] indicates optional arguments.

       kt start [id]: If ktorrent is not running, start it. Otherwise,
                      if id is given, start that torrent, otherwise start
                      all torrents.
       kt quit: Quit ktorrent.
       kt load url: Load the torrent given by url. Note that the torrent must
                    be manually startet afterwards.
       kt ls: Print a list of all torrents, of the format: index hash name.
       kt info [id]: Print more detailed info about the selected (or all)
                     torrent(s).
       kt stop [id]: Stop the torrent given by id, or all if id is missing.
       kt name [id]: Like ls, but names only.
       kt remove id: Remove the torrent given by id (but not the actual files).
       kt clear: Remove all torrents.
       kt files [id]: List information about the files of the selected torrent.
       kt pri [id] [priority]: Give the selected torrent the given priority.
       kt pri [id] [file index] [priority]: Set the priority of the given file.
       kt pri [id] equal: Give all files of the torrent the same priority.
       kt pri [id] first: Download the first files in the torrent first.
       kt stu [n] set upload rate to n.
       kt sdu [n] set download rate to n.
       kt suspend   suspend all torrents.
       kt resume    resume all torrents."
       exit ;;
esac
pid=$(pidof ktorrent)
if [ ! $pid ]; then
    case "$1" in
        start)
            ktorrent --display :0.0 ;;
        *)
            echo "ktorrent is not running!" ;;
    esac
    exit
fi
eval "export $(perl -pne 's/\0/\n/g' /proc/$(pidof ktorrent)/environ | fgrep -a DBUS_SESSION_BUS_ADDRESS)"
loc="org.kde.ktorrent"
cmd="qdbus $loc"
case "$1" in
 stu)
  if [ "$2" ]; then
   qdbus org.kde.ktorrent /settings setMaxUploadRate $2
   qdbus org.kde.ktorrent /settings apply
  else echo "upload rate missing!" ;  fi ;;
 std)
  if [ "$2" ]; then
   qdbus org.kde.ktorrent /settings setMaxDownloadRate $2
   qdbus org.kde.ktorrent /settings apply
  else echo "download rate missing!" ;  fi ;;
 suspend)
  qdbus org.kde.ktorrent /core org.ktorrent.core.setSuspended true ;;
 resume)
  qdbus org.kde.ktorrent /core org.ktorrent.core.setSuspended false ;;
    load)
        res=$($cmd /core loadSilently "$2" 1) ;;
    list|ls)
        torrents=$($cmd /core torrents)
        i=0
        for torrent in $torrents; do
            name=$($cmd /torrent/$torrent name)
            printf "%d %s %s\n" $i $torrent "$name"
            i=$(($i+1))
        done ;;
    info)
        if [ "$2" ]; then
            if (( ${#2} < 4 )); then
                torrents=($($cmd /core torrents))
                torrents=${torrents[$2]}
            else torrents=$2; fi
        else torrents=$($cmd /core torrents); fi
        i=0
        for torrent in $torrents; do
            name=$($cmd /torrent/$torrent name)
            size=$($cmd /torrent/$torrent totalSize)
            dsize=$($cmd /torrent/$torrent bytesToDownload)
            prog=$($cmd /torrent/$torrent bytesDownloaded)
            speed=$($cmd /torrent/$torrent downloadSpeed)
            seed=$($cmd /torrent/$torrent seedersConnected)
            leech=$($cmd /torrent/$torrent leechersConnected)
            priority=$($cmd /torrent/$torrent priority)
            seed_time=$($cmd /torrent/$torrent seedTime)
            ratio=$($cmd /torrent/$torrent shareRatio)
            sl=$(printf "[%d|%d]" $seed $leech)
            pri=$(printf "(%d)" $priority)
            printf "%3.0lf%% of %11d %4.0lf kb/s %s %8s %4s %s \n" $((100*$prog/$dsize)) $dsize $(($speed/1000)) $(echo "scale=5 ; $ratio / ($seed_time / 24)" | bc -l | awk '{printf "%.5f\n", $0}') $sl $pri "$name"
            i=$(($i+1))
        done ;;
    name|stop|start|remove|files)
        if (( ${#2} < 4 )); then
            torrents=($($cmd /core torrents))
            torrent=${torrents[$2]}
        else torrent=$2; fi
        case "$1" in
            name)
                $cmd /torrent/$torrent name ;;
            start)
                if [ "$2" ]; then res=$($cmd /core start $torrent)
                else res=$($cmd /core startAll); fi;;
            stop)
                if [ "$2" ]; then res=$($cmd /core stop $torrent)
                else res=$($cmd /core stopAll); fi;;
            remove)
                # qdbus boolean bug workaround: use dbus-send instead
                res=$(dbus-send --type=method_call --dest=$loc /core org.ktorrent.core.remove string:"$torrent" boolean:false) ;;
            files)
                n=$($cmd /torrent/$torrent numFiles)
                for (( i=0; i < $n; i++ )); do
                    path=$($cmd /torrent/$torrent filePath $i)
                    pct=$($cmd /torrent/$torrent filePercentage $i)
                    size=$($cmd /torrent/$torrent fileSize $i)
                    priority=$($cmd /torrent/$torrent filePriority $i)
                    printf "%d %3.0lf%% of %11d [%d] %s\n" $i $pct $size $priority "$path"
                done ;;
        esac ;;
    pri|priority|prioritize)
        if [ $3 ]; then
            if (( ${#2} < 4 )); then
                torrents=($($cmd /core torrents))
                torrent=${torrents[$2]}
            else torrent=$2; fi
            if [ ! $torrent ]; then exit; fi
            n=$($cmd /torrent/$torrent numFiles)
            if [ $4 ]; then
                res=$($cmd /torrent/$torrent setFilePriority $3 $4)
            else
                case $3 in
                    equal|equalize)
                        for (( i=0; i < $n; i++ )); do
                            res=$($cmd /torrent/$torrent setFilePriority $i 40)
                        done ;;
                    inc|increasing)
                        for (( i=0; i < $n; i++ )); do
                            pri=$(printf "%2.0lf" $(((4*$i/$n+3)*10)))
                            res=$($cmd /torrent/$torrent setFilePriority $i $pri)
                        done ;;
                    dec|decreasing)
                        for (( i=0; i < $n; i++ )); do
                            pri=$(printf "%2.0lf" $(((4*($n-$i-1)/$n+3)*10)))
                            res=$($cmd /torrent/$torrent setFilePriority $i $pri)
                        done ;;
                    first)
                        m=$(($n < 3 ? $n : 3))
                        for (( i=0; i < $m; i++ )); do
                            res=$($cmd /torrent/$torrent setFilePriority $i $(((6-$i)*10)))
                        done
                        for (( i=3; i < $n; i++ )) do
                            res=$($cmd /torrent/$torrent setFilePriority $i 30)
                        done ;;
                    *)
                        res=$($cmd /torrent/$torrent setPriority $3) ;;
                esac
            fi
        else echo Too few arguments!; fi ;;
    clear)
        torrents=$($cmd /core torrents)
        for torrent in $torrents; do
            res=$(dbus-send --type=method_call --dest=$loc /core org.ktorrent.core.remove string:"$torrent" boolean:false)
        done ;;
    quit)
        res=$($cmd /MainApplication quit) ;;
    *)
        echo "Unrecognized command: '$1'" ;;
esac
 

Members online


Latest posts

Top