You are not logged in.

#1 2021-12-03 13:11:06

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,435
Website

github copilot for shell scripts, examples

Copilot suggestions based on func name or #comments, I'am leavin this as generated, but
WARNING: If you plan to use any of this run it through shellcheck and add missing quotes and more.

#!/bin/bash

calc () {
    echo "scale=2; $1" | bc
}     

datediff () {
    d1=$(date -d "$1" +%s)
    d2=$(date -d "$2" +%s)
    echo $(( (d1 - d2) / 86400 ))
} # I like this one

svg_to_pdf () {
    in_file=$1
    out_file=$2
    inkscape --export-pdf=$out_file $in_file
}

tmp () {
    mkdir -p tmp
    cd tmp
}

runUntil () {
    until [ $1 -eq $2 ]; do
        $3
        let "i++"
    done
}

magick () {
    convert -density 300 -quality 100 -resize $1 $2 $3
}

1250toUtf8 ()   {
    iconv -f CP1250 -t UTF-8 $1 > $2
}

setWallpaper () {
    feh --bg-scale $1
}

reloadXResources () {
    xrdb -merge ~/.Xresources
}

crushPng () {
    pngcrush -rem alla -reduce -brute -l 9 -q $1 $2
}

flac2m4a () {
    ffmpeg -i $1 -acodec libfaac -ab 128k -ac 2 -ar 44100 -map_metadata 0 -id3v2_version 3 $2
}

wav2flac () {
    ffmpeg -i $1 -acodec flac -ab 128k -ac 2 -ar 44100 -map_metadata 0 -id3v2_version 3 $2
}

# find all png and jpg files in the current directory
# and echo them
find_images () {
    find . -type f -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.gif"
}

mp32m4a ()  {
    ffmpeg -i $1 -acodec libfaac -ab 128k -ac 2 -ar 44100 -map_metadata 0 -id3v2_version 3 $2
}

svg2ico () {
    inkscape --export-png=$1.png $1
    convert $1.png -resize 16x16 $1.ico
    rm $1.png
}

# convert time in format hh.mm.ss.mmm 25 fps timecode hh.mm.ss.ff is wanted 
convert ()  {
    local hh=$(echo $1 | cut -d '.' -f 1)
    local mm=$(echo $1 | cut -d '.' -f 2)
    local ss=$(echo $1 | cut -d '.' -f 3)
    local ff=$(echo $1 | cut -d '.' -f 4)
    local hh_=$(echo $2 | cut -d '.' -f 1)
    local mm_=$(echo $2 | cut -d '.' -f 2)
    local ss_=$(echo $2 | cut -d '.' -f 3)
    local ff_=$(echo $2 | cut -d '.' -f 4)
    local hh_=$(($hh_ * 3600))
    local mm_=$(($mm_ * 60))
    local ss_=$(($ss_))
    local ff_=$(($ff_))
    local hh=$(($hh * 3600))
    local mm=$(($mm * 60))
    local ss=$(($ss))
    local ff=$(($ff))
    local hh=$(($hh + $hh_))
    local mm=$(($mm + $mm_))
    local ss=$(($ss + $ss_))
    local ff=$(($ff + $ff_))
    if [ $ff -ge 100 ]; then
        ff=$(($ff - 100))
        ss=$(($ss + 1))
    fi
    if [ $ss -ge 60 ]; then
        ss=$(($ss - 60))
        mm=$(($mm + 1))
    fi
    if [ $mm -ge 60 ]; then
        mm=$(($mm - 60))
        hh=$(($hh + 1))
    fi
    echo "$hh.$mm.$ss.$ff"
} # havent tested this yet, but wow

# tell chuck norris jokes
chuck () {
    local joke=$(curl -s http://api.icndb.com/jokes/random)
    local joke=$(echo $joke | jq -r '.value.joke')
    echo $joke
}

weather () {
    local weather=$(curl -s http://wttr.in/$1)
    echo $weather
}

checkIfAptInstalled () {
    local package=$1
    local installed=$(dpkg-query -W -f='${Status}' $package 2>/dev/null | grep -c "ok installed")
    if [ $installed -eq 0 ]; then
        echo "false"
    else
        echo "true"
    fi
}

# check if command is available in system
checkIfCommandAvailable () {
    local command=$1
    local available=$(which $command 2>/dev/null)
    if [ -z "$available" ]; then
        echo "false"
    else
        echo "true"
    fi
}

# echo joke to stdout       
echo "What do you call a dog that does magic tricks?"
echo "A labracadabrador"

# echo cat joke to stdout   
cat <<EOF
What do you call a dog that does magic tricks?  A labracadabrador
EOF

Last edited by brontosaurusrex (2021-12-08 12:51:18)

Offline

#2 2021-12-03 14:43:32

twoion
一期一会
Registered: 2015-08-10
Posts: 3,436

Re: github copilot for shell scripts, examples

The quoting issues are critical and break everything in hilarious ways if any argument passed to those functions contains spaces or worse.

svg2ico '; rm -rf -- /home/*'

I recommend running "shellcheck" on any shellscript after writing it, some of its output is opiniated but it also will catch serious oversights.

Offline

#3 2021-12-05 05:14:40

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,615
Website

Re: github copilot for shell scripts, examples

twoion wrote:

I recommend running "shellcheck" on any shellscript after writing it, some of its output is opiniated but it also will catch serious oversights.

+10
We all make mistakes & typos sometimes and shellcheck will catch many of them.
You can ignore its advice sometimes if you're sure you understand why it's OK to do so.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Online

#4 2021-12-05 10:05:47

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,435
Website

Re: github copilot for shell scripts, examples

I've added a warning.

Offline

Board footer

Powered by FluxBB