You are not logged in.

#2726 2016-03-17 23:53:00

awesome8x
Member
Registered: 2016-01-31
Posts: 7

Re: Post your handy self made command line utilities

Trilby,

Yeah, turns out that and the "--unneeded" flag were what I was looking for. Thank you, and sorry for posting a useless script.

Offline

#2727 2016-03-18 12:46:31

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Post your handy self made command line utilities

awesome8x wrote:

Yeah, turns out that and the "--unneeded" flag were what I was looking for. Thank you, and sorry for posting a useless script.

It wasn't useless. You learned something and I learned something. smile

Offline

#2728 2016-03-20 03:51:53

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

Re: Post your handy self made command line utilities

As usual, iBus was not working as advertised.

gnome-control-center keyboard sets the keys (Shift)+Super+Space to cycle input sources--but nothing recognizes the keys, whatever is set. Gnome-shell might be expected to make the connection somehow. However, iBus does recognize its own configurable key, set by ibus-setup.

I don't know what GNOME devs have in mind with two places to configure keys for the same purpose and one that doesn't work but, whatever, making my own solution (again).

ibus-toggle

#! /bin/bash
if [ -n "$(pidof ibus-daemon)" ]; then
  ibus exit
else
  ibus-daemon -drx
fi

To emulate the intended behavior, fix a few things in gnome-control-center keyboard:
01_gcc_keyboard.png
Disable non-functional (Shift)+Super+Space shortcuts:
03_keyboard_shortcuts_disable_broken.png
Add a new custom shortcut to toggle iBus:
04_keyboard_shortcuts_add_toggle.jpg

Cycle individual input methods with the key set by ibus-setup:
05_ibus_setup_set_next_im.jpg

ibus-toggle also comes in handy with those applications that do not recognize iBus's input.

Last edited by quequotion (2016-03-20 12:13:04)

Offline

#2729 2016-03-20 10:31:35

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

Re: Post your handy self made command line utilities

simple script to toggle touchpad on/off, (used with keybinding to Fn+F5)

#!/bin/bash

pad=$(synclient | grep TouchpadOff | awk -F= '{print $2}')

if [ ${pad} == 0 ]; then synclient TouchpadOff=1
fi

if [ ${pad} == 1 ]; then synclient TouchpadOff=0
fi

Offline

#2730 2016-03-20 11:10:49

Diraso
Member
From: Germany
Registered: 2013-07-20
Posts: 9

Re: Post your handy self made command line utilities

I use a similar script, but i also move the cursor out of view / back to the center of the screen.

#!/bin/bash

TPSTATUS=$(synclient -l | grep -c 'TouchpadOff.*=.*0')

# Move mouse out of the way, if xdotool is present
if [[ -x /usr/bin/xdotool ]]; then
    if [[ $TPSTATUS -eq 0 ]]; then
        /usr/bin/xdotool mousemove --polar 0 0
    else
        /usr/bin/xdotool mousemove --polar 135 5000
    fi
fi

synclient TouchpadOff=$TPSTATUS

Offline

#2731 2016-03-20 11:36:26

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

Re: Post your handy self made command line utilities

Docbroke wrote:

simple script to toggle touchpad on/off, (used with keybinding to Fn+F5)

You can do it all in awk (trackpad-toggle)

#!/bin/sh
synclient TouchpadOff=$(synclient -l | awk '/TouchpadOff/ {print !$3}') 

grep -c as ^ works too.

Last edited by Alad (2016-03-20 11:38:54)


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

Offline

#2732 2016-03-20 11:58:26

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

Re: Post your handy self made command line utilities

that's awksome!
I'm just taking first baby-steps in bash-world!

Offline

#2733 2016-03-20 12:30:14

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

Re: Post your handy self made command line utilities

This isn't bash though, it's using ! to negate the value under $3 in awk, thus if the value is 0, it returns 1, and if it's 1 (or anything non-zero) it returns 0.  (NB. Awk also uses $1, $2, .., $n notation but for fields.)

PS: See http://wiki.bash-hackers.org/syntax/expansion/cmdsubst

Last edited by Earnestly (2016-03-20 12:33:26)

Offline

#2734 2016-03-20 15:52:10

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,768

Re: Post your handy self made command line utilities

Here is my tocuchpad toggle, with notifications

#! /usr/bin/bash

foo=$( synclient | grep TouchpadOff | grep 0 )
var=$?
if [ $var -eq  0 ]
then 
   notify-send "Touchpad State" off
   synclient TouchpadOff=1
else
   notify-send "Touchpad State" on
   synclient TouchpadOff=0
fi

I bind it to alt-space.   Seems we all have the same problem smile

Last edited by ewaller (2016-03-20 15:52:43)


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

Online

#2735 2016-03-20 15:57:23

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

Re: Post your handy self made command line utilities

ewaller wrote:

Seems we all have the same problem smile

Not me.  In my xinitrc:

synclient TouchpadOff=1

Problem solved wink

(I love trackpoints.)


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

Online

#2736 2016-03-22 17:10:52

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Post your handy self made command line utilities

Earnestly wrote:

This isn't bash though, it's using ! to negate the value under $3 in awk, thus if the value is 0, it returns 1, and if it's 1 (or anything non-zero) it returns 0.  (NB. Awk also uses $1, $2, .., $n notation but for fields.)

988bfec95b678436b3d5ad0999b952f0.jpg
I can't believe I actually understand and used awk at some point in my life. It's an amazing and super powerful language but... I think it took, like, 3 weeks for me to understand it and only 1 week for me to forget everything I learned. sad

Offline

#2737 2016-03-22 23:00:32

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

Re: Post your handy self made command line utilities

I can't believe I actually understand and used awk at some point in my life. It's an amazing and super powerful language but... I think it took, like, 3 weeks for me to understand it and only 1 week for me to forget everything I learned.

info gawk

also: https://www.youtube.com/watch?v=Sg4U4r_AgJU

Last edited by Alad (2016-03-22 23:00:42)


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

Offline

#2738 2016-03-23 06:44:03

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

Re: Post your handy self made command line utilities

I got some inspiration from "grep -c" usage and created one more script to run slock on lid-close. Here is the script

#!/bin/bash

# LID=$(cat /proc/acpi/button/lid/LID0/state | awk '{print $2}')


LID=$(cat /proc/acpi/button/lid/LID0/state | grep -c 'state:.*open')

if [ ${LID} == 0 ]; then slock
fi

and here is the service file

[Unit]
Description=slock on lid close

[Service]
Type=simple
User=sharad
Environment=DISPLAY=:0
ExecStart=/home/sharad/bin/lid
Restart=always
RestartSec=1

I am almost sure that I am not doing this right way, but it works, and I am happy to avoid acpid/xss-lock/xautolock etc! Suggestions are welcome!

EDIT: oops! the script is alright but service is wrongly configured, as it will keep triggering slock every second, when lid is closed, needs some improvement.

EDIT2: using this script with conky, systemd-service file posted above fails after single execution of slock!

Last edited by Docbroke (2016-03-23 09:59:30)

Offline

#2739 2016-03-23 06:53:36

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

Re: Post your handy self made command line utilities

Spare the feline:

# LID=$(awk '{print $2}' /proc/acpi/button/lid/LID0/state)
LID=$(grep -c 'state:.*open' /proc/acpi/button/lid/LID0/state)

Simpler:

(( LID = 0)) && slock

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2740 2016-03-23 07:26:21

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

Re: Post your handy self made command line utilities

yeh spared the feline !

however "(( LID = 0)) && slock" didn't work

EDIT: worked after changing operator to == as suggested in next post

Last edited by Docbroke (2016-03-23 10:02:28)

Offline

#2741 2016-03-23 07:34:51

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

Re: Post your handy self made command line utilities

My bad: the operator is ==.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2742 2016-03-23 10:20:40

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

Re: Post your handy self made command line utilities

So you've learned of grep -c, but apparently not grep -q:

grep -q 'state:.*open' /proc/acpi/button/lid/LID0/state || slock

This would be a bit better with a different match and a && instead of || - is the alternative to .*open *closed?  If so the following would be best:

grep -q 'state:.*closed' /proc/acpi/button/lid/LID0/state && slock

The reason being the first version with the || will run slock if grep fails for some unexpected reason like that file not existing or being accessible.


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

Online

#2743 2016-03-23 12:08:56

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

Re: Post your handy self made command line utilities

People hardcoding DISPLAY variables gets a bit old, at least for those who use multiple display servers. I use this:

#!/bin/bash

pid=$(pgrep -t tty$(fgconsole) xinit)
pid=$(pgrep -P $pid -n)

import_environment() {
    (( pid )) && for var; do
        IFS='=' read key val < <(egrep -z "$var" /proc/$pid/environ)

        printf -v "$key" %s "$val"
        [[ ${!key} ]] && export "$key"
    done
}

import_environment XAUTHORITY USER DISPLAY

xscreensaver-command -activate
xscreensaver-command -lock

Assumes you use xinit to start your WM. /usr/lib/systemd/system-sleep snippet:

#!/bin/bash
case $1/$2 in
	pre/*) xscreensaver-asroot ;;
esac

If you want it only to happen when you close the lid, use grep as above, or:

[[ $(awk '$2 ~ /closed/' /proc/acpi/button/lid/LID0/state) ]] && ... 

Last edited by Alad (2016-03-23 12:32:38)


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

Offline

#2744 2016-03-23 12:15:56

Awebb
Member
Registered: 2010-05-06
Posts: 6,282

Re: Post your handy self made command line utilities

Do I come from another dimension, or is the -q switch redundant in this instance? If I'm not still with a fever from that damn flue, grep returns true on a find and false on no finds and/or errors.

Offline

#2745 2016-03-23 13:43:19

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: Post your handy self made command line utilities

Awebb wrote:

Do I come from another dimension, or is the -q switch redundant in this instance? If I'm not still with a fever from that damn flue, grep returns true on a find and false on no finds and/or errors.

grep without the -q switch would also print the matching line(s).

Offline

#2746 2016-03-23 13:50:26

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

Re: Post your handy self made command line utilities

read -r _ state < /proc/acpi/button/lid/LID0/state

case $state in
    ...
esac

Why overcomplicate everything?

Edit: Although just using grep's exit status is better if that's all you need

Last edited by Earnestly (2016-03-23 13:54:19)

Offline

#2747 2016-03-23 13:51:45

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

Re: Post your handy self made command line utilities

@Trilby, thanks for "grep -q" idea, and yeh, closed is the another alternnative scenario for lid.

Offline

#2748 2016-04-10 12:33:00

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: Post your handy self made command line utilities

karol wrote:

Have a look at expac.

Have a look at the title of this topic

Anyway, I just wrote a simple wrapper for applications that don't support multi-threading. This example encodes flac to aac, but it can really be used for anything.

#!/bin/bash
set -e; cd `dirname $0`
MAXTHREADS=6

t=()
while read -d '' p
do
	mkdir -p "aac.tmp/${p%/*}"
	flac -dc "$p" | fdkaac - -o"aac.tmp/$p" & t=(${t[*]} $!)

	[[ ${#t[*]} -lt $MAXTHREADS ]] && continue

	wait ${t[0]} && unset t[0]

done < <(find flac/ -type f -name \*.flac -print0)

# wait for running threads
while [[ ${#t[*]} -gt 0 ]]; do wait ${t[0]} && unset t[0]; done

It uses an array, and keeps the # of threads consistent by waiting for the first thread when the maximum # is reached. Works best when all sub-processes take roughly the same time

Last edited by Spider.007 (2016-04-10 12:38:24)

Offline

#2749 2016-04-30 23:07:20

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

Re: Post your handy self made command line utilities

#!/usr/bin/env bash
# Play subtitles for a film if they exist

movie="$1"
mdir="${movie%/*}"
name="${movie##*/}"

cd "$mdir"
for file in *; do
  if [[ ${file%.*} == ${name%.*} ]]; then
    title="${file%.*}"
    for match in "$title"*; do
      if [[ $match =~ @*.(ass|srt|sub) ]]; then
        subtitles="$match"
      fi
    done
  fi
done

if [[ -n $subtitles ]]; then
  mpv --subfile="$subtitles" "$name"
else
  printf "%s\n" "No subs found, playing film anyway..."
  mpv "$name"
fi

# vim:set sw=2 ts=2 et:

Prompted by this: http://unix.stackexchange.com/questions … nfig-files


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2750 2016-05-01 22:31:12

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

Re: Post your handy self made command line utilities

...

# Quotes not necessary here
movie="$1"
mdir="${movie%/*}"
name="${movie##*/}"

...

# It is a bit anal but naked globs should have ./ prefixed, i.e. for a in ./*
# See <http://www.dwheeler.com/essays/filenames-in-shell.html>
# Edit: Also consider using nullglob if you're using bash.
for file in *; do

  # The RHS of the if statement needs to be quoted.
  # See <http://mywiki.wooledge.org/Quotes#When_Should_You_Quote.3F>
  if [[ ${file%.*} == ${name%.*} ]]; then

    # Unnecessary quoting here
    title="${file%.*}"

        # And here
        subtitles="$match"

# No need for -n here
if [[ -n $subtitles ]]; then

...

Last edited by Earnestly (2016-05-01 22:36:27)

Offline

Board footer

Powered by FluxBB