You are not logged in.

#2951 2017-03-10 09:50:10

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

Re: Post your handy self made command line utilities

Awebb wrote:

Yay, another shell! Infinite diversity in infinite combinations. Surak is pleased.

Remember 90's TVs with Picture-in-Picture?
Screenshot_from_2017_03_10_18_39_01.png

I altered pulse.out, to get $app by clicking a window and $out from a xenity radiolist. The one thing I could never get to work with PIP was a split audio output for the subscreen. Now that is easy.

Last edited by quequotion (2017-03-10 11:37:35)

Offline

#2952 2017-03-10 10:17:14

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

Re: Post your handy self made command line utilities

quequotion wrote:
Awebb wrote:

Yay, another shell! Infinite diversity in infinite combinations. Surak is pleased.

Remember 90's TVs with Picture-in-Picture?

I used to complain about the lack of even more splits, because I wanted to watch all the shows.

Online

#2953 2017-03-10 11:00:50

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

Re: Post your handy self made command line utilities

I thought we were supposed to have that two years ago.


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

Offline

#2954 2017-03-10 11:40:24

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

Re: Post your handy self made command line utilities

Trilby wrote:

I thought we were supposed to have that two years ago.

Where are the flying cars?

Awebb wrote:

I wanted to watch all the shows.

Screenshot_from_2017_03_12_10_55_26.png

Last edited by quequotion (2017-03-12 01:58:33)

Offline

#2955 2017-03-10 22:40:18

escondida
Package Maintainer (PM)
Registered: 2008-04-03
Posts: 157

Re: Post your handy self made command line utilities

Awebb: Well, as of 1989, yes (-:

Offline

#2956 2017-03-14 07:37:24

ankitrgadiya
Member
From: India
Registered: 2016-07-08
Posts: 9
Website

Re: Post your handy self made command line utilities

I do have a Github Repository with all the scripts I wrote which can be used by others.
Link


From: Ankit R Gadiya <https://ankitrgadiya.in>

Offline

#2957 2017-03-15 19:17:38

erikw
Member
From: Lund, Sweden
Registered: 2012-07-17
Posts: 17
Website

Re: Post your handy self made command line utilities

I would like to share some handy shell functions that I use daily. I find these very useful and I think many more would enjoy these!

The always-up-to-date versions can be found in my dotfiles@Github!

git root

Git overlay which adds a command to cd to the root of a git repo.
Example:
$ cd /git-repo
$ cd some/sub/dir
$ git root
$ # back in /git-repo now

git() {
	local gitbin=/usr/bin/git
	if [ "$1"  = root ]; then
		local root="$($gitbin rev-parse --show-cdup)"
		[ -n "$root" ] && cd "$root"
	else
		$gitbin $*
	fi
}
viack & vigrep

Wrapper around the classic 'vi with ack' with search highlighting, i.e. open files matching a given search term by ack in vim.
Example: $ viack -i search_term
Last argument is the search string. Preceding arguments are for ack.

viack() {
	local search
	# Get the last argument. Source: http://stackoverflow.com/questions/1853946/getting-the-last-argument-passed-to-a-shell-script
	for search; do true; done
	vim -p $(ack -l $@) -c 1 +/$search  # Go to first line then search downwards.
}

# Wrapper around the classic 'vi with grep'.
vigrep() {
	local search
	for search; do true; done
	vim -p $(grep -Rl $@) -c 1 +/$search
}
sourceifexists

Source file if it exists. Usefull for shell startup scripts used across many systems.
Example: sourceifexist $HOME/src/someproj/init.sh

sourceifexists() {
	[ -f "$1" ] && [ -r "$1" ] && . "$1"
}
cdw

cd into the directory containing the program found in PATH.
Example: $ cdw some_script.sh

cdw() {
	local prog="$1"
	type "$prog" &>/dev/null
	if [ "$?" -ne 0 ]; then
		echo "\"${prog}\" not found in \$PATH." >&2
	else
		dir=$(dirname $(which "$prog"))
		echo "$dir"
		cd "$dir"
	fi
}
Time conversions

The function names says it all...

epoch2iso8601() {
    local epoch="$1"
    date -d @$epoch +%Y-%m-%dT%H:%M:%S%z
}

iso86012epoch() {
    local iso="$1"
    date -d "$iso" +%s
}

pz

Last edited by erikw (2017-03-15 19:20:36)

Offline

#2958 2017-03-15 21:35:22

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,421

Re: Post your handy self made command line utilities

type "$prog" &>/dev/null ??
-edit
got it.

Last edited by kokoko3k (2017-03-15 21:38:27)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#2959 2017-03-15 21:41:34

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

Re: Post your handy self made command line utilities

It checks the value of $? on the following line, so it's a complicated way to do if type foo; then bar; fi. Since we seem to be interested in PATH, type -P is also more suited (and >/dev/null 2>&1 is less ambiguous, see http://wiki.bash-hackers.org/scripting/obsolete).

Other things to nitpick at: [ (use [[), $* (use "$@"), do true (???), echo (use printf), assorted quoting errors

Last edited by Alad (2017-03-15 21:43:12)


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

Offline

#2960 2017-03-16 01:35:51

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

Re: Post your handy self made command line utilities

If [[ -r "$1" ]] is true [[ -f "$1" ]] is implied, no need to test both.

Offline

#2961 2017-03-16 01:44:18

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

Re: Post your handy self made command line utilities

None of those conditionals do much anyhow.  Just do "source $1" (or ". $1") and redirect stderr if you want and it will have the same result.  If $1 is not set, `source` will fail and return an error.  If $1 is set but not a file, `source` will fail and return an error.  If $1 is a file but not readable, `source` will fail and return an error.  `source` will only successfully source $1 if $1 is set, is a file, and is readable - so the checks are all redundant.


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

Offline

#2962 2017-03-16 02:02:33

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

Re: Post your handy self made command line utilities

Docbroke wrote:

If [[ -r "$1" ]] is true [[ -f "$1" ]] is implied, no need to test both.

No, it's not.

$ [[ -r /dev/fd/1 ]]
$ echo $?
0
$ [[ -f /dev/fd/1 ]]
$ echo $?
1

But I agree to just let Bash deal with the errors.


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

Offline

#2963 2017-03-17 19:52:01

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

Re: Post your handy self made command line utilities

If anyone can use this:

Needs community/linuxtv-dvb-apps
https://www.linuxtv.org/docs/libdvbv5/index.html

Simple tv-tuner and recorder using dvbv5-zap
Use dvbv5-scan to make a channels.conf, or make one in this format

[49.1]
	VCHANNEL = 49.1
	SERVICE_ID = 3
	VIDEO_PID = 49
	AUDIO_PID = 52
	FREQUENCY = 213028615
	MODULATION = VSB/8
	DELIVERY_SYSTEM = ATSC

[49.2]
	VCHANNEL = 49.2
	SERVICE_ID = 4
	VIDEO_PID = 65
	AUDIO_PID = 68
	FREQUENCY = 213028615
	MODULATION = VSB/8
	DELIVERY_SYSTEM = ATSC

[49.3]
	VCHANNEL = 49.3
	SERVICE_ID = 5
	VIDEO_PID = 81
	AUDIO_PID = 84
	FREQUENCY = 213028615
	MODULATION = VSB/8
	DELIVERY_SYSTEM = ATSC

[49.4]
	VCHANNEL = 49.4
	SERVICE_ID = 6
	VIDEO_PID = 97
	AUDIO_PID = 100
	FREQUENCY = 213028615
	MODULATION = VSB/8
	DELIVERY_SYSTEM = ATSC
    
[49.5]
	VCHANNEL = 49.5
	SERVICE_ID = 7
	VIDEO_PID = 113
	AUDIO_PID = 116
	FREQUENCY = 213028615
	MODULATION = VSB/8
	DELIVERY_SYSTEM = ATSC

 
I put one ATSC channel with 5 streams as the example. All channels follow the
same format as above. You don't even need to scan for channels if you don't want.
You need to find the center frequency for each channel that you put in your
.conf file and follow the above example for the other numbers.
The channel identifier can be anything [49.1] [WABC] [CH1], it will list in
the script that way.

#! /usr/bin/env bash

# dvb:// tuner-player. Requires dvbv5-zap and a dvb_channel.conf 
# made with dvbv5-scan. One of mplayer, mpv or ffplay.

#Path to channels.conf
conf="$HOME/.mplayer/dvb_channel.conf"
#Get channels ID's from conf
channels=$(grep -oP '\[\K[^\]]+' < "$conf")

PS3="
Enter channel number: "

message="
Select channel then adjust antenna for best signal quality while 
watching signal meter. Press (enter) to play that channel once 
good signal is acquired.					  	
During play press (q) to quit station, (f) for fullscreen.					 	
-----------------------------------------------------------
Select a channel.   Ctrl C to exit.
"

while :; do
    clear
    echo "$message"
    select opt in $channels; do
        dvbv5-zap -ss -r -c "$conf" "$opt" &
        read
        clear
        echo "Wait I'm tuning "$opt""
        mplayer -vf scale=720:480 -cache 4096 \
            -cache-min 80 /dev/dvb/adapter0/dvr0 &> /dev/null
	#mpv --vf=scale=720:480 /dev/dvb/adapter0/dvr0 &> /dev/null
	#ffplay /dev/dvb/adapter0/dvr0 &> /dev/null
	pkill dvbv5-zap
	break
    done
done
#! /usr/bin/env bash

# dvb:// tuner-recorder. Requires dvbv5-zap and a dvb_channel.conf 
# made with dvbv5-scan

#Path for output file
cd $HOME/tel
#Path to channels.conf
conf="$HOME/.mplayer/dvb_channel.conf"
#Get channels ID's from conf
channels=$(grep -oP '\[\K[^\]]+' < "$conf")

PS3="
Enter channel number: "

message="$(tput setaf 3)
Select channel then adjust antenna for best signal quality 
while watching signal output. Press (enter) to record that
channel once good signal is acquired. (enter) again to stop.
-----------------------------------------------------
Select a channel.   Ctrl C to exit.$(tput sgr0)
"

while :; do
	clear
        echo "$message"
	select opt in $channels; do
            #No Overwrite
            num=1
            until [ ! -e tv$num.ts ]; do
                num=$(( $num + 1 ))
            done
            #Output to tv"$num".ts
            dvbv5-zap -ss -r -c "$conf" "$opt" -o tv"$num".ts &
	    read
	    pkill dvbv5-zap
	    break
	done
done

Edit: Formatting on indents

Last edited by teckk (2017-03-17 19:58:39)

Offline

#2964 2017-03-19 12:36:25

erikw
Member
From: Lund, Sweden
Registered: 2012-07-17
Posts: 17
Website

Re: Post your handy self made command line utilities

@Alad: Thanks for feedback. type -P seems to be a bash feature though (I use zsh).

Last edited by erikw (2017-03-19 12:36:35)

Offline

#2965 2017-03-19 15:45:47

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

Re: Post your handy self made command line utilities

Then use command -V, which works in any POSIX shell.


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

Offline

#2966 2017-03-20 06:20:10

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

Re: Post your handy self made command line utilities

Added some more logic to my previous script for running wpa_supplicant in loop. Now it automatically starts mobile_broadband if my wifi-network is not available. Note: there is no use of sudo, now.

crontab (for root user)

## start and keep connected to wifi at boot ##
@reboot /home/sharad/bin/netstart
* * * * * [[ ! -f /tmp/stopwifi ]] && /home/sharad/bin/netstart

netstart script

#!/bin/bash

connectbsnl() {
    touch /tmp/stopwifi			    ## stop cronjob 
    killall wpa_supplicant		    ## avoid polluting conky output
    pon bsnl
}

restartwifi() {
    if [[ "$(pidof wpa_supplicant)" ]]; then 
	wpa_cli reassociate 
    else
	wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
    fi
    [[ ! "$(pidof dhcpcd)" ]] && dhcpcd -4 wlan0

    sleep 5
    # if wifi-network (hotspot) is not available start mobile_broadband
    # comment-out below line when using other wifi-networks
    wpa_cli scan && wpa_cli scan_results | grep hotspot >/dev/null || connectbsnl
}

[[ -f /tmp/stopwifi ]] && rm /tmp/stopwifi  ## starts cronjob manually, avoids use of sudo
[[ "$(pidof pppd)" ]] && poff -a	    ## kill mobile_broadband if running (when started manually)

ping -c 1 8.8.8.8 || restartwifi

Offline

#2967 2017-03-22 19:42:04

progandy
Member
Registered: 2012-05-17
Posts: 5,263

Re: Post your handy self made command line utilities

This is a small oneliner to check if you need to reboot after a kernel upgrade.

alias kernel-check="[[ -f \"/proc/modules\" && ! -d \"/usr/lib/modules/$(uname -r)\" ]] && printf '==> WARNING: %s\n  -> %s\n' 'Running kernel has been updated or removed.' 'Reboot in the new kernel is required.'"

I run the same as a pacman hook:
/etc/pacman.d/hooks/99-z-kernel-reboot.hook

[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = File
Target = usr/lib/modules/*

[Action]
Description = Check for upgrade of running kernel
When = PostTransaction
Exec = /bin/bash -c "[[ -f \"/proc/modules\" && ! -d \"/usr/lib/modules/$(uname -r)\" ]] && printf '==> WARNING: %s\n  -> %s\n' 'Running kernel has been updated or removed.' 'Reboot in the new kernel is required.'"

| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#2968 2017-03-22 20:05:28

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Post your handy self made command line utilities

Docbroke wrote:

[[ ! "$(pidof dhcpcd)" ]] && ...

That is strange-looking logic. I thik it would be better as:

pidof dhcpcd > /dev/null || ...

Offline

#2969 2017-03-23 14:01:10

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

Re: Post your handy self made command line utilities

brebs wrote:
Docbroke wrote:

[[ ! "$(pidof dhcpcd)" ]] && ...

That is strange-looking logic. I thik it would be better as:

pidof dhcpcd > /dev/null || ...

I'm curious which performs better. How would one go about testing if either is faster?

Offline

#2970 2017-03-23 14:15:53

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

Re: Post your handy self made command line utilities

time might show the difference.   To get enough significant digits, you might need to iterate a few times .


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

#2971 2017-03-23 15:35:09

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

Re: Post your handy self made command line utilities

Breb's version is *definitely* faster.  However it is likely so trivial of a difference as to not show up without many iterations.  But there's no way your version could be faster.  The simplification of the boolean logic might not make much difference (!A && B -> A || B) but Brebs version is cleaner in that regard.  More importantly, though, it doesn't spawn an additional subshell.


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

Offline

#2972 2017-03-25 04:07:50

Hairyplotter
Member
Registered: 2017-03-25
Posts: 4

Re: Post your handy self made command line utilities

I love using the command line and I find Midnight Commander to be an indispensable tool. Having migrated from Ubuntu / Mint, I was use to mc dropping me to the current active directory on exit. After switching to Arch, I lost that functionality. For whatever reason /usr/lib/mc/mc-wrapper.sh didn't work as it was suppose to. After playing around I finally regained a feature that I find quite valuable, I created an alias that performed the same function as mc-wrapper.sh.

alias mc='mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"; DIR=$(cat /tmp/mc-$USER/mc.pwd.$$); cd "$DIR"; \rm -f /tmp/mc-$USER/mc.pwd.$$; unset DIR'

mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"    # This starts mc while creating a temporary file to hold the last working directory on exit. the "~ "$PWD"" loads the users home directory in the left pane and current working directory (Where mc was started) in the right pane. If you want mc to start with both panes pointing at the current working directory, remove the  ~ "$PWD".

DIR=$(cat /tmp/mc-$USER/mc.pwd.$$)    # This copies the contents for the temporary file /tmp/mc-$USER/mc.pwd.$$ in to an environment variable DIR.

cd "$DIR"   # Changes to the directory pointed to by $DIR, which should be the current working directory when exiting mc.

\rm -f /tmp/mc-$USER/mc.pwd.$$   # This removes the temporary file by calling /usr/bin/rm, bypassing any alias definitions you may have called rm. Remove the \ if you have an alias called rm that you want used instead of calling the binary directly.

unset DIR   # This removes the variable DIR from the environment.


I hope this helps someone.
Not being familiar with the community yet, I overly explained this simple alias for those that may not understand. If I went overboard, I apologize in advance.

Offline

#2973 2017-03-25 12:00:30

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

Re: Post your handy self made command line utilities

There's really no need for the DIR variable.  First simplification:

alias mc='mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"; cd $(cat /tmp/mc-$USER/mc.pwd.$$); \rm -f /tmp/mc-$USER/mc.pwd.$$'

You can also get rid of the cat and just have the shell read the contents of the file:

alias mc='mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"; cd $(</tmp/mc-$USER/mc.pwd.$$); \rm -f /tmp/mc-$USER/mc.pwd.$$'

EDIT: I'd also second the below comment.  This does seem like an odd workaground.  My coments were just to streamline what was there - I don't use mc.


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

Offline

#2974 2017-03-25 12:00:49

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

Re: Post your handy self made command line utilities

idk, the wrapper works for me. You probably want to investigate why it doesn't work for you rather than come up with an ad-hoc replacement.

Last edited by Alad (2017-03-25 12:01:46)


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

Offline

#2975 2017-03-25 14:30:10

Hairyplotter
Member
Registered: 2017-03-25
Posts: 4

Re: Post your handy self made command line utilities

Trilby wrote:

There's really no need for the DIR variable.  First simplification:

alias mc='mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"; cd $(cat /tmp/mc-$USER/mc.pwd.$$); \rm -f /tmp/mc-$USER/mc.pwd.$$'

You can also get rid of the cat and just have the shell read the contents of the file:

alias mc='mc -P "/tmp/mc-$USER/mc.pwd.$$" ~ "$PWD"; cd $(</tmp/mc-$USER/mc.pwd.$$); \rm -f /tmp/mc-$USER/mc.pwd.$$'

EDIT: I'd also second the below comment.  This does seem like an odd workaground.  My coments were just to streamline what was there - I don't use mc.

There actually is a need for the $DIR variable. If you try to change in to a directory containing spaces with the refinements you posted you get : bash: cd: too many arguments.

Alad wrote:

idk, the wrapper works for me. You probably want to investigate why it doesn't work for you rather than come up with an ad-hoc replacement.

Normally I would agree, except it doesn't work for me.
For the record, I did investigate. The temp file is created and populated with the correct directory. It passes all of the tests, at which point it executes the cd command without generating any errors but doesn't change to the directory. I thought it would be cleaner and easier to just make an alias that works and not run the risk of it breaking if mc is ever updated. Since editing the script itself would be the only way to get it to work.

Offline

Board footer

Powered by FluxBB