You are not logged in.

#3301 2020-01-05 21:23:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,332
Website

Re: Post your handy self made command line utilities

In the spirit of reinventing the wheel, and doing it badly... I dumped all AUR helpers last year and the one piece of functionality I was missing was searching for packages (other than using surfraw, which is fine). So I use this. Let the witchburning begin!

#!/usr/bin/env bash
# Search for packages in repos and AUR

package=$1
url='https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg='
aurfile='/tmp/aursearch'

pacman -Ss "^${package}$"

status=$?
if (( "$status" != 0 )); then
  printf "%s\n" "Not in repos, searching AUR..."

  trap "rm '${aurfile}'" EXIT INT TERM

  curl -s "${url}$1" -o "$aurfile"
  awk '{gsub(/"/,"");  printf "• %s\n", $0}' \
      <(jshon -e results -a -e Name < "$aurfile")
fi

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3302 2020-01-05 21:28:01

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 3,013

Re: Post your handy self made command line utilities

jasonwryan wrote:

I dumped all AUR helpers last year and the one piece of functionality I was missing was searching for packages (other than using surfraw, which is fine). So I use this. Let the witchburning begin!

Okay, let me start.

"Hey, not only do you use AUR helpers, you're the author of one!"


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3303 2020-01-05 22:17:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,631
Website

Re: Post your handy self made command line utilities

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3304 2020-01-06 06:03:39

waitnsea
Member
From: France
Registered: 2013-02-10
Posts: 37

Re: Post your handy self made command line utilities

Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

Sure, caterpillars work better, and here
    it's not an advertisement, it's pure ecological delirium !

Last edited by waitnsea (2020-01-06 09:43:25)

Offline

#3305 2020-01-08 12:22:04

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 9,381
Website

Re: Post your handy self made command line utilities

Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

https://en.wikipedia.org/wiki/Paddle_st … dle_wheels ?


Sakura:-
Mobo: MSI X299 TOMAHAWK ARCTIC // Processor: Intel Core i7-7820X 3.6GHz // GFX: nVidia GeForce GTX 970 // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 5x 1TB HDD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3306 2020-01-08 12:26:31

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 621
Website

Re: Post your handy self made command line utilities

WorMzy wrote:
Trilby wrote:

The only way AUR helpers could be compared to wheels would be to compare them to wheels on a boat.

https://en.wikipedia.org/wiki/Paddle_st … dle_wheels ?

It's funny, because AUR helpers actually (mostly) are outdated technology with low efficency.

Offline

#3307 2020-01-08 13:31:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,631
Website

Re: Post your handy self made command line utilities

And most of them are also steaming piles of ship!


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3308 2020-01-09 10:36:09

Awebb
Member
Registered: 2010-05-06
Posts: 5,328

Re: Post your handy self made command line utilities

I find AUR helpers immensely useful. You can search the AUR, show infos about packages and even be informed about updates with a few quick commands... Then you feed that information to your own scripts and call it a day.

The buildorder command of auracle is really nice.

Offline

#3309 2020-01-15 01:01:43

Alad
Wiki Admin/IRC Op/TU
From: Bagelstan
Registered: 2014-05-04
Posts: 2,066
Website

Re: Post your handy self made command line utilities

From what I've seen, people outright claiming there is no use for AUR helpers have a very limited amount of AUR packages. When you have* more than say, 100 AUR packages, some automation is in order.

To get back to the topic at hand, jq might be worth looking into for the script in #3301.

* "have" can both mean "use" and "maintain". I've certainly seen people maintain more than 300 AUR packages. An AUR helper can just as well assist with maintanance tasks.

Last edited by Alad (2020-01-15 01:04:20)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Honest Alad's Package Emporium—Now with added bugs! (Grand reopening: December 1st 2018)

Offline

#3310 2020-01-15 02:18:13

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,332
Website

Re: Post your handy self made command line utilities

Alad wrote:

To get back to the topic at hand, jq might be worth looking into for the script in #3301.

How so? I have always used jshon because it was community contribution. What advantages are there to jq that would make it a worthwhile change?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3311 2020-01-15 02:31:59

Alad
Wiki Admin/IRC Op/TU
From: Bagelstan
Registered: 2014-05-04
Posts: 2,066
Website

Re: Post your handy self made command line utilities

Mostly that it would reduce your script to 1-2 lines? E.g there's no need to remove quotes, because of the -r flag.

Last edited by Alad (2020-01-15 02:32:16)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Honest Alad's Package Emporium—Now with added bugs! (Grand reopening: December 1st 2018)

Offline

#3312 2020-01-23 20:02:41

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 963

Re: Post your handy self made command line utilities

@jasonwryan, I have a few questions about your script #3301
Why do you use curly braces in the trap?
With pacman you search for exact results, why didn't you set it for the AUR search also, any specific reason?
You set variable 'package=$1', the curl command uses '$1', why? Thanks.

Offline

#3313 2020-01-23 20:31:15

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,332
Website

Re: Post your handy self made command line utilities

That was an earlier iteration. The current version also searches AUR.

The curly braces are for legibility, and the variable package was setting up for the larger script (I pasted it here for feedback before going further in case I screwed something obvious up) smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3314 2020-01-23 21:47:40

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 963

Re: Post your handy self made command line utilities

Yes clear. I have already used the one from #3301 works nice and it gave me a clear picture how to use the AUR API wink
I have created a script with the help of yours to update all my AUR packages trough the API, if I think it's ready to publish I will.
I will have a look at the new one you published tomorrow, thanks.

Offline

#3315 2020-01-24 10:38:12

Alad
Wiki Admin/IRC Op/TU
From: Bagelstan
Registered: 2014-05-04
Posts: 2,066
Website

Re: Post your handy self made command line utilities

I have created a script with the help of yours to update all my AUR packages trough the API, if I think it's ready to publish I will.

No! More AUR helpers! See what you've done, triangle thingy! wink

Last edited by Alad (2020-01-24 10:39:29)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Honest Alad's Package Emporium—Now with added bugs! (Grand reopening: December 1st 2018)

Offline

#3316 2020-01-25 17:54:53

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 963

Re: Post your handy self made command line utilities

Nice script @jasonwryan, the one on 'shiv'. Though I personally like the pacman search more because it can give you colored output which gives a better separation in my script and shows if the package is already installed, but I think that's personal;)
I adapted the jshon check also.
Here is a first draft based on #3301

#!/bin/bash
# shellcheck disable=2162
#set -x
# AUR helper that leaves you in control

#exclude file, don't search for this package, one package(name only) per line
exclude="$HOME"/bin/exclude/aur-update-excl
buildir="$HOME"/build

#Don't touch the URL's or variables below only if you must
url1='https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg=' #search api
url2='https://aur.archlinux.org/rpc/?v=5&type=info&arg[]='     #info api
url3='https://aur.archlinux.org/cgit/aur.git/snapshot/'      #get package

package=$2
aurfile='/tmp/aurfile'
result='/tmp/result'
trap 'rm -f "$aurfile" "$result"' EXIT INT TERM

type jshon > /dev/null 2>&1 || {
  printf "%s\n" "No jshon, bailing...";
  exit 1;
}

_reposearch() {
  pacman -Ss "^${package}$"

  status=$?
  if [ "$status" != 0 ]; then
    echo "• Not in repos, searching AUR..."
    printf "\n" && _aursearch
  elif [ "$status" = 0 ]; then
    printf "\n" && read -p "• Search AUR too? Y or N : " aur
    printf "\n" && [[ "$aur" =~ ([Yy]) ]] && _aursearch || exit
  fi
}

_aursearch() {
    curl -s "${url1}$package" -o "$aurfile"
    mapfile -t aur < <(jshon -e results -a -e Name -u < "$aurfile")

    [ ${#aur[@]} -eq 0 ] && printf "• Also not in AUR" && unset aur && exit
    printf "%s\n" "• Exit or download one of the AUR result/s"

    select name in "${aur[@]}" exit; do
      case $name in
          exit) unset aur && exit;;
             *) printf "%s\n" "$name" > "$result"; printf "\n"; break;;
      esac
    done
    [ -n "$result" ] && unset aur && _get || exit
}

#_checkupd() {
#  aurprog=/tmp/aurprog
#  aurinfo=/tmp/aurinfo
#  aurver=/tmp/aurver
#  localver=/tmp/localver
#  trap 'rm "$aurinfo" "$aurprog" "$aurver" "$localver"' EXIT INT TERM

#  if [ -e "$exclude" ]; then
#    pacman -Qm  > "$aurprog"
#    while read -r excl; do
#     sed -i "/$excl/d" "$aurprog"
#    done < "$exclude"
#  else
#    pacman -Qm > "$aurprog"
#  fi

#  while read -r prog version; do
#    echo "$version" > "$localver"

#    curl -s "${url2}$prog" -o "$aurinfo"
#    jshon -e results -a -e Version -u < "$aurinfo" > "$aurver"

#    [ "$( diff "$localver" "$aurver" )" != '' ] &&
#     printf "%s\n" "• Update for $prog New version is $(<"$aurver")" && _get
#  done < "$aurprog"
#}

_get() {
  [ -e "$result" ] && prog="$(<"$result")"
  [ "$1" = -g ] && prog="$package"
  [ -d "$buildir/$prog" ] &&
   mv "$buildir/$prog" "$buildir/${prog}.bak"
  curl -sL "${url3}${prog}.tar.gz" -o "$buildir/${prog}.tar.gz"

  tar zxvf "$buildir/${prog}.tar.gz" -C "$buildir" && rm "$buildir/${prog}.tar.gz"

  printf "%s\n" "• Downloaded $prog & extracted to $buildir/$prog"
}

_pass() {
  [ "$1" = -r ] && _reposearch
  [ "$1" = -a ] && _aursearch
  [ "$1" = -g ] && _get "$@"
#  [ "$1" = -u ] && _checkupd
  [ "$1" = "" ]
  [ -z "$1" ] && printf "%s\\n" "Options: [ -r <package-name> #search package in official repo or/and in AUR ]
         [ -a <package-name> #search if package excist in AUR ]
         [ -u #check for AUR updates ]
         [ -g <package-name> #get AUR package ]"
}
_pass "$@"

exit 0

edit: the exclude file only works for the update part, not the search..;)
made small clean changes, no need for awk at all, 'u - unstring' is enough for this script

edit2: for now I have disabled the check for updates, because not all git packages update their PKGBUILD
There are packages which are good like 'profile-sync-daemon' but a few are not like 'archiso-git'
It would be nice if pkgver was updated to the correct version;) then for scripts like this one you don't have to invent the wheel...

Last edited by qinohe (2020-01-27 17:08:09)

Offline

#3317 2020-02-13 13:27:56

neol09
Member
Registered: 2020-02-13
Posts: 2

Re: Post your handy self made command line utilities

Switching between two languages is straightforward
But i wanted to switch between 3 languages and display the status

#!/bin/bash

#  This script wraps setxkbmap. For those writing in more than two languages.
#    It switches to your last active language unless activated in quick
#    succession, In which case it cycles. --inspired by gBoard ( android )

# CONF
languages=(us il ru)
timeout=1s

file="/tmp/${USER}_LANG"
current=$(setxkbmap -print | sed -En \
  "/xkb_symbols/{s/.*($(sed 'y/ /|/' <<< "${languages[@]}")).*/\\1/p}")

setLang() {
  setxkbmap "$1"
  echo "[${1}]:${last}" > "$file"
  "$0" rest &
}

if [ "$1" = "rest" ]; then
  sleep "$timeout"
  exit 2
elif [ ! -f "$file" ]; then
  last=${languages[1]}
  setLang "${languages[0]}"
  chmod 600 "$file"
  exit 1
else

  last="$(sed 's/.*://' "$file")"

  for L in "${languages[@]}"; do
    if [ "$L" = "$current" ]; then
      if pgrep -fl "${0##*/} rest" || [ "$last" = "$current" ]; then
        setLang "${languages[$((++a % ${#languages[@]}))]}"
      else
        buff=$last last=$current
        setLang "$buff"
      fi
      break
    fi
    (( a++ ))
  done
fi

an example for i3status

read_file lang {
path = /tmp/noel09_LANG
format = "%title: %content   "
}

and a tmux example

set -g status-right "#(cat /tmp/noel09_LANG)"

I enjoy bash, i want get better. roast me!

[edit] updated quoting,
Credit for the implementation goes to gradgrind
Haven't found other ways of getting the active keyboard layout.

[edit2] Is it over-complicated yet?

Last edited by neol09 (Yesterday 21:24:19)

Offline

#3318 2020-02-13 14:52:07

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 963

Re: Post your handy self made command line utilities

Only had a quick glance, but quoting isn't right, you do when you shouldn't '~=' and you don't when you should;)
Advice, install shellcheck or shellcheck-static (AUR) which is without Haskell 'bloat' (if you don't need it).
Shellcheck will at least help you spot really bad things. It may be wrong in rare occasions.
The script masters here may have more good advices..
Welcome to arch wink

Offline

#3319 2020-02-13 15:48:27

neol09
Member
Registered: 2020-02-13
Posts: 2

Re: Post your handy self made command line utilities

I've used shellcheck in the past but I've never heard about shellcheck-static! sounds lovely.
will update the post implementing all of it's adivce

Thanks for the warm welcome!

Offline

#3320 Today 18:01:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 963

Re: Post your handy self made command line utilities

neol09 wrote:

[edit2] Is it over-complicated yet?

Well, kinda yes tongue for something that can be done with 'setxkbmap us/il/ru'
It does work though. I haven't got a use for it anyway but sure you can simplify it.
Hobby away I'm sure you figure it out.

edit: at least one example;)

setxkbmap -query -symbols | awk '/layout/{print $2}'

oops, of course I meant

setxkbmap -query |awk '/layout/{print $2}'

Last edited by qinohe (Today 18:52:48)

Offline

Board footer

Powered by FluxBB