You are not logged in.

#3226 2019-01-27 23:48:27

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

Re: Post your handy self made command line utilities

A little bit better scripts for that device.

#! /usr/bin/bash

#Firewall log printer for Arris BGW210-700
#Prints log to terminal and logs to file continuously until Ctrl C
#Needs curl, awk, comm

#BGW210 Gateway LAN IP
url="http://172.16.0.1"

#Firewall log
fw_log="f.txt"

#Retrieve interval for log 
sleep_time=60

#Print char x times
str=$(printf "%90s")

#Get log data from BGW210, get data between td tags with awk
Get_Log() {
    curl -s "$url"/cgi-bin/logs.ha | 
        awk -v OFS='\t' -F '[<|>]' '/td/{
            print $9, $13, $17, $21, $25}' | 
                tail -n 30 #| grep 'Policy (filtersets, etc.)'
                            #| grep '172.16.0.30'
                            #| sort -k 2,2 -k 1,1
                            #| grep 'UDP'
                            #| grep 'TCP
}

#Get active devices
Active() {
    curl -s "$url"/cgi-bin/home.ha |
        awk -v OFS='\t' -F '[<|>]' '/td scope="row" class="col2"/{
            print $5, $9, $13, $17, $19}'
}

#Main loop. Get logs, compare, print diff.
b=""
while :; do
    a=$(Get_Log) 
    echo "Tail of firewall log:"    
    comm --nocheck-order -13 <(echo "$b") <(echo "$a") | tee -a fw_log
    echo "Update in "$sleep_time" seconds:"
    echo "${str// /─}"
    sleep "$sleep_time"
    
    act=$(Active)
    echo "Active devices on network:"
    echo "$act" | awk '{gsub(/&nbsp;/,""); print}'
    echo "Update in "$(($sleep_time * 2))" seconds:"
    echo "${str// /─}"
    
    b=$(Get_Log) 
    echo "Tail of firewall log:"  
    comm --nocheck-order -13 <(echo "$a") <(echo "$b") | tee -a fw_log
    echo "Update in "$sleep_time" seconds:"
    echo "${str// /─}"   
    sleep "$sleep_time"  
done
#! /usr/bin/python

#Firewall log printer for Arris BGW210-700
#Prints log to terminal continuously until Ctrl C

import urllib.request
import lxml.html
from time import sleep

#BGW210 Gateway LAN IP
url = 'http://172.16.0.1'

#Retrieve interval for log 
sleep_time = 60

class GetBGW210Log():
    def __init__(self):
        #Get log data from BGW210 with urllib
        def Get_Log():
            req = urllib.request.Request((url)+'/cgi-bin/logs.ha')
            html = urllib.request.urlopen(req).read()
            root = lxml.html.fromstring(html)
            
            #Get data between td tags with lxml, append to list
            log = []
            for i in root.xpath('//td'):
                log.append(i.text_content())
                
            #Make rows out of items in log, append to new log
            logb = []
            def rows(data, rowsize): 
                while data:
                    row, data = data[:rowsize], data[rowsize:]
                    yield row
            for row in rows((log), 6):
                logb.append(row)
                
            #Return last 30 items in list
            return logb[-30:]
            
        #Get devices on LAN
        def Device():
            dlog = []
            req = urllib.request.Request((url)+'/cgi-bin/home.ha')
            html = urllib.request.urlopen(req).read()
            root = lxml.html.fromstring(html)
            
            log = []
            for i in root.xpath('//td[@class="col2"]'):
                log.append(i.text_content())
                
            def rows(data, rowsize): 
                while data:
                    row, data = data[:rowsize], data[rowsize:]
                    yield row
            for row in rows((log), 5):
                print (*row, sep='\t')
                
        #Main loop. Get logs, compare, print diff.
        blog = []        
        while True:
            alog= Get_Log()
            new1 = ([x for x in alog+blog if (x not in blog)])
            print ('Tail of firewall log:')
            for i in new1:
                print (*i, sep='\t')
            print ('Update in', (sleep_time), 'seconds:')
            print('─' * 90)
            sleep(sleep_time)
            
            print ('Active devices on network:')
            Device()
            print ('Update in', ((sleep_time) * 2), 'seconds:')
            print('─' * 90)
            
            blog = Get_Log()
            new2 = ([x for x in alog+blog if (x not in alog)])
            print ('Tail of firewall log:')
            for i in new2:
                print (*i, sep='\t')
            print ('Update in', (sleep_time), 'seconds:')
            print('─' * 90)   
            sleep(sleep_time)
        
if __name__ == "__main__":
    GetBGW210Log()

Offline

#3227 2019-02-12 16:14:42

bedtime
Member
Registered: 2019-02-12
Posts: 74

Re: Post your handy self made command line utilities

Automatically run YouTube video in VLC after copying the link.

Install xclip:

# pacman -S xclip

Run the script:

#!/bin/sh

# xclip needs the clipboard to be filled or it complains:
echo makexcliphappy | xclip -i -selection clipboard

while :;do
	
	# '-selection' parameter make take primary, secondary, or clipboard
	clip=$(xclip -o -selection clipboard)
	
	[ "$clip" != "$prevclip" ] && [ $(echo $clip | awk '/watch\?v=/||/youtu\.be/') ] && (vlc --play-and-exit $clip)&

	prevclip=$clip

	sleep 1

done

Now, when you want to watch a YouTube video in VLC just highlight and copy the main link, and it will load up in VLC player. smile

*** EDIT ***
Added code so xclip doesn't complain at startup due to empty clipboard.

Last edited by bedtime (2019-02-12 20:47:31)

Offline

#3228 2019-02-25 21:48:22

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

Re: Post your handy self made command line utilities

This niche-case monster is as much a hobby as a script. Its a television remote control made of bash and notify-send.
It can be used on the command line, but is meant for an analog interface. I use [Super]+[Play] for PWR, [Super]+{[Up] ,[Down]} for CH{+,-}, and [Super]+[R] for REC.

digitvctl:

#! /bin/bash
############################################################################## EXPLANATION
# This script contols an Earthsoft PT-series ISDB-T/S capture card
#
# The purpose is to provide functions that can be mapped to keys like a simple remote control (TODO: Graphical interface; android app?)
#
# Remote control fuctions (assign to hotkeys):
# digitvctl on                    = PWR button      (TV on/off,  DVR off)
# digitvctl chanup                = CH+ button      (channel up)
# digitvctl chandown              = CH- button      (channel down)
# digitvctl record/rec            = REC button      (DVR on/off)
# digitvctl recordandplay/recplay = REC+Play button (DVR&TV on/off)
#
# This also serves as a command-line frontend to digitvrec, geared toward VCR controls (TODO: Graphical interface; android app?)
#
# Recording functions work in a VCR way:
# All record modes feature simultaneous playback if watching TV.
# Record configured channel for default time to timestamped file
#
# digitvctl record/rec                                (REC)
#
# Record configured channel for default time to timestamped file
# digitvctl recordandplay/recplay                     (REC+Play)
#
# Record configured channel for "time (seconds)" to timestamped file
# digitvctl record(andplay)/rec(play) time            (REC(+Play) + timer)
#
# Record "channel" for "time (seconds)" to timestamped file
# digitvctl record(andplay)/rec(play) chan time       (REC(+Play) + CH + timer)
#
# Record "channel" for "time (seconds)" to file "title"
# digitvctl record(andplay)/rec(play) chan time title (REC(+Play) + CH + timer + title)
#
# Dependencies:
# recpt1, digitvrec, timeout, compatible player (tested with vlc, mplayer and mpv)
############################################################################## SETUP
# True when no config file exists (first run)
# Create config file (first channel; record 7200 seconds = 2 hours = 14.7GB file; Notification 0; spoof pid x3)
[[ ! -f ~/.digitvrc ]] && echo -e "14\n7200\n0\n99999\n99999\n99999\n" > ~/.digitvrc &

# True when no capture directory exists (first run)
# Create capture directory
[[ ! -d ~/Videos/Uncompressed\ Capture/ ]] && mkdir -p ~/Videos/Uncompressed\ Capture/ &

[[ -z ${EXPTIME} ]] && EXPTIME="1000"                # Default notification expiration (not all servers implement the timeout parameter)
[[ -z ${URGENCY} ]] && URGENCY="normal"            # Default notification urgency (notify-osd shows only critical if screensaver suspended)
[[ -z ${RECPRIORITY} ]] && RECPRIORITY="idleprio"    # Record at SCHED_IDLEPRIO (with BFS & schedtool) to avoid desktop lag
[[ -z ${PLAYPRIORITY} ]] && PLAYPRIORITY="isosched"  # Playback at SCHED_ISO (with BFS & schedtool) for near-realtime streaming
[[ -z ${PLAYEROPTS} ]] && PLAYEROPTS="-fs --hwdec vdpau --vo=vdpau --audio-channels=fl-fr-bl-br-fc-lfe --profile=pseudo-gui -- "           # Custom player options
[[ -z ${PLAYERCMD} ]] && PLAYERCMD="mpv"             # Compatible player
[[ -z ${DIGITVCMD} ]] && DIGITVCMD="recdvb"          # recpt1 or derivative earthsoft controller
[[ -z ${DIGITVOPTS} ]] && DIGITVOPTS="--b25 --strip --dev 1" # Custom "${DIGITVCMD}" string
[[ -z ${DIGITVCPID} ]] && DIGITVCPID="$(sed -n '4{p;q;}' ~/.digitvrc)"
[[ -z ${PLAYERPID} ]] && PLAYERPID="$(sed -n '5{p;q;}' ~/.digitvrc)"
[[ -z ${RECPID} ]] && RECPID="$(sed -n '6{p;q;}' ~/.digitvrc)"
############################################################################## LOGIC
case "${1}" in
  on) # ON/OFF
    if [ -e "/proc/${DIGITVCPID}" ] || [ -e "/proc/${PLAYERPID}" ]; then # True when either digitv or player running (Watching TV and/or Recording)
      if [ -z "${2}" ]; then # True when $2 unspecified (PWR/REC(+Play) = OFF)
        if [ ! -e "/proc/${RECPID}" ]; then # True when digitvrec not running (Not Recording)
          REASON="TV: OFF"
          BUTTON="PWRoff"
        else
          if [ ! -e "/proc/${PLAYERPID}" ]; then # True when player not running (Headless Recording)
            REASON="DVR: OFF"
            BUTTON="RECoff"
          else
            REASON="TV: OFF; DVR: OFF"
            BUTTON="REC+Playoff"
          fi
        fi
        DETAIL="Goodbye!"
        ICON=media-playback-stop-symbolic
      else
        case "${2}" in
          record|recordandplay) # (TODO: Multiple instances)
            if [ -e "/proc/${PLAYERPID}" ] && [ ! -e "/proc/${RECPID}" ]; then # True when player running but not digitvrec (Watching TV, Not Recording)
              EXPTIME=700
              REASON="DVR: ACTIVATED; TV: RESUME"
              DETAIL="Recording and Playing"
              ICON=media-record-symbolic
              "${0}" on & # Break current stream; Card supports one stream per input
              sleep 1
              "${0}" on recordandplay "${3}" "${4}" "${5}" & # Record and watch
            else
              REASON="DVR: OFF"
              DETAIL="Recording Stopped"
              ICON=media-playback-stop-symbolic
              "${0}" on & # Stop recording
            fi
          ;;
          *) # Catch bad input (TODO: Direct channel input)
            BADNESS="SWITCH(OFF?)"
          ;;
        esac
      fi
    else
      if [ -z "${2}" ]; then # True when $2 unspecified (PWR/REC(+Play) = ON)
        CHANNEL="$(sed -n '1{p;q;}' ~/.digitvrc)" # Set channel from config file (resume from last)
        REASON="Playing"
        DETAIL="CH: ${CHANNEL}"
        ICON=media-playback-start-symbolic
        BUTTON="PWRon"
      else
        if [ "${2}" = "record" ] || [ "${2}" = "recordandplay" ]; then # True when $2 is record or recordandplay (REC(+Play) = ON)
          if [ -z "${3}" ]; then # True when $3 unspecified (no channel, time or title)
            CHANNEL="$(sed -n '1{p;q;}' ~/.digitvrc)" # Set channel from config file (resume from last)
            RECTIME="$(sed -n '2{p;q;}' ~/.digitvrc)" # Set rectime from config file (default) (TODO: Other time formats)
            TITLE="CH$CHANNEL+$(date +%F+%R:%S)" # Set title with timestamp: CH#+YYYY-MM-DD+HH:MM:SS
          else
            if [ -z "${4}" ]; then # True when $4 unspecified (no time or title)
              CHANNEL="$(sed -n '1{p;q;}' ~/.digitvrc)" # Set channel from config file (resume from last)
              RECTIME="${3}" # (TODO: Other time formats; Check $3 is number)
              TITLE="CH${CHANNEL}+$(date +%F+%R:%S)" # Set title with timestamp: CH#+YYYY-MM-DD+HH:MM:SS
            else
              CHANNEL="${3}" # (TODO: Channel names; Check $3 is number)
              RECTIME="${4}" # (TODO: Other time formats; Check $4 is number)
              if [ -z "${5}" ]; then # True when $5 unspecified
                TITLE="CH${CHANNEL}+$(date +%F+%R:%S)" # Set title with timestamp: CH#+YYYY-MM-DD+HH:MM:SS
              else
                CHANNEL="${3}" # (TODO: Channel names; Check $3 is number)
                RECTIME="${4}" # (TODO: Other time formats; Check $4 is number)
                TITLE="${5}" # (TODO: Verify $5 string safety)
              fi
            fi
          fi
          REASON="DVR: Recording"
          DETAIL="CH: ${CHANNEL}"
          ICON=media-record-symbolic
          if [ "${2}" = "recordandplay" ]; then # True when $2 is recordandplay (REC+Play = ON)
            BUTTON="REC+Playon"
          else
            BUTTON="RECon"
          fi
        else # Catch bad input
          BADNESS="SWITCH(ON?)"
        fi
      fi
    fi
  ;; # END ON/OFF
  chanup|chandown) # CH+ & CH-
    if [ -e "/proc/${RECPID}" ]; then # True when digtvrec running (Recording; do not interrupt to change channel!)
      BADNESS="Recording, cannot CH+/CH-"
    else
      if [ -e "/proc/${PLAYERPID}" ] && [ -e "/proc/${DIGITVCPID}" ]; then # True when player and "${DIGITVCMD}" are running (Watching TV)
      CHANNEL="$(sed -n '1{p;q;}' ~/.digitvrc)" # Set channel from config file
      EXPTIME=700
        if [ "${1}" = "chanup" ]; then # CH+
          ICON=media-skip-forward-symbolic # (TODO: Better CH+ icon)
          REASON="TV: CH+"
          case "${CHANNEL}" in # (TODO: Channel names; Non-static list; Direct input: digitv on [number or name])
            14)
              CHANNEL=15
            ;;
            15)
              CHANNEL=22
            ;;
            22)
              CHANNEL=32
            ;;
            32)
              CHANNEL=33
            ;;
            33)
              CHANNEL=34
            ;;
            34)
              CHANNEL=14
            ;;
            # *) # Catch bad input (for unimplemented direct input)
              # BADNESS="CHANNEL"
            # ;;
          esac
        else
          ICON=media-skip-backward-symbolic # (TODO: Better CH- icon)
          REASON="TV: CH-"
          case "${CHANNEL}" in # (TODO: Channel names; Non-static list; Direct input: digitv on [number or name])
            34)
              CHANNEL=33
            ;;
            33)
              CHANNEL=32
            ;;
            32)
              CHANNEL=22
            ;;
            22)
              CHANNEL=15
            ;;
            15)
              CHANNEL=14
            ;;
            14)
              CHANNEL=34
            ;;
            # *) # Catch bad input (for unimplemented direct input)
              # BADNESS="CHANNEL"
            # ;;
          esac
        fi
        # if [ -z $BADNESS ]; then # True when $BADNESS unspecified (No error) (for unimplemented direct input)
          DETAIL="CH: $CHANNEL"
          BUTTON=$1
        # fi
      else
        BADNESS="CHANGE CH?"
      fi
    fi
  ;; # END  CH+ & CH-
  record|rec) # Shortcut to REC
    "${0}" on record "${2}" "${3}" "${4}" &
  ;;
  recordandplay|recplay) # Shortcut to REC+Play
    "${0}" on recordandplay "${2}" "${3}" "${4}" &
  ;;
  "") # Plenty of comments, no help.
    BADNESS="TODO: HELP"
  ;;
  *) # Catch bad input
    BADNESS="FUNCTION"
  ;;
 esac
############################################################################## EXECUTION
if [ -n "${BUTTON}" ]; then # True when BUTTON specified (Button pressed)
  case "${BUTTON}" in
    PWRon|PWRoff)
      if [ "${BUTTON}" = "PWRon" ]; then

        # linux-ck only
        #"${PLAYPRIORITY}" "${DIGITVCMD}" $DIGITVOPTS "${CHANNEL}" - - | "${PLAYERCMD}" $PLAYEROPTS - & # Watch TV

        "${DIGITVCMD}" $DIGITVOPTS "${CHANNEL}" - - | "${PLAYERCMD}" $PLAYEROPTS - & # Watch TV
        PLAYERPID="${!}"

	sleep 1 # "${DIGITVCMD}" starts slowly? (TODO: get pid from "${DIGITVCMD}" output)
        DIGITVCPID="$(pidof ${DIGITVCMD} || printf 99999)"
        sed -i "4s/.*/${DIGITVCPID}/" ~/.digitvrc # Save pid to config file
        sed -i "5s/.*/${PLAYERPID}/" ~/.digitvrc # Save pid to config file
      else
        kill -9 "${DIGITVCPID}" "${PLAYERPID}" & # Kill both
        sed -i "4s/.*/99999/" ~/.digitvrc # Save bogus pid to config file
        sed -i "5s/.*/99999/" ~/.digitvrc # Save bogus pid to config file
      fi
    ;;
    chanup|chandown)
      sed -i "1s/.*/${CHANNEL}/" ~/.digitvrc & # Save channel to config file
      "${DIGITVCMD}"ctl --pid "${DIGITVCPID}" --channel "${CHANNEL}" & # Change channel (TODO: Check channel availability)
      #TODO: Check for crash: sleep 1;
    ;;
    RECon|RECoff)
      if [ "${BUTTON}" = "RECon" ]; then

        # linux-ck only
        #"${RECPRIORITY}" digitvrec "${CHANNEL}" "${RECTIME}" "${TITLE}" & # Record (Headless) (TODO: REC indicator)

        digitvrec "${CHANNEL}" "${RECTIME}" "${TITLE}" & # Record (Headless) (TODO: REC indicator)

        RECPID="$(pidof digitvrec || printf 99999)"
	sleep 1 # "${DIGITVCMD}" starts slowly? (TODO: get pid from "${DIGITVCMD}" output)
        DIGITVCPID="$(pidof ${DIGITVCMD} || printf 99999)"
        sed -i "6s/.*/${RECPID}/" ~/.digitvrc & # Save pid to config file
        sed -i "4s/.*/${DIGITVCPID}/" ~/.digitvrc # Save pid to config file
      else
        kill -9 "${DIGITVCPID}" "${RECPID}" & # Kill both
        sed -i "6s/.*/99999/" ~/.digitvrc & # Save bogus pid to config file
        sed -i "4s/.*/99999/" ~/.digitvrc & # Save bogus pid to config file
      fi
    ;;
    REC+Playon|REC+Playoff)
      if [ "${BUTTON}" = "REC+Playon" ]; then

        # linux-ck only
        #"${RECPRIORITY}" digitvrec "${CHANNEL}" "${RECTIME}" "${TITLE}" & # Record (TODO: REC indicator)

        digitvrec "${CHANNEL}" "${RECTIME}" "${TITLE}" & # Record (TODO: REC indicator)

        RECPID="$(pidof digitvrec || printf 99999)"
	sleep 1 # "${DIGITVCMD}" starts slowly? (TODO: get pid from "${DIGITVCMD}" output)
        DIGITVCPID="$(pidof ${DIGITVCMD} || printf 99999)"
        sed -i "6s/.*/${RECPID}/" ~/.digitvrc & # Save pid to config file
        sed -i "4s/.*/${DIGITVCPID}/" ~/.digitvrc # Save pid to config file
        sleep 1

        # linux-ck only
        #"${PLAYPRIORITY}" timeout "${RECTIME}" "${PLAYERCMD}" $PLAYEROPTS ~/Videos/Uncompressed\ Capture/"${TITLE}".mp4 & # Watch Recording

        timeout "${RECTIME}" "${PLAYERCMD}" $PLAYEROPTS ~/Videos/Uncompressed\ Capture/"${TITLE}".mp4 & # Watch Recording

        PLAYERPID="${!}"
        sed -i "5s/.*/${PLAYERPID}/" ~/.digitvrc # Save pid to config file
      else
        kill -9 "${DIGITVCPID}" "${PLAYERPID}" "${RECPID}" & # Kill all
        sed -i "6s/.*/99999/" ~/.digitvrc # Save bogus pid to config file
        sed -i "4s/.*/99999/" ~/.digitvrc # Save bogus pid to config file
        sed -i "5s/.*/99999/" ~/.digitvrc # Save bogus pid to config file
      fi
    ;;
  esac
fi
############################################################################## ERROR
if [ -n "$BADNESS" ]; then # True when BADNESS is specified (Error)
  URGENCY="critical"
  REASON="TV: Invalid Input"
  DETAIL=$BADNESS
  ICON=action-unavailable-symbolic
fi
############################################################################## OUTPUT
# True when REASON specified (Message)
[[ -n "$REASON" ]] && exec notify-send -a "Digital TV" --icon=$ICON --urgency=$URGENCY --expire-time=$EXPTIME "$REASON" "$DETAIL"

And a VCR-inspired recording backend:

digitvrec

#! /bin/bash
##############################################################################
# This script contols an Earthsoft PT-series ISDB-T/S capture card
#
# The purpose is to operate a backend, command-line DVR.
# Other scripts, like digitvctl, could use it to set up more comfortable interfaces.
#
# DVR Functions:
# digitvrec 			Record configured channel for default time to timestamped file.
# digitvrec chan 		Record "chan" for default time to timestamped file.
# digitvrec chan time 		Record "chan" for "time (seconds)" to timestamped file.
# digitvrec chan time title 	Record "channel" for "time (seconds)" to file "title".
############################################################################## SETUP
# True when no config file exists (first run)
# Create config file (first channel; record 7200 seconds = 2 hours = 14.7GB file; Notification 0; spoof pid x3)
[[ ! -f ~/.digitvrc ]] && echo -e "14\n7200\n0\n99999\n99999\n99999\n" > ~/.digitvrc &

# True when no capture directory exists (first run)
# Create capture directory
[[ ! -d ~/Videos/Uncompressed\ Capture/ ]] && mkdir -p ~/Videos/Uncompressed\ Capture/ &

[[ -z ${DIGITVCMD} ]] && DIGITVCMD="recpt1"          # recpt1 (TODO: why dvb drivers disregarded by all japanese guides?)
[[ -z ${DIGITVOPTS} ]] && DIGITVOPTS="--b25 --strip" # Custom recpt1 string (arib25 decryption not optional)
############################################################################## LOGIC
if [ -z "${1}" ]; then # True when $1 unspecified (no channel, time or title)
	CHANNEL="$(sed -n '1{p;q;}' ~/.digitvrc)" # Set channel from config file (resume from last)
	RECTIME="sed -n '2{p;q;}' ~/.digitvrc)" # Set rectime from config file (default)
	TITLE="CH$CHANNEL+$(date +%F+%R:%S)" # Set title with timestamp: CH###+YYYY-MM-DD+HH:MM:SS
else
	CHANNEL="${1}" # (TODO: Check $1 is number)
	if [ -z "${2}" ]; then # True when $2 unspecified (no time or title)
		RECTIME="$(sed -n '2{p;q;}' ~/.digitvrc)" # Set rectime from config file (default)
		TITLE="CH$CHANNEL+$(date +%F+%R:%S)" # Set title with timestamp: CH#+YYYY-MM-DD+HH:MM:SS
	else
		RECTIME="${2}" # (TODO: Other time formats; Check $2 is number)
		if [ -z "${3}" ]; then # True when $3 unspecified (no title)
			TITLE="CH$CHANNEL+$(date +%F+%R:%S)" # Set title with timestamp: CH#+YYYY-MM-DD+HH:MM:SS
		else
			TITLE="${3}" # (TODO: Verify $5 string safety)
		fi
	fi
fi
############################################################################## EXECUTION
exec "${DIGITVCMD}" $DIGITVOPTS "${CHANNEL}" "${RECTIME}" ~/Videos/Uncompressed\ Capture/"${TITLE}".mp4 # Record to file (TODO: REC indicator)

Last edited by quequotion (2019-02-26 08:16:27)

Offline

#3229 2019-02-26 01:19:44

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

Re: Post your handy self made command line utilities

FYI, there is no need to nest the function case in a conditional to check if any command was passed, just test that as one of the cases:

case "$1" in
   on) ...
   ...
   "") BADNESS="TODO: HELP" ;;
   *) BADNESS="FUNCTION" ;;
esac

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

Offline

#3230 2019-02-26 08:19:53

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

Re: Post your handy self made command line utilities

Trilby wrote:

test that as one of the cases:

   "") BADNESS="TODO: HELP" ;;

The More You Know [shooting star emoji failed]

Thank you.

Last edited by quequotion (2019-02-26 10:39:38)

Offline

#3231 2019-03-19 18:40:31

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Post your handy self made command line utilities

There's a lot I do with Vim and I was always using the Vim cli to open tabs and was always using a separate one for using sudo, then there's Nerdtree, nice to have as addition but not fast enough.

That's why I decided to create this tiny plugin which are mostly put together from pieces of other scripts, I just tweaked them a little.
I must admit I have never used any other finders like "fuzzy finder" but, this one is blazing fast!!
The script only opens files or directories found in the first search, if you open a dir. you would still need Nerdtree, but still fast. I use that sometimes, if I don't know exactly which file in a single dir. I want to edit, Nerdtree opens in the dir. you choose from dmenu. Blank searches or ESC. are treated as such.
Also, sudo!, I've seen teh most complicated methods to do that, this one is simple, if you need to edit a file owned by root use the second shortcut. Your environment needs to know EDITOR=vim btw.
If you open F.I. /etc/fstab, the current buffer is closed first(not really, it's ptu on hold), then your PW is asked for, then /etc/fstab is opened in the current buffer, until you close it, you can't reach the previous current buffer, not a major drawback to me, but if you know how to change that, let me know;)
If you want everything to work the way I describe you'd need vim, Nerdtree and Dmenu and the next lines of code

Put this functions & mapped keys in your .vimrc

" Dmenu locate
" Remove newline from string
function! Chomp(str)
  return substitute(a:str, '\n$', '', '')
endfunction

" Locate a file with dmenu & open it & rm temp file
function! DmenuVimEdit(cmd)
  let dmena = system("./.config/dmenu/d-script/dmenu_vim_find")
  if exists("dmena")
    let dmenb = Chomp(system("cat /tmp/vde "))
  endif
  if filereadable("/tmp/vde")
    execute a:cmd . " " . dmenb
  endif
  let dmenc = system('rm -rf /tmp/vde')
endfunction

map <c-t> :call DmenuVimEdit("tabe")<cr>
map <c-f> :call DmenuVimEdit("!sudo -e")<cr>
" End Dmenu locate

Save the next script in '~/.config/dmenu/d-script/dmenu_vim_find'  or change path in vimrc file
You also need a dmenurc file.

#!/bin/sh

if [ -f "$HOME"/.config/dmenu/dmenurc ]; then
  . "$HOME"/.config/dmenu/dmenurc
else
  demcmd='dmenu -i'
fi

vde=$(vde 2> /dev/null) || vde=/tmp/vde

input="$(xsel -o | $demcmd -p "search file:")"

[ "$input" != '' ] && result="$(locate -e -r "$input" \
| $demcmd  -l 10  -p "search result:" )" || exit

[ -n "$result" ] && echo "$result" > "$vde"

exit 0

edit: removed useless if statements;)

Last edited by qinohe (2019-03-20 22:34:37)

Offline

#3232 2019-03-21 12:12:37

bedtime
Member
Registered: 2019-02-12
Posts: 74

Re: Post your handy self made command line utilities

This script automatically compiles, kills, and runs C and C++ files, and it works for gcc and g++ compilers. Please note that it has only been tested on single file programs. You must add the compile statement within the code. The script will find those parameters and run them.

Also note that the script is a bare-bones type thing; it has no error handling. It will only tell you if the script has passed or failed compilation.

Example C file:

/*

  The line below MUST be entered to be compiled:
  gcc -O3 -Wall bar.c -o bar -lpthread -lX11 -L/usr/include/curl/lib -lcurl

*/

#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<unistd.h>     /* close()  */
#include<linux/if.h>   /* struct ifreq */
#include<X11/Xlib.h>
#include<curl/curl.h>
#include<stdlib.h>
#include<pthread.h>

inline void fast_memcpy(char *str, const char *str2, int length)
{
    for(;--length > -1; )
        str[length] = str2[length];
}
...

The program:

c.sh:

#!/bin/bash


file=$1
vi $file


echo "Compiling "$file"...\\n"

cmd=$(awk '/g\+\+/ || /gcc/' $file)
echo "Command: \n"$cmd"\n"

$cmd

if [ $? -eq 0 ]; then
    echo $file" compiled successfully.\\n"
   
    # Find the program's name and run it:
    prog=$(echo $cmd | awk '{ for(i=1;i<=NF;i++) if ($i == "-o") print $(i+1) }')

    echo "killing "$prog"...\\n"
    killall -q $prog

    echo "Running "$prog"...\\n\\n"
    $prog
 
else
    echo $file" compiled unsuccessfully.\\n\\nAborting file execution.\\n\\n"
fi

Offline

#3233 2019-03-21 12:31:23

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

Re: Post your handy self made command line utilities

You seem to default to awk when grep would be plenty.  But more importantly, it looks like you could benefit from using a Makefile and perhaps a key binding or two from vim to build / build and run.


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

Offline

#3234 2019-03-21 15:19:46

bedtime
Member
Registered: 2019-02-12
Posts: 74

Re: Post your handy self made command line utilities

Trilby wrote:

You seem to default to awk when grep would be plenty.

The thing is that I only know awk decently; I'm betting that grep would be the lighter and more ideal solution.

But more importantly, it looks like you could benefit from using a Makefile and perhaps a key binding or two from vim to build / build and run.

I know very little about how Makefiles work... I am liking the sound of the key binding for vim, as I use vi as my IDE. Will look that up! smile

Offline

#3235 2019-04-13 21:11:55

t3kk3n
Member
Registered: 2014-04-18
Posts: 24

Re: Post your handy self made command line utilities

I wrote this script the other day to assist me in turning on, and off, the testing repositories, with one command. I understand this looks horrible, however, I did it this way to test if only say, [testing] is enabled, but [community-testing] isn't, therefore it shouldn't mess with the config. Of course, the sed commands could be stripped out of this, and added to your own install script (as I did afterwards). Oh, and change the pacaur command with whatever pacman helper you are using, if you decide to use this/improve on it/etc.

#!/bin/bash

function gen_help () {
    echo -e " [****] $0 <on/off> - enables or disables both testing and community-testing repositories in /etc/pacman.conf"
}

if [ $# -eq 0 ]; then gen_help; exit 1; fi

xbit=0
sbit="$1"

if ! [[ "$sbit" == "on" || "$sbit" == "off" ]]; then
    gen_help
    exit 1
fi

if [[ "$sbit" == "on" ]]; then
    xbit=1
fi

hvar="#"
s1val=0
s2val=0

stat1v1=$(sed -n '/\[testing\]/p' /etc/pacman.conf)
stat1v2=$(sed -n '/\[testing\]/{n;p}' /etc/pacman.conf)
stat2v1=$(sed -n '/\[community-testing\]/p' /etc/pacman.conf)
stat2v2=$(sed -n '/\[community-testing\]/{n;p}' /etc/pacman.conf)

x1=$(echo "$stat1v1" | cut -c1)
x2=$(echo "$stat1v2" | cut -c1)
y1=$(echo "$stat2v1" | cut -c1)
y2=$(echo "$stat2v2" | cut -c1)

if [[ ("$x1" == "$hvar") && ("$x2" == "$hvar") ]]; then
    s1val=1
elif [[ ("$x1" != "$hvar") && ("$x2" != "$hvar") ]]; then
    s1val=2
fi

if [[ ("$y1" == "$hvar") && ("$y2" == "$hvar") ]]; then
    s2val=1
elif [[ ("$y1" != "$hvar") && ("$y2" != "$hvar") ]]; then
    s2val=2
fi

if [[ ("$s1val" != "$s2val") || ("$s1val" == 0 || "$s2val" == 0) ]]; then
    echo -e " [****] Mismatch detected in /etc/pacman.conf, exiting"
    exit 1
fi

if [[ "$s1val" == 1 && "$xbit" == 0 ]]; then
    echo -e " [****] Testing/community-testing already disabled"
elif [[ "$s1val" == 1 && "$xbit" == 1 ]]; then
    sudo sed -i '/\[testing\]/,+1 s/^#//' /etc/pacman.conf
    sudo sed -i '/\[community-testing\]/,+1 s/^#//' /etc/pacman.conf
    echo -e " [****] Testing/community-testing enabled"
    pacaur -Sy
elif [[ "$s1val" == 2 && "$xbit" == 0 ]]; then
    sudo sed -i '/\[testing\]/,+1 s/^/#/' /etc/pacman.conf
    sudo sed -i '/\[community-testing\]/,+1 s/^/#/' /etc/pacman.conf
    echo -e " [****] Testing/community-testing disabled"
    pacaur -Sy
elif [[ "$s1val" == 2 && "$xbit" == 1 ]]; then
    echo -e " [****] Testing/community-testing already enabled"
else
    echo -e " [****] Mismatch or other error, please check /etc/pacman.conf"
fi

Offline

#3236 2019-04-14 00:13:15

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

Re: Post your handy self made command line utilities

That all could be much simpler:

alias testing_on='sed -i "/\[.*testing\]/,+1 s/^#*//" /etc/pacman.conf'
alias testing_off='sed -i "/\[.*testing\]/,+1 s/^/#/" /etc/pacman.conf'

But whichever way you do it, definitely get rid of that `pacman -Sy`.  You should just never do that.  Period.


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

Offline

#3237 2019-04-14 02:19:52

luanpotter
Member
Registered: 2019-04-14
Posts: 9
Website

Re: Post your handy self made command line utilities

Really nice thread! I have several that I have created over the years on my dotfiles repo (specially in the scripts folder).
A few that I really like are:

end:

find . -name "*$1"

Very simple but I use all the time. It could be an alias but I failed to find out how to have the parameter inside like that, so I made a script.

fvim:

Another one is fvim; basically copy a class name+line from a Java stack trace and fvim that to open that class in that line on vim. Useful only when coding Java, of course.

rex:

Another one that I've been using a lot is rex. This one you can install via npm (npm i -g bash-rex), is basically a sed but using regular human being's regexes (like the ones seen on most programming language's (java, js, python)). For example:

echo 'package-name-1.2.3' | rex '(\w)-\d[\d-.:+]*' '$1'
> package-name

My lack of knowledge of sed's convoluted syntax would make the writing that kind of operation a hassle for me without rex. That's just a matter of taste (and my personal ignorance), of course.

Offline

#3238 2019-04-14 02:47:02

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

Re: Post your handy self made command line utilities

luanpotter wrote:

It could be an alias but I failed to find out how to have the parameter inside like that, so I made a script.

Aliases can't have parameters - but that could be a function:

end() { find . -name '*$1'; }

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

Offline

#3239 2019-04-14 02:57:57

t3kk3n
Member
Registered: 2014-04-18
Posts: 24

Re: Post your handy self made command line utilities

Trilby wrote:

That all could be much simpler:

alias testing_on='sed -i "/\[.*testing\]/,+1 s/^#*//" /etc/pacman.conf'
alias testing_off='sed -i "/\[.*testing\]/,+1 s/^/#/" /etc/pacman.conf'

But whichever way you do it, definitely get rid of that `pacman -Sy`.  You should just never do that.  Period.

So then, -Syu all the time, when I would want to essentially refresh the database? Just asking, because whenever I do switch between enabled and disabled, and then go to install/update a package, it does say to run 'pacaur -Sy' to refresh.

Offline

#3240 2019-04-14 03:29:24

luanpotter
Member
Registered: 2019-04-14
Posts: 9
Website

Re: Post your handy self made command line utilities

Trilby wrote:

Aliases can't have parameters - but that could be a function

That's very clever!

Thanks for the tip smile

Offline

#3241 2019-04-14 03:44:41

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

Re: Post your handy self made command line utilities

Trilby wrote:

Aliases can't have parameters - but that could be a function:

end() { find . -name '*$1'; }

"*$1"

Offline

#3242 2019-04-14 03:50:34

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

Re: Post your handy self made command line utilities

t3kk3n wrote:

whenever I do switch between enabled and disabled, and then go to install/update a package, it does say to run 'pacaur -Sy' to refresh.

What says to do that?  Whatever that is, stop reading it as it's giving dangerous advice.


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

Offline

#3243 2019-04-14 04:52:11

t3kk3n
Member
Registered: 2014-04-18
Posts: 24

Re: Post your handy self made command line utilities

Trilby wrote:
t3kk3n wrote:

whenever I do switch between enabled and disabled, and then go to install/update a package, it does say to run 'pacaur -Sy' to refresh.

What says to do that?  Whatever that is, stop reading it as it's giving dangerous advice.

Ok, part my fault for not explaining better - I'm constantly messing with configuration files, and spinning up new instances of arch in a VM for testing purposes. I wrote that script to look to see if pacman.conf had testing enabled or not, and put pacaur[/pacman] -Sy in the script just to refresh/rebuild(?) the local database, so I can then update and/or install packages from testing. So essentially, instead of -Sy, just do -Syu. Got it, thanks!

And as far as that massage goes, it was pacman itself telling me to run -Sy, since nothing was updated locally, yet.

Offline

#3244 2019-04-14 10:23:39

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,295
Website

Re: Post your handy self made command line utilities

t3kk3n wrote:

it was pacman itself telling me to run -Sy, since nothing was updated locally, yet.

You are mistaken, pacman(8) would not tell you to (potentially) break your system. Unless Allan has done something to it recently ofc big_smile

To keep this on-topic, here is a function I use to rebuild ISO images after modifying them:

function mkiso {
 xorriso -as mkisofs \
 -iso-level 3 \
 -full-iso9660-filenames \
 -volid custom \
 -eltorito-boot isolinux/isolinux.bin \
 -eltorito-catalog isolinux/boot.cat \
 -no-emul-boot -boot-load-size 4 -boot-info-table \
 -isohybrid-mbr isolinux/isohdpfx.bin \
 -eltorito-alt-boot \
 -e boot/grub/efi.img \
 -no-emul-boot -isohybrid-gpt-basdat \
 -output ../"$1" ./
}

Run the function in the main directory of the image contents, the first argument provides the name of the image.

Last edited by Head_on_a_Stick (2019-04-14 10:25:13)


Para todos todo, para nosotros nada

Offline

#3245 2019-05-06 00:13:28

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

Re: Post your handy self made command line utilities

I finally got around to cleaning up my pseudo-address book script to take better advantage of 'notmuch'.  I've not kept an address book of emails for years as I realized everyone I'd want in my addressbook will be found somewhere in my mail archives - plus I may periodically want to email someone who I've corresponded with before but never bothered to put in an address book.

So instead, I set up mutt's query_command to check senders I've received mail from in the past year (can be adjusted at the "date:1y") that also match all the words in the current entry with the results sorted in decreasing order of how frequently they've emailed me:

#!/bin/sh

notmuch address --deduplicate=address --output=count \
        date:1y $(printf " AND from:/%s/" $@) | sort -nr | sed '
                1 iSearch Results for '"$*"'
                />$/ s/^[0-9]*\s*\(.*\) <\([^>]*\)>/\2\t\1/
                /[^>]$/ s/^[0-9]*\s*//
        '

Last edited by Trilby (2019-05-06 00:14:01)


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

Offline

#3246 2019-05-14 01:25:33

cowile
Member
Registered: 2019-05-01
Posts: 15

Re: Post your handy self made command line utilities

I've written dozens of these over the years, but I always find most of them don't turn out as useful as originally thought. These are exceptions.

This one creates symlinks to point to configuration files and personal scripts under a git repository.

#!/usr/bin/env bash
mkdir -p ~/.config

location="desktop"
repository=~/code/toolbox

while getopts "dlr:" flag
do
	case "$flag" in
		d) location="desktop" ;;
		l) location="laptop" ;;
		r) repository="$OPTARG" ;;
		?) echo "Invalid flag"; exit 1 ;;
	esac;
done

ln -fsT "$repository/scripts/" ~/bin

for cfg in "$repository"/config-files/{common,"$location"}/*
do
	b=$(basename "$cfg")

	if [[ "$b" == "config" ]]
	then
		for cfg2 in "$cfg"/*
		do
			ln -fs "$cfg2" ~/.config
		done
	else
		ln -fs "$cfg" ~/."$b"
	fi
done

This one uses bemenu (dmenu clone for wayland) to provide a menu of tmux sessions and open a terminal with that session, or creates one if it doesn't exist. I bind this to $mod+Return in my window manager.

#!/usr/bin/env bash

options=$(tmux list-sessions | cut -d : -f 1)
selection=$(echo "$options" | bemenu)

if [[ -n "$selection" ]]
then
	exec termite -t "$selection" -e "tmux new-session -A -s \"$selection\""
fi

Last edited by cowile (2019-05-14 04:24:31)


The Linux motto is "Laugh in the face of danger." Oops, wrong one. "Do it yourself." Yes, that's it. -- Linus Torvalds

Offline

#3247 2019-05-14 01:34:26

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

Re: Post your handy self made command line utilities

cowile, your scripts use a `env sh` shebang, yet they contain bashisms - they will not run on many implementations of `sh`.  You should specify bash in the shebang for those (or zsh if you prefer).


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

Offline

#3248 2019-05-14 04:10:52

cowile
Member
Registered: 2019-05-01
Posts: 15

Re: Post your handy self made command line utilities

Trilby wrote:

cowile, your scripts use a `env sh` shebang, yet they contain bashisms - they will not run on many implementations of `sh`.  You should specify bash in the shebang for those (or zsh if you prefer).

I assume you are talking about the [[ conditionals and getopts. You are correct. They are bad habits on my part. I will change the shebang to bash.

Last edited by cowile (2019-05-14 04:11:19)


The Linux motto is "Laugh in the face of danger." Oops, wrong one. "Do it yourself." Yes, that's it. -- Linus Torvalds

Offline

#3249 2019-05-14 04:20:59

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

Re: Post your handy self made command line utilities

Yup, and the brace expansion in the for loop.  I certainly wouldn't call their use bad habit - I'm just pedantic about assumptions that 'sh' should/would be bash.


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

Offline

#3250 2019-05-14 04:26:59

cowile
Member
Registered: 2019-05-01
Posts: 15

Re: Post your handy self made command line utilities

Trilby wrote:

Yup, and the brace expansion in the for loop.  I certainly wouldn't call their use bad habit - I'm just pedantic about assumptions that 'sh' should/would be bash.

That isn't what I meant. The habit I was talking about was always starting with sh and not changing it when I figure out it should be bash.


The Linux motto is "Laugh in the face of danger." Oops, wrong one. "Do it yourself." Yes, that's it. -- Linus Torvalds

Offline

Board footer

Powered by FluxBB