You are not logged in.

#2426 2014-08-27 17:33:57

Theta91
Member
Registered: 2014-08-21
Posts: 13

Re: Post your handy self made command line utilities

steve___ wrote:

I am using sxiv v1.1.1, how does this differ from:

'-r   Search the given directories recursively for images to view.' ?

The only real difference would be that it only pulls from the dir you feed it and not any subdirs*. Mainly my goal was a single command that would take both a directory and an image file. [granted, I could've just added the r option to the Popen string in such a case, but I typically don't want to view the images in subdirs of a dir.]

*assuming I've actually understood `sxiv -r dir/` correctly and it collates all photos in dir and any subdirs for viewing.

Offline

#2427 2014-08-27 17:44:12

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

If you want the images in a specific dir, try:

$ sxiv -q /path/to/images/*

Last edited by steve___ (2014-08-27 17:52:13)

Offline

#2428 2014-08-27 17:58:42

Theta91
Member
Registered: 2014-08-21
Posts: 13

Re: Post your handy self made command line utilities

steve___ wrote:

If you want the images in the current dir, try:

$ sxiv -q *

Functionally, that's what the script does, except you feed it 'dir' instead of 'dir/*', which is easier on the fingers with auto-completion smile

Offline

#2429 2014-08-28 06:42:53

quequotion
Member
From: Oita, Japan
Registered: 2013-07-29
Posts: 814
Website

Re: Post your handy self made command line utilities

EDIT: This has been superseded by makepkg-optimize, following the acceptance of my revised proposals for libmakepkg.

I've kept this to myself long enough; it's one of my favorites:

This wrapper works with makepkg and all aur-helpers!

qqbuild

#! /bin/bash

while (( "$#" )); do
  case "$1" in
    pgo) # Build with profile generation or rebuild using profile data
      if [ ! -d "$PWD/profile" ]; then
        mkdir "$PWD/profile"
        QQPGO="-fprofile-generate -fprofile-dir=$PWD/profile"
        QQPGOLDFLAGS="-lgcov"
      else
        [[ ! -d "$PWD/profile.used" ]] &&  mv "$PWD/profile" "$PWD/profile.used"
        QQPGO="-fprofile-correction -fprofile-use -fprofile-dir=$PWD/profile.used"
      fi
      shift
    ;;
    cflags) # inject a particular -cflag "-or -several -cflags"
      QQEXTRACFLAGS="$2"
      shift 2
    ;;
    ldflags) # inject a particular ,-ldflag ",-or,-several,-ldflags"
      QQEXTRALDFLAGS="$2"
      shift 2
    ;;
    makeflags) # inject a particular --makeflag "-or --several --makeflags"
      QQEXTRAMAKEFLAGS="$2"
      shift 2
    ;;
    nolto) # disable link-time-optimization
      QQNOLTO="yes"
      shift
    ;;
    graphite) # vector optimizations
      QQGRAPHITE="-fgraphite-identity -floop-nest-optimize -ftree-loop-distribution -ftree-vectorize"
      shift
    ;;
    rice) # maximum tuning
      QQRICECFLAGS="-Ofast -fbranch-target-load-optimize2 -fcx-fortran-rules -fdata-sections -ffloat-store -fgcse-las -fgcse-sm -fipa-pta -floop-nest-optimize -fmodulo-sched -fmodulo-sched-allow-regmoves -fno-enforce-eh-specs -funsafe-math-optimizations -fno-threadsafe-statics -fnothrow-opt -fno-var-tracking-assignments -fomit-frame-pointer -fopenmp -fPIC -freg-struct-return -freschedule-modulo-scheduled-loops -fsched-pressure -fsched-spec-load -fsched-spec-load-dangerous -fsched-stalled-insns=0 -fsched2-use-superblocks -fselective-scheduling -fselective-scheduling2 -fsel-sched-pipelining -fsel-sched-pipelining-outer-loops -fshort-wchar -ftree-parallelize-loops=$(grep "^core id" /proc/cpuinfo | sort -u | wc -l) -ftree-lrs -ftree-vectorize -fvariable-expansion-in-unroller -maccumulate-outgoing-args -Wno-sizeof-pointer-memaccess" # -fmerge-all-constants
      QQRICELDFLAGS="-lpthread -lgomp" #,-shared
      shift
    ;;
    help|*)
      [[ ! "${1}" == "help" ]] && \
        break
      echo -e  "\nTweak build environment:\n"
               "$0 pgo - build with profile generation or rebuild using profile data\n"
               "$0 cflags - inject a particular -cflag \"-or -several -cflags\"\n"
               "$0 ldflags - inject a particular ,-ldflag \",-or,-several,-ldflags\"\n"
               "$0 makeflags - inject a particular --makeflag \"-or --several --makeflags\"\n"
               "$0 nolto - disable link-time optimization\n"
               "$0 graphite - enable vector optimizations\n"
               "$0 rice - maximum tuning\n"
               "\nOptions are stackable:\n"
               "\n$0 rice pgo makeflags -j1\n"
      exit
    ;;
  esac
done

#export QQCPPFLAGS #unimplemented
export QQCFLAGS="$QQEXTRACFLAGS $QQRICECFLAGS $QQPGO $QQGRAPHITE"
export QQCXXFLAGS="$QQCFLAGS"
export QQLDFLAGS="$QQEXTRALDFLAGS $QQRICELDFLAGS $QQPGOLDFLAGS"
export QQMAKEFLAGS="$QQEXTRAMAKEFLAGS"
[[ -n "${QQNOLTO}" ]] && export QQNOLTO

#printf "qqcxxflags:$QQCFLAGS\nqqldflags:$QQLDFLAGS\nqqmakeflags:$QQMAKEFLAGS\nqqnolto:$QQNOLTO\n" #debug

command -- "$@"

Also append this to makepkg.conf, to enable LTO by default:

#-- Link-Time Optimization
if [ ! "$QQNOLTO" == "yes" ]; then
  CFLAGS+=" -flto=$(getconf _NPROCESSORS_ONLN)"
  CXXFLAGS+=" -flto=$(getconf _NPROCESSORS_ONLN)"
  LDFLAGS+=" $CFLAGS -fuse-linker-plugin"
  LTOPLUGIN="$(gcc -print-search-dirs | grep install | awk '{print $2 "liblto_plugin.so"}')"
  ARFLAGS+=" --plugin $LTOPLUGIN"
  RANLIBFLAGS+=" --plugin $LTOPLUGIN"
  NMFLAGS+=" --plugin $LTOPLUGIN"
fi

Use qqbuild to wrap something that builds a package:

qqbuild nolto rice makepkg

See "qqbuild help" for details!

EDIT: I have proposed integrating the build options from qqbuild into makepkg itself on the pacman-dev mailing list.

EDIT again: But my proposal was rejected because makepkg is getting simplified; fork that!

Last edited by quequotion (2019-07-11 08:39:10)

Offline

#2430 2014-08-28 07:38:44

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Hmm, the idea is quite similar to what I did in FFcast. One tip,

- $@
+ command -- "$@"

First, you need to quote "$@" for word splitting to work properly.
Second, prefixing `command` will avoid e.g. internal functions from being run.

Last edited by lolilolicon (2014-08-28 07:39:09)


This silver ladybug at line 28...

Offline

#2431 2014-08-28 09:56:49

quequotion
Member
From: Oita, Japan
Registered: 2013-07-29
Posts: 814
Website

Re: Post your handy self made command line utilities

lolilolicon wrote:

First, you need to quote "$@" for word splitting to work properly.
Second, prefixing `command` will avoid e.g. internal functions from being run.

Thanks! I wasn't thinking about preventing internal functions; have to keep in mind someone will always be able to find some way to break something!

Offline

#2432 2014-08-29 13:46:46

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

Awk script to format iw scan output:

iwscan.awk

/^BSS/ {
        MAC = $2
        aps[MAC]["enc"] = "Open"
}
$1 == "SSID:" {
        aps[MAC]["SSID"] = $2
}
$1 == "freq:" {
        aps[MAC]["freq"] = $NF " MHz"
}
$1 == "signal:" {
        aps[MAC]["sig"] = $2 " " $3
}

$1 == "WEP:" {
        aps[MAC]["enc"] = "WEP"
}
$1 == "RSN:" {
        aps[MAC]["enc"] = "RSN"
}
$1 == "WPA:" {
        aps[MAC]["enc"] = "WPA"
}

END {
        printf "%-32s %-16s %-16s %-16s\n",
                "SSID", "Frequency", "Signal", "Encryption"

        for (ap in aps) {
                printf "%-32s %-16s %-16s %-16s\n",
                        aps[ap]["SSID"], aps[ap]["freq"], aps[ap]["sig"], aps[ap]["enc"]
        }
}

output

dfr-laptop dev # iw wlan0 scan | awk -f ./iwscan.awk
SSID                             Frequency        Signal           Encryption
saitaOpen                        2412 MHz         -74.00 dBm       Open
oppilas                          2412 MHz         -77.00 dBm       RSN
NADIA                            2462 MHz         -89.00 dBm       RSN
SoneraGateway00-22-07-ED-79-95   2412 MHz         -89.00 dBm       WPA
wEKSOTE                          2412 MHz         -76.00 dBm       RSN
SaitaBYOD                        2412 MHz         -75.00 dBm       RSN
wULAPPA                          2412 MHz         -75.00 dBm       RSN

Last edited by defer (2014-08-29 13:53:28)

Offline

#2433 2014-08-29 15:23:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,201

Re: Post your handy self made command line utilities

defer wrote:

Awk script to format iw scan output:

But be careful.  Note the last line in the output of iw help


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#2434 2014-08-29 15:54:38

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

You could just do it in C - wireless_tools's iwlib should do all that.  Here's a starter extracted from swifer:

// compile: gcc -o this_file this_file.c -liw
#include <stdio.h>
#include <iwlib.h>

int main(int argc, const char **argv) {
	if (argc < 2 || (getuid() != 0)) return 1;
	wireless_scan_head context;
	wireless_config cur;
	int we_ver, skfd;
	we_ver = iw_get_kernel_we_version();
	skfd = iw_sockets_open();
	iw_get_basic_config(skfd, (char *) argv[1], &cur);
	iw_scan(skfd, (char *) argv[1],we_ver, &context);
	wireless_scan *ws;
	for (ws = context.result; ws; ws = ws->next) {
		if (ws->b.key_flags == 2048) printf("X ");
		else printf("o ");
		printf("%2d%% %s\n", 100 * ws->stats.qual.qual / 70, ws->b.essid);
	}
	iw_sockets_close(skfd);
	return 0;
}

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#2435 2014-08-30 07:59:05

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

If you are using gawk, you can use the switch statement:

    {
    switch ($1) {
          case /SSID/:
             aps[MAC]["SSID"] = $2
             break
          case /freq/: 
             aps[MAC]["freq"] = $NF " MHz"
             break
          case /signal/: 
             aps[MAC]["sig"] = $2 " " $3
             break
          case /WEP/: 
             aps[MAC]["enc"] = "WEP"
             break
          case /RSN/: 
             aps[MAC]["enc"] = "RSN"
             break
          case /WPA/: 
             aps[MAC]["enc"] = "WPA"
            }
     }

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2436 2014-08-30 10:18:13

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

Or just use `wpa_cli scan` and `wpa_cli scan_results` which gives you much better information anyway.

Offline

#2437 2014-08-31 04:08:33

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

Earnestly wrote:

Or just use `wpa_cli scan` and `wpa_cli scan_results` which gives you much better information anyway.

You're right. wpa_cli is better.

Offline

#2438 2014-09-02 00:48:45

snakeroot
Member
Registered: 2012-10-06
Posts: 168

Re: Post your handy self made command line utilities

Script to Find .pac{new,old,orig}

I always forget to clean up after pacman. This script can be run from a cron job or systemd service or standalone. It finds files left by pacman and emails a list of users set in the $recips variable.

The script writes directly to the $recips mail spool in mbox format. This allows receipt of reminder emails without requiring an mta.

#! /bin/bash
[[ $EUID -eq 0 ]] || { printf "%s \n" "ERROR: pac_find must be run as root"; exit 1; }
result_string=$(sudo /usr/bin/find /boot /etc /home /lib /opt /root /usr -regextype posix-egrep -regex '.*/.*\.pac(new|orig|save)' -print0 | /usr/bin/sed 's/\x0/\|/g')
declare -a recips=('chris' 'root')

if [[ -n "$result_string" ]]; then
	for i in "${recips[@]}"; do
		c_time=$(/usr/bin/env LC_ALL=C /usr/bin/date +%c)
		mbox="/var/spool/mail/$i"
		/usr/bin/flock $mbox echo -e "From systemd@${HOSTNAME} ${c_time}\nDate: $c_time \nFrom: systemd@${HOSTNAME} \nTo: ${i}@${HOSTNAME} \nRe: Pacman Files \nThe following pacman files have been found: \n\t$(echo $result_string | /usr/bin/sed 's/|/\n\t/g')\n" >> $mbox
	done
fi

Offline

#2439 2014-09-02 20:52:38

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

It's best to deal with pacnew files ASAP i.e. right after you updated your system.

Offline

#2440 2014-09-05 05:18:07

allencch
Member
Registered: 2011-03-25
Posts: 118

Re: Post your handy self made command line utilities

I would like to share a script that can list the installed but renamed or removed AUR packages.
The following script intends to display a list of installed packages, but not found in either AUR or official repositories. They are either removed or renamed. package-query is required.

#!/bin/bash
#Get the packages
packages=(`pacman -Qmq | sort`)

#For each of the package, check with the AUR
packagesStr=''
for x in ${packages[@]} ; do
  packagesStr+="'$x' "
done

packagesOnAur=(`/bin/sh -c "package-query -A -f '%n' $packagesStr | sort"`)

#As the rule of thumb, the installed packages are more than packages checked in the AUR
for((i=0;i<${#packagesOnAur[@]};i++)) ; do
  j=0
  while [[ $j -lt ${#packages[@]} ]] ; do
    if [[ ${packagesOnAur[$i]} == ${packages[$j]} ]] ; then
      unset packages[$j]
      packages=("${packages[@]}")
      break
    fi
    ((j++))
  done
done

for x in ${packages[@]} ; do
  echo $x
done

Last edited by allencch (2014-09-05 05:18:23)

Offline

#2441 2014-09-05 05:46:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

Merging with Command Line utilities...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2442 2014-09-08 14:44:46

Thisguy_
Member
Registered: 2012-12-03
Posts: 41

Re: Post your handy self made command line utilities

#!/bin/bash
PARSE="`echo "$*" | sed s/" "/"+"/g`"
/usr/bin/links http://www.google.com/search?q=$PARSE

A little macro to make links/lynx google something real quick.

#!/bin/bash
if [ "$1" == "" ]; then
	PARSE="ssh"
else
	PARSE="$1"
fi

i=0;
while [ ! "`screen -ls | grep $PARSE`" == "" ]; do
	i=`expr $i \+ 1`;
	if [ $i == 1 ]; then
		screen -S $PARSE -p 0 -X stuff ' ';
	fi;
	if [ $i == 60 ]; then
		i=0;
	fi;
	sleep 1;
done

Repeatedly type spaces into an ssh session run through screen, one per minute. If you like to use ssh and screen for tunneling and get the same issue I do, where ssh cuts itself off after a while, this is useful.

Last edited by Thisguy_ (2014-09-08 14:48:22)

Offline

#2443 2014-09-08 15:07:13

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Post your handy self made command line utilities

Thisguy_ wrote:

Repeatedly type spaces into an ssh session run through screen, one per minute. If you like to use ssh and screen for tunneling and get the same issue I do, where ssh cuts itself off after a while, this is useful.

You need to look at ClientAlive{Interval,Countmax} and TCPKeepAlive in sshd_config smile

Scott

Offline

#2444 2014-09-08 15:21:20

Thisguy_
Member
Registered: 2012-12-03
Posts: 41

Re: Post your handy self made command line utilities

Unfortunately, I don't have access to sshd on the servers that are cutting me off wink

(In the case that you accidentally slipped the D into that file name and I'm just pointing out your mistake by one of my own, I'll check ssh_config.)

EDIT: Edited for clarity. I have an account, obviously. But I'm not root or powerful.

Last edited by Thisguy_ (2014-09-08 15:25:10)

Offline

#2445 2014-09-08 20:21:08

dz
Member
Registered: 2013-08-21
Posts: 16

Re: Post your handy self made command line utilities

I wanted a general-purpose script to run similar commands in parallel.  I came up with this, and welcome people's suggestions for improvements.

#!/usr/bin/env zsh
set -ue

adding_to_command=1
argsets=( )
command=( )

for arg in "$@"; do
  if [[ "$arg" = "--" ]]; then
    adding_to_command=0
  elif [[ "$adding_to_command" -eq 1 ]]; then
    command+=( "$arg" )
  else
    argsets+=( "$arg" )
  fi
done
unset arg 

for argset in "${argsets[@]}"; do
  printf "%s\0" "$argset"
done | xargs -P4 -n1 -0 "${command[@]}"
unset argset

Here's how it works:

$ juxt echo hello, -- "world" "asynchronous processing"
hello, asynchronous processing
hello, world

Offline

#2446 2014-09-08 22:11:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

dz, see GNU Parallel


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#2447 2014-09-08 23:13:54

karkhaz
Member
Registered: 2014-01-25
Posts: 79

Re: Post your handy self made command line utilities

dz wrote:

I wanted a general-purpose script to run similar commands in parallel.  I came up with this, and welcome people's suggestions for improvements.

Here's how it works:

$ juxt echo hello, -- "world" "asynchronous processing"
hello, asynchronous processing
hello, world

The above could have been accomplished using

echo "world\nasynchronous processing" | parallel "echo hello, {}"

Using GNU Parallel.

Offline

#2448 2014-09-08 23:42:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

For another fun approach, you can abuse make as it naturally processes in parallel:

$ cat Makefile
%:
	@echo hello, $@
$ make world "asynchronous processing"
hello, world
hello, asynchronous processing

EDIT: Even better:

$ cat Makefile 
CMD   ?= echo

%:
	@${CMD} $@

$ CMD="echo hello," make world "asynchronous processing"
hello, world
hello, asynchronous processing

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#2449 2014-09-09 05:29:55

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

karkhaz wrote:
dz wrote:

I wanted a general-purpose script to run similar commands in parallel.  I came up with this, and welcome people's suggestions for improvements.

Here's how it works:

$ juxt echo hello, -- "world" "asynchronous processing"
hello, asynchronous processing
hello, world

The above could have been accomplished using

echo "world\nasynchronous processing" | parallel "echo hello, {}"

Using GNU Parallel.

At least in bash, you have to use 'echo -e' to interpret the '\n':

$ echo "world\nasynchronous processing" | parallel "echo hello, {}"
hello, world\nasynchronous processing
$ echo -e "world\nasynchronous processing" | parallel "echo hello, {}"
hello, world
hello, asynchronous processing

Offline

#2450 2014-09-09 11:22:01

karkhaz
Member
Registered: 2014-01-25
Posts: 79

Re: Post your handy self made command line utilities

karol wrote:

At least in bash, you have to use 'echo -e' to interpret the '\n':

$ echo "world\nasynchronous processing" | parallel "echo hello, {}"
hello, world\nasynchronous processing
$ echo -e "world\nasynchronous processing" | parallel "echo hello, {}"
hello, world
hello, asynchronous processing

...I should probably sit down and learn bash one day, since everybody seems to use it. Using zsh makes me lazy.

Also, a neater way of doing this using parallel is

parallel "echo hello, " ::: world "asynchronous processing"

Offline

Board footer

Powered by FluxBB