You are not logged in.

#2826 2016-10-07 20:09:43

Stencon281
Member
Registered: 2016-09-21
Posts: 20

Re: Post your handy self made command line utilities

Trilby wrote:
Sachiko wrote:

This is to make it seem like the script is contemplating something...

You've previously worked for Microsoft?

Do I laugh or cry?

Offline

#2827 2016-10-08 12:15:11

basica
Member
From: Australia
Registered: 2012-10-31
Posts: 211

Re: Post your handy self made command line utilities

After doing a good bunch of LFS PKGBUILDS manually, I decided it was time to automate the process a tiny bit. It's not perfect, but it is "good enough"; for my purposes at least with this python script. Critiques are of course are welcome.

import os, sys
dl_file = sys.argv[1]

# get the filename from the url download
i = dl_file.rfind("/")
temp = dl_file[i+1:]
# get the package name and version, discard the extension
fname = temp.split(".tar.")
# split the package name from the package version
i = fname[0].rfind("-")
pkgname = fname[0][:i]
pkgver = fname[0][i+1:]
# generate a new generic url download from the above variables
url = dl_file.replace(pkgname,"$pkgname").replace(pkgver,"$pkgver")

# create the PKGBUILD with the data generated above

os.makedirs(pkgname)
pkg_file = open(pkgname + '/PKGBUILD','w')
pkg_data = "# Maintainer: something@somewhere.com\n\n"
pkg_data += "pkgname=" + pkgname + "\n"
pkg_data += "pkgver=" + pkgver + "\n"
pkg_data += "pkgrel=1" + "\n"
pkg_data += "pkgdesc='Description goes here'" + "\n"
pkg_data += "arch=('x86_64')" + "\n"
pkg_data += "license=('GPL2')" + "\n"
pkg_data += 'source=("'+ url +'")' + "\n"
pkg_data += "sha512sums=('')" + "\n\n"
pkg_data += 'build(){\n\tcd "$srcdir/$pkgname-$pkgver"\n}' + "\n"
pkg_data += 'package(){\n\tcd "$srcdir/$pkgname-$pkgver"\n\tmake DESTDIR=$pkgdir install\n}'
pkg_file.write(pkg_data)
pkg_file.close()  

Last edited by basica (2016-10-08 12:15:51)

Offline

#2828 2016-10-09 02:16:29

Eschwartz
Member
Registered: 2014-08-08
Posts: 613

Re: Post your handy self made command line utilities

snakeroot wrote:

This is brilliant! How have I lived without this? Since I found the documentation is a little opaque, here's what I did and plan to do.

BACKGROUND

Single user system, with all AUR packages held in their respective build directories and installed via "pacman -U".

STEPS

1. Create new "/home/<user>/packages" directory
2. Move all current AUR packages into "/home/<user>/packages" directory
3. Edit "/etc/makepkg.conf" to add the line

PKGDEST=/home/<user>/packages

4. Add the following new lines to "/home/<user>/.bashrc":

alias aurupdate="repo-add -n -R /home/chris/packages/AUR.db.tar.xz /home/chris/packages/*.pkg.tar.xz"
alias aurinstall="sudo pacman -Syu $(pacman -Sl AUR | grep -v installed | cut -d ' ' -f 2)"

5. source .bashrc and run aurupdate
6. Going forward, after building new AUR packages run aurinstall

Does this make sense? Do you have any suggestions on better ways to do this?

I do something similar...

I have PKGDEST set to /var/cache/pacman/custom in makepkg.conf and added it as a CacheDir in my pacman.conf
I also use the following script to update my custom repo, which allows me to specify the packages to update in repo-add (if specified, it is faster than trying everything), allows me to easily remove something if necessary, and excludes kernel updates (because I don't want to automatically replace my running kernel + modules, see FS#16702):

#!/bin/bash

shopt -s extglob

cd /var/cache/pacman/custom/

act="add"
force="-n"

while [[ "${1}" != "" ]]; do
    case "${1}" in
        -r|--remove)
            act="remove"
            ;;
        -f|--force)
            force=""
            ;;
        -k|--kernel)
            upgrade_kernels=1
            ;;
        *)
            break
            ;;
    esac
    shift
done

if [[ "${1}" != "" ]]; then
    while [[ "${1}" != "" ]]; do
        if [[ "${act}" = "remove" ]]; then
            packages+=("${1}")
        else
            packages+=("${1}-+([a-z0-9.:_+])-[0-9]-@(any|i686|x86_64).pkg.tar.xz")
        fi
        shift
    done
else
    packages=(*.pkg.tar.xz)
fi

for pkg in "${packages[@]}"; do
    if [[ "${pkg}" =~ ^linux-?.* ]]; then
        kernel_pkgs+=("${pkg}")
    else
        regular_pkgs+=("${pkg}")
    fi
done

if [[ "${upgrade_kernels}" != "" ]]; then
    repo-${act} ${force} custom.db.tar.gz ${kernel_pkgs[@]}
else
    repo-${act} -R ${force} custom.db.tar.gz ${regular_pkgs[@]}
fi

My pacman.conf lists the custom repo and includes /etc/pacman.d/nocustom.conf with the main part of my pacman configuration. I have yaourt set to use that instead -- so yaourt will still treat custom packages as foreign packages and get them from the AUR.

Last edited by Eschwartz (2016-10-11 04:17:29)

Offline

#2829 2016-10-09 02:17:06

Eschwartz
Member
Registered: 2014-08-08
Posts: 613

Re: Post your handy self made command line utilities

basica wrote:

After doing a good bunch of LFS PKGBUILDS manually, I decided it was time to automate the process a tiny bit. It's not perfect, but it is "good enough"; for my purposes at least with this python script. Critiques are of course are welcome.

import os, sys
dl_file = sys.argv[1]

# get the filename from the url download
i = dl_file.rfind("/")
temp = dl_file[i+1:]
# get the package name and version, discard the extension
fname = temp.split(".tar.")
# split the package name from the package version
i = fname[0].rfind("-")
pkgname = fname[0][:i]
pkgver = fname[0][i+1:]
# generate a new generic url download from the above variables
url = dl_file.replace(pkgname,"$pkgname").replace(pkgver,"$pkgver")

# create the PKGBUILD with the data generated above

os.makedirs(pkgname)
pkg_file = open(pkgname + '/PKGBUILD','w')
pkg_data = "# Maintainer: something@somewhere.com\n\n"
pkg_data += "pkgname=" + pkgname + "\n"
pkg_data += "pkgver=" + pkgver + "\n"
pkg_data += "pkgrel=1" + "\n"
pkg_data += "pkgdesc='Description goes here'" + "\n"
pkg_data += "arch=('x86_64')" + "\n"
pkg_data += "license=('GPL2')" + "\n"
pkg_data += 'source=("'+ url +'")' + "\n"
pkg_data += "sha512sums=('')" + "\n\n"
pkg_data += 'build(){\n\tcd "$srcdir/$pkgname-$pkgver"\n}' + "\n"
pkg_data += 'package(){\n\tcd "$srcdir/$pkgname-$pkgver"\n\tmake DESTDIR=$pkgdir install\n}'
pkg_file.write(pkg_data)
pkg_file.close()  

Build functions are not mandatory...

So this basically just takes a download url and tries to extract a pkgname-pkgver from it, then assumes the software can be built with make install? Should work okay, except that $pkgdir needs to be quoted just like srcdir... also, you might as well tell sha512sums to SKIP itself since fr auto-generated self-used PKGBUILDs integrity checks aren't really useful (and they certainly aren't supplying security).

Offline

#2830 2016-10-09 03:36:35

basica
Member
From: Australia
Registered: 2012-10-31
Posts: 211

Re: Post your handy self made command line utilities

Eschwartz wrote:

Build functions are not mandatory...

So this basically just takes a download url and tries to extract a pkgname-pkgver from it, then assumes the software can be built with make install? Should work okay, except that $pkgdir needs to be quoted just like srcdir... also, you might as well tell sha512sums to SKIP itself since fr auto-generated self-used PKGBUILDs integrity checks aren't really useful (and they certainly aren't supplying security).

I am not using these PKGBUILDS in this form; most of them need to have additional things added to the build and package functions. It's just to save me time rewriting things from scratch all the time or copying another PKGBUILD. After creating this I found you can create templates for makepkg which might make this redundant, but I'll need to take a look at it to see how it works first. Also, thanks for the note about the pkgdir variable. I'll will update it.

Offline

#2831 2016-10-09 04:19:47

Eschwartz
Member
Registered: 2014-08-08
Posts: 613

Re: Post your handy self made command line utilities

Same difference. smile

Anyway, makepkg-template is really meant for PKGBUILDs that contain verbatim code which sometimes needs to be updated... in multiple PKGBUILDs.
It is like using m4_include() for your PKGBUILD.in and of course you need to manually add the template call then run makepkg-template which I don't think is what you had in mind.


Also, I would use a formatted text block for the PKGBUILD as it is a lot more readable, e.g.:

#!/usr/bin/python

pkgname='somepackage'
pkgver='1.2.3'
url='http://example.com/somepackage-1.2.3.tar.gz'

PKGBUILD='''
# Maintainer: something@somewhere.com

pkgname={PKGNAME}
pkgver={PKGVER}
pkgrel=1
pkgdesc='Description goes here'
arch=('x86_64')
license=('GPL2')
source=("{URL}")
sha512sums=('SKIP')

build(){{
	cd "$srcdir/$pkgname-$pkgver"
}}
package(){{
	cd "$srcdir/$pkgname-$pkgver"
	make DESTDIR="$pkgdir" install
}}
'''.strip()

print(PKGBUILD.format(PKGNAME=pkgname, PKGVER=pkgver, URL=url))

Note that actual brackets in the resulting formatted text must be doubled.
The opening and closing triple-quotes are on their own lines, the resulting leading/trailing newlines get stripped though so it's all good. smile

Last edited by Eschwartz (2016-10-09 04:33:40)

Offline

#2832 2016-10-09 04:32:47

basica
Member
From: Australia
Registered: 2012-10-31
Posts: 211

Re: Post your handy self made command line utilities

Eschwartz wrote:

Same difference. smile

Anyway, makepkg-template is really meant for PKGBUILDs that contain verbatim code which sometimes needs to be updated... in multiple PKGBUILDs.
It is like using m4_include() for your PKGBUILD.in and of course you need to manually add the template call then run makepkg-template which I don't think is what you had in mind.


Also, I would use a formatted text block for the PKGBUILD as it is a lot more readable, e.g.:

...

Note that actual brackets in the resulting formatted text must be doubled.

That is very handy. I've been using python for ages and never knew about that. Cheers smile

Offline

#2833 2016-11-05 14:26:12

parchd
Member
Registered: 2014-03-08
Posts: 303

Re: Post your handy self made command line utilities

After setting up encryption on my system, I was planning on using chkboot (see AUR), but I figured if I want my system to be secure I should understand how it works. To that end I tried writing my own equivalent (which I will probably end up using).
I call it "tamper". Here it is in case anyone fancies a look, or better still wants to give feedback:

#!/bin/bash

TAMPER_DIR="/var/lib/tamper/"
BOOT_LOG="$TAMPER_DIR/boot"
MBR_LOG="$TAMPER_DIR/mbr"

MBR_DEVICE="/dev/vda"

function get_mbr(){
        dd if=$MBR_DEVICE of=/tmp/mbr.bak bs=512 count=1
}

function get_boot(){
        tar -c /boot --exclude /boot/grub/grubenv -f /tmp/boot.bak
}

function check_mbr(){
        if [ -e $MBR_LOG ]; then
                get_mbr &&
                sha512sum -c <(tail -n1 $MBR_LOG)
        else
                echo "'$MBR_LOG' does not exist" >&2
                return 1
        fi
}

function check_boot(){
        if [ -e $BOOT_LOG ]; then
                get_boot &&
                sha512sum -c <(tail -n1 $BOOT_LOG)
        else
                echo "'$BOOT_LOG' does not exist" >&2
                return 1
        fi
}

function check() {
        check_mbr && 
        rm /tmp/mbr.bak || echo "MBR did not validate!" >&2
        check_boot &&
        rm /tmp/boot.bak || echo "/boot did not validate!" >&2
}

function update(){
        mkdir -p $TAMPER_DIR &&
        if ! check_mbr; then
                if ! [ -e /tmp/mbr.bak ]; then
                        get_mbr
                fi
                sha512sum /tmp/mbr.bak >> $MBR_LOG
                mv /tmp/mbr.bak $TAMPER_DIR
                echo "Updated mbr"
        else
                echo "MBR already up to date"
                rm /tmp/mbr.bak
        fi

        if ! check_boot; then
                if ! [ -e /tmp/boot.bak ]; then
                        get_boot
                fi
                sha512sum /tmp/boot.bak >> $BOOT_LOG
                mv /tmp/boot.bak $TAMPER_DIR
                echo "Updated boot"
        else
                echo "/boot already up to date"
                rm /tmp/boot.bak
        fi
}

case $1 in
        "--help" ) ;&
        "-h" )
                echo "Options: --update (-u): update the tamper database"
                ;;
        "--update" ) ;&
        "-u" )
                update &&
                echo "Updated"
                ;;
        "--check" ) ;&
        "-c" )
                check
                ;;
esac

exit $?

Offline

#2834 2016-11-06 16:04:15

blippy
Member
Registered: 2010-11-18
Posts: 25

Re: Post your handy self made command line utilities

I have a script which I call "parf":

#!/usr/bin/env bash

RC=$HOME/.parfrc

function print_help {
cat <<EOF
Prints out the content of the RC file ($RC), with newlines replaced by colons
-c add current directory to RC file
-e edit RC file using default editor
-h this help
-s SEP specifiy separator
EOF
}

SEP=':'

while getopts "cehs:" opt
do
    case $opt in
        c) echo `pwd` >> $RC ;;
        e) $EDITOR $RC ;;
        h) print_help ;;
        s) SEP=$OPTARG ;;
        *) echo "Unrecognised argument: $opt" ; exit 1 ;;
    esac
done


# construct a complete path
function prpath {
    if [ -f $RC ]; then
        local res=
        while read line
        do
            #echo inpu $line
            if [ -d "$line" ]; then
                res="$line$SEP$res"
                #echo $line
                #echo res so far "$res"
            fi
        done < <(tac $RC)
        echo "$res"
    fi
}

prpath

It is a PATH editor. PATHs are stored in ~/.parfc, one per line. If you want to add the current dir to the PATH, type parf -c. If you want to see the path that is created, type parf. If you want to edit your path configuration file, type parf -e. Don't need the path any more? Just parf -e and delete the offending line. You will need to set the EDITOR variable in order to edit the path.

Add parf somewhere bash can see it, and in your .bashrc add the line
PATH=`parf`$PATH

You also need to start a new bash shell for the changes to take effect.

It saves me a lot of hassle.

Offline

#2835 2016-11-06 16:14:59

blippy
Member
Registered: 2010-11-18
Posts: 25

Re: Post your handy self made command line utilities

Something I'm getting into is bosh (browseable output shell), available in AUR.

The idea behind it is that you provide a configuration file specifiying a command to run, and action keys. bosh runs the command. You can select an output line, press an action key, and bosh runs the action for that key, on that line.

The world's your lobster as to what you can get it to do. A canned script is called bops, which prints the running processes. Select the process you want to kill, and type 'k'. Nifty.

I am using it at the moment to list files in a certain directory. I can highlight the file I am interested in, and select "v" to view the file contents.

Limitless possibilities.

Offline

#2836 2016-11-06 18:02:08

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,794
Website

Re: Post your handy self made command line utilities

bosh sounds interesting, but the PKGBUILD for it is ridiculous.  There are countless things wrong, but the troubling issues are the use of "unknown" license when it is very clearly GPL2 and the failure to properly use the configure script to install in /usr.  I'll post a proper PKGBUILD in the comments for that package momentarily.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#2837 2016-11-06 22:07:05

Alad
Wiki Admin
Registered: 2014-05-04
Posts: 1,309

Re: Post your handy self made command line utilities

#2833, #2834: Obligatory reference to shellcheck.net …


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
User:Alad

Offline

#2838 2016-11-06 22:25:49

parchd
Member
Registered: 2014-03-08
Posts: 303

Re: Post your handy self made command line utilities

Alad wrote:

#2833, #2834: Obligatory reference to shellcheck.net …

Was there a specific critique?
I'd already put mine through shellcheck and had no issues detected, but I don't usually write more than one-liners so improvement suggestions on style (or otherwise) are more than welcome.

Offline

#2839 2016-11-23 12:54:45

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,552

Re: Post your handy self made command line utilities

Simple script i've bound to Alt-shift-F11 to switch nvidia's ForceFullCompositionPipeline state (i know, too many sed)

nvidia.compositionpipeline.switch.sh

#!/bin/bash
killall aosd_cat
if nvidia-settings -t -q CurrentMetaMode|grep 'ForceCompositionPipeline=On' &>/dev/null ; then
    nvidia.compositionpipeline.disable.sh &
    echo "FFCP=OFF" | aosd_cat -n "Sans Bold 15"  -x 0 -y 0 -p 0 -t 0 -b 255 -s 255 -d 10 -R yellow  -u 5000 &
    echo "FFCP=OFF" | aosd_cat -n "Sans Bold 15"  -x 0 -y 0 -p 2 -t 0 -b 255 -s 255 -d 10 -R yellow  -u 5000 &
        else
    nvidia.compositionpipeline.enable.sh &
    echo "FFCP=ON" | aosd_cat -n "Sans Bold 15"   -x 0 -y 0 -p 0 -t 0 -b 255 -s 255 -d 10 -R "#99ccff"  -u 5000 &
    echo "FFCP=ON" | aosd_cat -n "Sans Bold 15"   -x 0 -y 0 -p 2 -t 0 -b 255 -s 255 -d 10 -R "#99ccff"  -u 5000 &
fi

nvidia.compositionpipeline.enable.sh

#/bin/bash
sh -c "nvidia-settings --assign CurrentMetaMode=\"$(nvidia-settings -t -q CurrentMetaMode |tr -d "\n"|sed 's/ViewPortIn=/ForceFullCompositionPipeline=On, ViewPortIn=/g'|sed 's/.*:://'|sed 's/^ *//;s/ *$//')\""

nvidia.compositionpipeline.disable.sh

#!/bin/bash
sh -c "nvidia-settings --assign CurrentMetaMode=\"$(nvidia-settings -t -q CurrentMetaMode |tr -d "\n"|sed 's/.*:://'|sed 's/^ *//;s/ *$//'|sed "s/CompositionPipeline=On/CompositionPipeline=Off/g")\""

Offline

#2840 2016-11-23 23:07:34

Mladia
Member
Registered: 2016-04-30
Posts: 26

Re: Post your handy self made command line utilities

I have a script that gives me the current weather forecast.

It finds the city by IP and then send query to yahoo.

getWeather.sh

#!/bin/bash


place=$(curl -s http://whatismycountry.com/ | sed -n 's|.*> *\(.*\)</h3>|\1|p')

if ! [ -z  $@  ]
then
	place=$@
fi

echo "Location is $place"

qu="select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"$place\") and u='c'"



curl -s https://query.yahooapis.com/v1/public/yql -d q="$qu" -o ~/.cache/weather.xml

#for which hour?
grep "yweather:forecast" ~/.cache/weather.xml | grep -o "<title>Conditions [^\"]*</title>" | grep -o "[0-9]*:[0-9]* [A-Z][A-Z]"

#print temperature
grep "yweather:condition" ~/.cache/weather.xml | grep -o "temp=\"[^\"]*\"" | grep -o "\"[^\"]*\"" | grep -o "[^\"]*"

Offline

#2841 2016-11-24 08:24:52

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 559

Re: Post your handy self made command line utilities

You will like "curl wttr.in"


Arch is home!
i3 rofi w3m mc vim mutt pass
w3m-dmenu

Offline

#2842 2016-11-24 13:52:22

Awebb
Member
Registered: 2010-05-06
Posts: 4,640

Re: Post your handy self made command line utilities

Docbroke wrote:

You will like "curl wttr.in"

We were unable to find your location, so we have brought you to Oymyakon, one of the coldest permanently inhabited locales on the planet.

Brrrrr!

Offline

#2843 2016-11-24 15:59:19

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 559

Re: Post your handy self made command line utilities

Awebb wrote:
Docbroke wrote:

You will like "curl wttr.in"

We were unable to find your location, so we have brought you to Oymyakon, one of the coldest permanently inhabited locales on the planet.

Brrrrr!

wttr.in uses ip address to identify location, if that fail you may try

 curl wttr.in/"yourlocation"

Arch is home!
i3 rofi w3m mc vim mutt pass
w3m-dmenu

Offline

#2844 2016-12-06 11:06:27

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,552

Re: Post your handy self made command line utilities

Another workaround for an unfixed plasma bug.
Since sometimes it does not update panels when compositing is switched off, it happens that i when i play games, i'm often in late, because i see the un-updated clock on the other head (i play with no compositing).
Fortunately, the Xrender Backend does not impact performance and nvidia ForceCompositingPipeline is able to do vsync even when using Xrender, so here is a script i bound to alt+shift+F10 that switches compositor type and notify it via OSD:

#!/bin/bash
killall aosd_cat
Current_compositing=$(qdbus org.kde.KWin /Compositor compositingType)

function reconfigure {
    qdbus org.kde.KWin /KWin reconfigure
    sleep 0.2
    qdbus org.kde.KWin /Compositor suspend
    sleep 0.2
    qdbus org.kde.KWin /Compositor resume   
}

function OSD {
    echo "$1" | aosd_cat -n "Sans Bold 15"   -x 0 -y 0 -p 0 -t 0 -b 255 -s 255 -d 10 -R "#99ccff"  -u 5000 &
    echo "$1" | aosd_cat -n "Sans Bold 15"   -x 0 -y 0 -p 2 -t 0 -b 255 -s 255 -d 10 -R "#99ccff"  -u 5000 &

}

if [ $Current_compositing = "gl2" ] ; then
    echo Current:gl2
    echo Switch :xrender
    kwriteconfig5 --file kwinrc --group Compositing --key Backend XRender
    OSD "Compositor: OpenGL -> xrender"
    reconfigure
    exit
fi

if [ $Current_compositing = "xrender" ] ; then
    echo Current:xrender
    echo Switch :none
    OSD "Compositor: xrender -> Disabled"
    qdbus org.kde.KWin /Compositor suspend
    exit
fi

if [ $Current_compositing = "none" ] ; then
    echo Current:none
    echo Switch :gl2
    kwriteconfig5 --file kwinrc --group Compositing --key Backend OpenGL
    OSD "Compositor: disabled -> OpenGL"
    reconfigure
    exit
fi

Offline

Board footer

Powered by FluxBB