Post your Command line tricks

Forum rules
Share your brain ;)
User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Wed Apr 29, 2015 4:37 pm

not so much CLI as it is scripting, but can be useful for both.

was playing with Lemonbar and figured it would be nice of it were somehow possible to have it tie in, colourwise, with one's Xresources colourscheme. so I wanted a quick way to grab a colour from the Xresources to use it in my script. i figured i'd use grep, but some online searching led me to use sed instead, which can do the same basic thing as grep, namely find PATTERN in FILE.

Code: Select all

sed -n "/*color4:/s/*color4://p" ~/.Xresources | tr -d [:blank:]
since i make thankful use of Drew's colourswitching-thingy (Bork! included by default, but i assume all spins?), my .Xresources doesn't have the colours listed directly but instead works through an #include. to deal with that, i did the following.

Code: Select all

xcolors=$(sed -n "/#include/s/#include//p" ~/.Xresources | sed "s/<\(.*\)>/\1/")
mycolor=$(sed -n "/*color4:/s/*color4://p" $xcolors | tr -d [:blank:])
as you can see in both examples, this is crude in the sense that it will let you pick just one colour and you will have to hope and pray it is the right one for your notification (or whatever), and if not you have to trial-and-error and see which number does fit. when you decide to change colourschemes, another run of trial-and-error might be needed to get what you want.

so it's definitely not perfect, but it is an idea that might be expanded upon, and at least i learned something new about sed :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
z3bra
Window Manager
Posts: 53
Joined: Fri Jun 27, 2014 8:34 am
Location: France
Contact:

Re: Post your Command line tricks

Unread post by z3bra » Wed Apr 29, 2015 5:52 pm

Hey, did you know? You can interact with the X resources database using its client: "xrdb".
And as every database client, it can both write and READ data to/from the database:

Code: Select all

xrdb | grep -E '\*color[0-9]*:'
BANGARANG, MOTHERFUCKER.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Wed Apr 29, 2015 6:08 pm

^ that :D
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Wed Apr 29, 2015 7:43 pm

hahahahaha, fuck, nope, clearly didn't know. oh well, i learned some more sed at least :)
thanksss

edit: HOWEVER i would like to point out that the issue with which colour to grab still stands. if anybody has anything bright to say in that regard, that would be nice. if not i'll keep thinking :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Wed Apr 29, 2015 8:01 pm

in my case it doesn't help though, as xrdb seems to only be able to get this information from ~/.Xresources and not from a seperate file:

Code: Select all

[22:59:59]$ xrdb ~/.Xresources 
xrdb:  "*background" on line 43 overrides entry on line 3
xrdb:  "*foreground" on line 44 overrides entry on line 4

[23:00:08]$ cat /home/rhowaldt/xcolors/xcolor_code/blend 
! dkeg 2014
! blend

*background:   #121212
*foreground:   #abacae

*color0:       #2a2a2a
*color1:       #4c3333
*color2:       #959c4f
*color3:       #976c4e
*color4:       #3d5a72
*color5:       #978a9b
*color6:       #4a5e5b
*color7:       #a1a7ad
*color8:       #3a3a3a
*color9:       #8d4d4d
*color10:      #b6bd6e
*color11:      #bc9f67
*color12:      #7a9dbc
*color13:      #ae9eb3
*color14:      #80a09b
*color15:      #c7cbcf

[23:00:23]$ xrdb /home/rhowaldt/xcolors/xcolor_code/blend 

(the command in z3bra's example does nothing because xrdb requires a filename to work on)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
z3bra
Window Manager
Posts: 53
Joined: Fri Jun 27, 2014 8:34 am
Location: France
Contact:

Re: Post your Command line tricks

Unread post by z3bra » Wed Apr 29, 2015 8:12 pm

Oh, my bad, I forgot the most important bit!

Code: Select all

xrdb -query | grep -E '\*color[0-9]*:'
It will then output what's currently loaded in the database
BANGARANG, MOTHERFUCKER.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Wed Apr 29, 2015 8:41 pm

aaaah thanks! damn, and i looked through the manpage but missed that query bit.
then to get the actual colour so it can be used in a script, you can still use part of my suggestion:

Code: Select all

xrdb -query | sed -n "/*color4:/s/*color4://p" $xcolors | tr -d [:blank:]
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Thu Apr 30, 2015 6:32 am

just a very little side note, rho, you can comment line 3 and 4 in .Xresources which usually sets the background and foreground for all X11 appllications (like xpdf or xcalc), using the almight *background and *foreground entries, but spits out the warning because of the override in the last row. Means, if you want the grey scheme in xpdf after commenting line 3 and 4, you need to set the resources of xpdf explicitly, for example like described here: https://wiki.archlinux.org/index.php/X_ ... _resources
..gnutella..

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Thu Apr 30, 2015 6:51 am

^ Didn't knew that Thanks mb !

@rho *cough* colr *cough* :)
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Thu Apr 30, 2015 9:38 am

^^ thanks :)
^ thanks, now i see how that would be useful, notice you even provided a dmenu example with it :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
ivanovnegro
Minister of Truth
Posts: 5448
Joined: Wed Oct 17, 2012 11:12 pm

Re: Post your Command line tricks

Unread post by ivanovnegro » Thu Apr 30, 2015 5:17 pm

Clear old configuration files from your Debian system. Did that after the freeze, it removed a bunch of shit.

Code: Select all

apt-get purge $(dpkg -l | awk '/^rc/ { print $2 }')
You probably use that command already or in one of the BBQ cleaner scripts.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Thu Apr 30, 2015 6:11 pm

Ah, that's a nice one, removed the stuff that --purge didn't catch. Thanks!
..gnutella..

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Post your Command line tricks

Unread post by pidsley » Thu Apr 30, 2015 6:26 pm

Thanks ivan -- I will add this to the sysup script.

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Fri May 01, 2015 4:41 pm

from here: http://stackoverflow.com/questions/3510 ... -and-regex
Explaining the grep '[p]ython csp_build.py' bit in a bit more detail:

When you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep 'leep' won't create a process with sleep in it, it instead creates grep 'leep' and here's the tricky bit: the grep doesn't find it because it's looking for the regular expression "any character from the character class (which is s) followed by leep.

In other words, it's looking for sleep but the grep process is grep 'leep' which doesn't have sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

it's one less process than adding | grep -v grep; and
it's elegant and sneaky, a rare combination :-)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Fri May 01, 2015 7:09 pm

^ Woah, Thanks so much rho !

Not much of a trick, I use `most` as my main pager but sometimes for whatever reason I like to view some manpages with `less` so here's a little tweaked out shell function:

Code: Select all

h ()
{
man='/usr/bin/man'
export PAGER='less'

	env \
		LESS_TERMCAP_mb=$(printf "\e[1;31m") \
		LESS_TERMCAP_md=$(printf "\e[1;31m") \
		LESS_TERMCAP_me=$(printf "\e[0m") \
		LESS_TERMCAP_se=$(printf "\e[0m") \
		LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
		LESS_TERMCAP_ue=$(printf "\e[0m") \
		LESS_TERMCAP_us=$(printf "\e[1;32m") \
			man "$@"

export PAGER='most -dw'
}
So the usual `man info` command will open the manpage using most and `h info` will open the manpage using less with the colors and will restore back `most` as the main pager.

Another very stupid `cd` shell function:

Code: Select all

cd ()
{
  if [ -n "$1" ]; then
   clear && echo && builtin cd "$@" && ls --group-directories-first --color=no && echo
  else
   clear && echo && builtin cd ~ && ls --group-directories-first --color=no && echo
  fi
}
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Fri May 01, 2015 7:26 pm

^ challenge: How hard could it be to replace bash's default pager (when tabbing large dir listings) 'less', instead of 'more'?
I think I gave it a go and just typed a bit more... :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Post your Command line tricks

Unread post by stark » Fri May 01, 2015 8:29 pm

^ I don't think that's possible, that's an internal pager built into the readline and I haven't found any options to change it.

from bash's man page:

Code: Select all

page-completions (On)
       If set to On, readline uses an internal more-like pager to display a screenful of possible completions at a time.
Typing more is the only option I can think of ;) or maybe messing with this function http://git.savannah.gnu.org/cgit/readli ... ete.c#n545 ?
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Post your Command line tricks

Unread post by rhowaldt » Fri May 01, 2015 11:54 pm

this might be naive or something, but i thought you could just do

Code: Select all

PAGER="less -X"
(that was always my choice, so example here)
have that in your .bashrc and it's default, no?
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Post your Command line tricks

Unread post by machinebacon » Sat May 02, 2015 3:25 am

yup, IMO just / export PAGER="less -X" / before the man command and there you go.
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Post your Command line tricks

Unread post by wuxmedia » Sun May 03, 2015 8:17 pm

for 'man' sure and anything that calls it. As stark observed in the readline of bash one cannot change it..

Code: Select all

wux@numenor:~$ ls /etc/
Display all 198 possibilities? (y or n)
yeah looks like fiddling with the code. Not going to be doing that :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

Post Reply