You are not logged in.

#2851 2017-01-01 21:20:23

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 4,942
Website

Re: Post your handy self made command line utilities

I'm using this stanza in ~/.profile as a display manager replacement:

case "$(tty)" in
        "/dev/tty1") exec startx;;
        "/dev/tty2") exec startx /usr/bin/bspwm;;
        "/dev/tty3") exec startx /usr/bin/kodi;;
        "/dev/tty4") exec startx /usr/games/steam -bigpicture;;
esac

EDIT: that's from my BunsenLabs system, hence the strange steam path smile

Last edited by Head_on_a_Stick (2017-01-01 23:24:34)

Offline

#2852 2017-01-03 21:22:01

dmerej
Member
From: Paris
Registered: 2016-04-09
Posts: 83
Website

Re: Post your handy self made command line utilities

OMG! That's so simple and clean. How did I not think of that?
Thanks for sharing smile

Last edited by dmerej (2017-01-03 21:23:42)


Responsible Coder, Scrum Master, Buildfarm Guru, Python3 Fan
twitter: @d_merej mastodon:@dmerej@mamot.fr

Offline

#2853 2017-01-05 15:45:27

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,371

Re: Post your handy self made command line utilities

Wrote a upnp port forwarder. So I can do forward 4001 4002 4003 to open ports and forward -close 4001 4002 4003 to close them

package main

import (
	"flag"
	"log"
	"os"
	"strconv"

	goupnp "github.com/NebulousLabs/go-upnp"
)

func main() {
	close := flag.Bool("close", false, "Close ports")
	flag.Parse()
	f, err := goupnp.Discover()
	if err != nil {
		log.Println(err)
		return
	}
	for _, v := range os.Args[1:] {
		if v == "-close" {
			continue
		}
		i, err := strconv.Atoi(v)
		if err != nil {
			log.Println(err)
			continue
		}
		if *close {
			if err := f.Clear(uint16(i)); err != nil {
				log.Println(err)
			}
		} else {
			if err := f.Forward(uint16(i), "go forwarder"); err != nil {
				log.Println(err)
			}
		}
	}
}

ENTER

~ Arch is not Arch unless it is Arch ~

Offline

#2854 2017-01-07 04:29:27

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

This one is very tiny, but handy, I have bound this script to "windowskey+o"

lariza $( echo $( xclip -o )) #before coffee

 lariza "$( xclip -o )" 

Just select the url, and press "win+o" it openes in lariza.

Last edited by Docbroke (2017-01-07 06:52:56)


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2855 2017-01-07 04:49:36

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

Re: Post your handy self made command line utilities

What's with the echo?  That's completely redundant:

lariza "$(xclip -o)"

FWIW, with qutbrowser, I just need this in keys.conf:

open -t {primary}
    p

Then I can just press p to open a tab to whatever was selected - if it wasn't a url the word or phrase will be looked up in my default search engine.


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

Offline

#2856 2017-01-07 05:08:50

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

looks like I missed morning coffee today.
You need qutebrowser open for using your shortcut, and it is like you select url open qutebrowser or go to the open window and press p.

For search I use my rofi/dmenu bsed script bound to shift+F11. So it is like "hold shift key and press F11 and then press insert" (this opens rofi and  PRIMARY is copied to input)
EDIT: and tabs are managed by i3, so I don't need tabs support in browser, so when I was using qutebrowser I had "tabs are windows" option in my config file.

Last edited by Docbroke (2017-01-07 05:36:07)


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2857 2017-01-09 16:31:18

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

this two scripts helps to mount/unmout android filesystem through mtp. It depends on simple-mtpfs (commandline utility from aur)

This one is used  to mount all android phones connected through mtp

sharad ~ > cat bin/phone
#!/bin/bash

[[ -d ~/mount ]] || mkdir ~/mount
cd ~/mount

for d in $(simple-mtpfs --list-devices | awk '{ print $1 }')
    do [[ -d $d ]] || mkdir "$d"
    simple-mtpfs --device $d $d
done

this one is used to unmount all, and clean the mount directory

sharad ~ > cat bin/unphone
#!/bin/bash

shopt -s lastpipe 

cd ~/mount
echo * | read dir 

for i in $dir
    do fusermount -u $i
done
rmdir ~/mount/*

Last edited by Docbroke (2017-01-09 16:42:17)


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2858 2017-01-09 19:06:20

teckk
Member
Registered: 2013-02-21
Posts: 323

Re: Post your handy self made command line utilities

#! /usr/bin/env bash

#Script for simple-mtpfs

PS3="
Select an option.:  "

#mount directory
dir="$HOME/usb"
					
while :; do
  clear
  options="Quit List_Device Mount Umount"
    select opt in $options; do
      case $opt in
        Quit) clear; exit
        ;;

        List_Device) simple-mtpfs --list-devices
        ;;
            
        Mount) simple-mtpfs "$dir"; echo "Mounted to "$dir"" 
        ;;

        Umount) fusermount -u "$dir"; echo "Unmounted" 
        ;;
      esac
    done
done

Offline

#2859 2017-01-10 01:28:04

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

@teckk, I haven't tested your script but I guess it won't mount >1 android devices. I still need to find out how to mount selected device when there are >1 devices are connected throught mtp.


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2860 2017-01-10 22:17:05

teckk
Member
Registered: 2013-02-21
Posts: 323

Re: Post your handy self made command line utilities

Oh ok,

simple-mtpfs -l
1: SamsungGalaxy models (MTP)
2: HTCAndroid Device ID2 (Zopo, HD2...)
simple-mtpfs --device 1 ~/usb1
ls ~/usb1
0                      ServeStream
Alarms                 SpeedSoftware
Android              
BSPlayer1.17.170.apk   brouter
Download........
simple-mtpfs --device 2 ~/usb2
ls ~/usb2
Alarms   Download    Movies   Pictures   Ringtones    obb
Android  'FM Recording'  Music........
mount
....
simple-mtpfs on /home/me/usb1 type fuse.simple-mtpfs (rw,nosuid,nodev,relatime,user_id=1000,group_id=100)
simple-mtpfs on /home/me/usb2 type fuse.simple-mtpfs (rw,nosuid,nodev,relatime,user_id=1000,group_id=100)
fusermount -u ~/usb1 ~/usb2 or both

Offline

#2861 2017-01-11 02:59:37

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

well I guess you misunderstood, what I was saying is that your script mounts single device, my script posted above mounts all the connected devices, but none of the script allows selecting 1/2 devices from all the connected devices. I already know how to use simple-tmpfs from commandline.


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2862 2017-01-19 07:52:47

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

touchpad on/off with xinput

#!/bin/bash

[[ $(xinput list-props 12 | awk '/Device Enabled/ { print $4 }') == 0 ]] && (xinput enable 12 && notify-send "touchpad enabled") || (xinput disable 12 && notify-send "touchpad disabled")

Note: "12" is device id for touchpad in my laptop


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2863 2017-01-19 09:25:17

Raynman
Member
Registered: 2011-10-22
Posts: 1,250

Re: Post your handy self made command line utilities

You can also use a name instead of an id. I suspect that would be less prone to change. And why cram the whole thing into a single line? How about

#!/bin/bash

# name or id that might change in the future
touchpad=12

if [[ $(xinput list-props "$touchpad" | awk '/Device Enabled/ { print $4 }') == 0 ]]; then
  action=enable
else
  action=disable
fi
xinput $action "$touchpad" && notify-send "touchpad ${action}d"

Offline

#2864 2017-01-19 09:36:03

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

Re: Post your handy self made command line utilities

Raynman wrote:

You can also use a name instead of an id. I suspect that would be less prone to change. And why cram the whole thing into a single line? How about

#!/bin/bash

# name or id that might change in the future
touchpad=12

if [[ $(xinput list-props "$touchpad" | awk '/Device Enabled/ { print $4 }') == 0 ]]; then
  action=enable
else
  action=disable
fi
xinput $action "$touchpad" && notify-send "touchpad ${action}d"

Now I propose putting the final line into a function instead, so the next poster can finally sit down a while and patch bash with an OO component.

Offline

#2865 2017-01-19 09:54:22

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

as you wish

#!/bin/bash

#xinput device id for touchpad
touchpad=12

# enable touchpad
ON() {
xinput enable $touchpad && notify-send "touchpad enabled"
}

# disable touchpad
OFF() {
xinput disable $touchpad && notify-send "touchpad disabled"
}

# get status of touchpad
status=$(xinput list-props $touchpad | awk '/Device Enabled/ { print $4 }')

# toggle touchpad status
[[ $status == 0 ]] && ON || OFF

Last edited by Docbroke (2017-01-19 09:55:39)


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2866 2017-01-19 16:41:46

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

Re: Post your handy self made command line utilities

Bash does not have a ternary operator, so "&& foo || bar" will have unpredictable results. For example, if "ON" returns false for some reason, "OFF" would still be run.

{ echo 1; true; } && { echo 2; false; } || { echo 3; }                                                                     
1
2
3

P.S. That said, please don't post another copy with those changes, we all know what it looks like by now...

Last edited by Alad (2017-01-19 16:42: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

#2867 2017-01-19 16:50:08

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

Re: Post your handy self made command line utilities

This has come up before - and using that pseudo-ternary operator is perfectly appropriate in cases like these.  If the first conditional is false, the third command will run.  The only problem is if the first conditional is true and the second command fails, then the third command will also run.  But that will have no side-effect here.  If the second command fails, the intended behavior of turning it ON has already failed and it is currently OFF, thus running another command to try to turn it OFF will have no effect.

The logic of the statment is "If it's on, turn it off" and "If it's off, turn it on (and if turning it on fails, go ahead and try to turn it off again)."


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

Offline

#2868 2017-01-19 16:52:51

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

Re: Post your handy self made command line utilities

Well, you could equally say that there's no need to quote variables because "they won't have white space in them". In the end, you add an unexpected factor to your code which might bite you later. If you really feel like keeping everything on one line, you can seperate if/then with semi-colons.

if foo; then bar; else baz; fi

Last edited by Alad (2017-01-19 16:54:54)


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

#2869 2017-01-19 16:54:36

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

Re: Post your handy self made command line utilities

No, because variables are by definition variable.  They could include anything at some later point.  This is not self modifying code - we know a priori what it includes.


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

Offline

#2870 2017-01-19 16:55:10

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

Re: Post your handy self made command line utilities

Alad wrote:

Bash does not have a ternary operator

Awk does.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2871 2017-01-19 16:56:41

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

Re: Post your handy self made command line utilities

No, because variables are by definition variable.  They could include anything at some later point.  This is not self modifying code - we know a priori what it includes.

That was just an example. The point is that I wouldn't want to spend extra brain cycles into figuring out if some side effect might or might not manifest, or be relevant in a particular scenario. Especially later on when I completely forgot what my code does.

edit: looks like there were some edits in the meanwhile... I'll pass on painting the bikeshed further.

Last edited by Alad (2017-01-19 17:03:48)


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

#2872 2017-01-20 14:00:24

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

Re: Post your handy self made command line utilities

Continuing on the thread, list all optional dependencies that are not installed:

#!/bin/bash
join -v2 -o2.2,2.1 <(expac -Q '%n' | sort) <(expac -Qv '%n\t%o' -l '\t' | awk '$2 !~ /None/ { 
	for(i = 2; i <= NF; ++i) printf("%s\t%s\n", $i, $1) | "sort -k1,1"
}') | sort | column -t

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

#2873 2017-01-20 14:27:49

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,168

Re: Post your handy self made command line utilities

how about making this readable (less crammed)


Arch is home!
sway rofi qutebrowser vifm vim mutt lizzy pass

Offline

#2874 2017-01-20 14:42:08

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

Re: Post your handy self made command line utilities

k

#!/bin/bash
pair_fields() {
	awk '$2 !~ /None/ { 
		for(i = 2; i <= NF; ++i) {
			printf("%s\t%s\n", $i, $1)
		}
	}'
}

join -v2 -o2.2,2.1 <(expac -Q '%n\n%S' -l '\n' | sort) \
	<(expac -Qv '%n\t%o' -l '\t' | pair_fields | sort -k1,1) | sort | column -t

Last edited by Alad (2017-01-20 15:25: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

#2875 2017-01-20 14:51:23

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

Re: Post your handy self made command line utilities

Note that piping within awk is generally slower than piping it's output.  I've confirmed that in this case.  Also piping outside awk seems far more readable to me.


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

Offline

Board footer

Powered by FluxBB