You are not logged in.

#76 2018-03-12 13:15:22

martix
Kim Jong-un Stunt Double
Registered: 2016-02-19
Posts: 1,267

Re: Handy command-line stuff for terminals or scripts

There is the small script Spark ohnonot posted above (can be installed with this one liner:)

sudo sh -c "curl https://raw.githubusercontent.com/holman/spark/master/spark -o /usr/local/bin/spark && chmod +x /usr/local/bin/spark"

Using spark and the following command gives an overview regarding file sizes in a directory (handy to discover unusual big files or directories):

du -BM * | cut -dM -f1 | spark

Edit: Check programs started by xdg:

/usr/lib/x86_64-linux-gnu/openbox-xdg-autostart --list

To find a command that was used before (e.g. something with tmux), then

history|grep tmux

is handy. Even better: "hgrep". Put this

alias hgrep='history | grep --color=auto'

in ~/.bashrc. After "exec bash" there is the new command "hgrep" available for e.g. hgrep tmux.


Let's check the IP from command line:

wget -q -O - checkip.dyndns.org | sed -e "s/[^[:digit:]|.]//g"

or

dig +short myip.opendns.com @resolver1.opendns.com

Last edited by martix (2018-04-20 21:10:17)

Offline

#77 2018-04-27 07:39:26

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
Website

Re: Handy command-line stuff for terminals or scripts

If you use git...
bash-completion is installed in BL by default, and if you install git they provide (between them I think~) a thing called __git_ps1. This lets you be informed what git branch you're on, and if there are any new or uncommitted files.
It might save you from a silly mistake some day. cool

Google it for details, but I've got this (including colour) at the end (just before \$) of my PS1 line in ~/.bashrc:

\[\e[32m\]$(__git_ps1 "[%s]")\[\e[0m\]

This gives you the branch name at the end of your prompt like this, eg [master]

If you're not inside a git directory it shows nothing, and doesn't seem to significantly slow down the terminal.

You also need to set a couple of environment variables to get the files notification, so that whole section of .bashrc looks like this:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\e[32m\]$(__git_ps1 "[%s]")\[\e[0m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1 "[%s]")\$ '
fi
unset color_prompt force_color_prompt

I switched this on last week and am appreciating it very much.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#78 2018-04-27 17:28:06

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,029

Re: Handy command-line stuff for terminals or scripts

That is sweet! Thanks for the tip.

Offline

#79 2018-04-27 17:50:22

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

^^ sweet.
had to dig it out & source from /usr/share/git/git-prompt.sh on archlinux, fyi.
testing performance impact. i'd hate my prompt to become slow...

Offline

#80 2018-04-27 22:38:20

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Handy command-line stuff for terminals or scripts

Grml's zsh config also has a git status readout for the prompt:

https://grml.org/zsh/

Sample:

empty@Xanadu:~ $ zsh
empty@Xanadu ~ % cd git/bunsen-configs
empty@Xanadu ~/git/bunsen-configs (git)-[helium-dev_grey] %

Many other handy features as well, I particularly like the semi-automatic programmable spell-correction glasses

Last edited by Head_on_a_Stick (2018-04-28 15:48:34)


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

#81 2018-04-28 09:20:22

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

testing performance impact. i'd hate my prompt to become slow...

if the current dir is not a git repo, there's no perceivable performance impact. none.
in fact i already forgot that i put it in there yesterday...
so that's a good thing.

Offline

#82 2018-05-17 22:40:04

misko_2083
Member
Registered: 2016-05-24
Posts: 93

Re: Handy command-line stuff for terminals or scripts

Script to send F5 keyboard key via xdotool to firefox window in focus every 30 seconds.

#!/bin/bash

     while  : ; do
         # gets root window property _NET_ACTIVE_WINDOW
         WIN=($(xprop -root _NET_ACTIVE_WINDOW))

         # extracts window id (base seven)
         WIN="${WIN[4]%%","*}"

         WIN="$(printf "%s\n" ${WIN})"  # bypass weird array bug

          # Decimal window id of firefox
          WindowID="$(xdotool search --class "firefox" 2>/dev/null | tail -1)"

          # Base seven window id of firefox
          WindowIDbSeven=$(printf "0x%07x" ${WindowID})

         # test if acive window id mathes with dolphins window id
         if [[ "${WIN}" == "${WindowIDbSeven}" ]]; then
             # sends F5 to firefox's window id
             xdotool key --clearmodifiers "$WindowIDbSeven" F5
         fi
          
          WIN=()

         sleep 30

     done

Offline

#83 2018-05-18 05:53:30

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Handy command-line stuff for terminals or scripts

^ Nice! cool

You can drop a command by initiating the while loop with `sleep`, like this:

#!/bin/sh

     while sleep 30 ; do
         # gets root window property _NET_ACTIVE_winDOW
         win=($(xprop -root _NET_ACTIVE_winDOW))

         # extracts window id (base seven)
         win="${win[4]%%","*}"

         win="$(printf "%s\n" ${win})"  # bypass weird array bug

          # Decimal window id of firefox
          windowid="$(xdotool search --class "firefox" 2>/dev/null | tail -1)"

          # Base seven window id of firefox
          windowidbseven=$(printf "0x%07x" ${windowid})

         # test if acive window id mathes with dolphins window id
         if [ "${win}" = "${windowidbseven}" ]; then
             # sends F5 to firefox's window id
             xdotool key --clearmodifiers "$windowidbseven" F5
         fi
          
          win=()

     done

I've also converted the script to POSIX (although I haven't tested it) so that a speed boost and memory saving can be had under Debian (/bin/dash is faster, lighter and less buggy that /bin/bash).


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

#84 2018-05-18 11:59:29

misko_2083
Member
Registered: 2016-05-24
Posts: 93

Re: Handy command-line stuff for terminals or scripts

Head_on_a_Stick wrote:

^ Nice! cool

You can drop a command by initiating the while loop with `sleep`, like this:

#!/bin/sh

     while sleep 30 ; do
         # gets root window property _NET_ACTIVE_winDOW
         win=($(xprop -root _NET_ACTIVE_winDOW))

         # extracts window id (base seven)
         win="${win[4]%%","*}"

         win="$(printf "%s\n" ${win})"  # bypass weird array bug

         # Decimal window id of firefox
         windowid="$(xdotool search --class "firefox" 2>/dev/null | tail -1)"

         # Base seven window id of firefox
         windowidbseven=$(printf "0x%07x" ${windowid})

         # test if acive window id mathes with dolphins window id
         if [ "${win}" = "${windowidbseven}" ]; then
             # sends F5 to firefox's window id
             xdotool key --clearmodifiers "$windowidbseven" F5
         fi
          
         win=()

     done

I've also converted the script to POSIX (although I haven't tested it) so that a speed boost and memory saving can be had under Debian (/bin/dash is faster, lighter and less buggy that /bin/bash).

while sleep 30 ... Very good point Head_on_a_Stick, "sleep" always returns exit status 0, (after 30 seconds here).

Bash arrays are not posix compliant. We need a little juggling to extract window id out of "xprop -root _NET_ACTIVE_WINDOW" output for dash. smile

#!/bin/sh

     while sleep 30 ; do
         # gets root window property _NET_ACTIVE_WINDOW
          win="$(xprop -root _NET_ACTIVE_WINDOW | cut -d' ' -f5)"

          # Decimal window id of firefox
          windowid="$(xdotool search --onlyvisible --class "firefox" 2>/dev/null | tail -1)"

          # Base seven window id of firefox
          windowidbseven=$(printf "0x%07x" ${windowid})

         # test if acive window id mathes with dolphins window id
         if [ "${win%%,*}" = "${windowidbseven}" ]; then
             # sends F5 to firefox's window id
             xdotool key --clearmodifiers "$windowidbseven" F5
         fi
          
          win=""

     done

I use arrays too much. hmm


<-------------------------------------------------------

Next script is a bash script mouse-demo which is a good example on how to control the script with the mouse in terminal.
https://pastebin.com/6QGR97Vg

Last edited by misko_2083 (2018-05-18 12:58:05)

Offline

#85 2018-05-23 07:15:32

ohnonot
...again
Registered: 2015-09-29
Posts: 3,194
Website

Re: Handy command-line stuff for terminals or scripts

misko_2083 wrote:

I use arrays too much. hmm

posix compliance is overrated.
i mean, python isn't posix compliant either.
arrays are the only thing that make me, a poor bash hacker, feel like a real coder.

Offline

#86 2018-09-28 08:47:17

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 4,673
Website

Re: Handy command-line stuff for terminals or scripts

Split a string by multiple regex matches

Most of the familiar utilities - sed, grep & friends - deal with data a line at a time, but more and more these days line breaks are becoming irrelevant, and parsing something like a config file harder, at least with shell commands. Just now I was playing with a bash function to parse a conky 10 style config file. Running down it with read works as long as the writer formatted it nicely with each entry on its own line, but in fact conky is quite happy with something like:

 --[[ conky configuration
 Index » Scripts, Tutorials & Tips » Show us your conky
 ]] conky.config
  = { imlib_cache_size = 0,
--  Window Settings
    own_window = true ,own_window_type = 'normal',
    own_window_hints = 'undecorated,above,skip_taskbar,skip_pager,sticky',
-- (more follows)

So you see the 'conky.config = {' is split over two lines, and follows right after the comment block ]], the first config entry is right after the config opening '{' and two entries are on the same line. You can't work down this line by line, so I tried squashing all the config entries into a single line:

imlib_cache_size = 0,own_window = true,own_window_type = 'normal',own_window_transparent = true,own_window_hints = 'undecorated,above,skip_taskbar,skip_pager,sticky',color0 = "B0E0E6",gap_x = 20,gap_y = 45,alignment = 'top_right',double_buffer = true,

With that, at first it looks as if you could just split it into individual entries using the ',' as a delimiter, but what about those commas inside quotes like own_window_hints = 'undecorated,above,skip_taskbar,skip_pager,sticky',?

You either need to learn lua or Python, or presistently stick to Bash and use a regular expression. This works cool and the Bash array BASH_REMATCH helps. It's a pity Bash won't work down the string putting every occurrence of a regex into the array, but only the first match, but there's a way out - get the first match, then chop that bit off the front of the string and try the regex again. This way you can get every occurrence, and using inner brackets ( ) you can pick out sub-elements of the match without any further processing.

So in this particular case:

line="imlib_cache_size = 0,own_window = true,own_window_type = 'normal',own_window_transparent = true,own_window_hints = 'undecorated,above,skip_taskbar,skip_pager,sticky',color0 = \"B0E0E6\",gap_x = 20,gap_y = 45,alignment = 'top_right',double_buffer = true,"
regex="([[:alnum:]_]+)[[:blank:]]*=[[:blank:]]*([[:alnum:]]+|'[^']+'|\"[^\"]+\")"
while [[ $line =~ $regex ]] ; do echo "name: ${BASH_REMATCH[1]}"; echo "value: ${BASH_REMATCH[2]}"; line=${line#*${BASH_REMATCH[0]}}; done
name: imlib_cache_size
value: 0
name: own_window
value: true
name: own_window_type
value: 'normal'
name: own_window_transparent
value: true
name: own_window_hints
value: 'undecorated,above,skip_taskbar,skip_pager,sticky'
name: color0
value: "B0E0E6"
name: gap_x
value: 20
name: gap_y
value: 45
name: alignment
value: 'top_right'
name: double_buffer
value: true

I was quite happy to get this working, and it might come in useful again some day.

Original idea here: https://unix.stackexchange.com/a/251031

BTW Don't try to use this to parse html or XML or all the wrath of the interwebs will be visited upon you.  yikes


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

Board footer

Powered by FluxBB