You are not logged in.

#51 2012-08-09 23:17:16

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: Cool Functions for your .bashrc file

Latest news from Arch Official website (but you can change the source: this script is simply a stupid - but coloured :-D - rss reader):

# The characters "ç, £, §" are used as metacharacters. They should not be encountered in a feed...
echo -e "$(echo $(curl --silent http://www.archlinux.org/feeds/news/ | sed -e ':a;N;$!ba;s/\n/ /g') | \
	sed -e 's/>/ç/g' |
	sed -e 's/<\/aç/£/g' |
	sed -e 's/href\=\"/§/g' |
	sed -e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' |
	sed -e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' |
	sed -e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' |
	sed -e 's/&lt;pç/\n/g' |
	sed -e 's/&lt;bç\|&lt;strongç/\\e[01;30m/g' -e 's/&lt;\/bç\|&lt;\/strongç/\\e[00;37m/g' |
	sed -e 's/&lt;a[^§]*§\([^\"]*\)\"[^ç]*ç\([^£]*\)[^£]*£/\\e[01;32m\2\\e[00;37m \\e[01;34m[ \\e[01;35m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' |
	sed -e 's/&lt;liç/\n \\e[01;34m*\\e[00;37m /g' |
	sed -e 's/<[^>]*>/ /g' |
	sed -e 's/&lt;[^ç]*ç//g' |
	sed -e 's/[磧]//g')\n\n"

Last edited by grufo (2012-08-09 23:17:43)

Offline

#52 2012-08-09 23:24:46

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: Cool Functions for your .bashrc file

Or if you want a generic (but still coloured) function for your .bashrc file...:

feed() {
	# The characters "ç, £, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g') | \
		sed -e 's/&gt;/ç/g' |
		sed -e 's/&lt;\/aç/£/g' |
		sed -e 's/href\=\"/§/g' |
		sed -e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' |
		sed -e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' |
		sed -e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' |
		sed -e 's/&lt;pç/\n/g' |
		sed -e 's/&lt;bç\|&lt;strongç/\\e[01;30m/g' -e 's/&lt;\/bç\|&lt;\/strongç/\\e[00;37m/g' |
		sed -e 's/&lt;a[^§]*§\([^\"]*\)\"[^ç]*ç\([^£]*\)[^£]*£/\\e[01;32m\2\\e[00;37m \\e[01;34m[ \\e[01;35m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' |
		sed -e 's/&lt;liç/\n \\e[01;34m*\\e[00;37m /g' |
		sed -e 's/<[^>]*>/ /g' |
		sed -e 's/&lt;[^ç]*ç//g' |
		sed -e 's/[磧]//g')\n\n"
}

Usage:

$ feed http://www.archlinux.org/feeds/news

Offline

#53 2012-08-14 18:01:23

mhtrinh
Member
Registered: 2010-10-06
Posts: 19

Re: Cool Functions for your .bashrc file

# Change cd function to enable to go back
function cd() {
        if [ $# -lt 1 ]; then
                pushd ~ > /dev/null
        else
                pushd "$@" > /dev/null
        fi
}

alias b='popd > /dev/null; pwd'

BTW : is "function" or "alias" use preferable ?
TODE : change dir forward :-p

Offline

#54 2012-08-14 23:23:54

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,229
Website

Re: Cool Functions for your .bashrc file

Not directly a function, but related.... I found this on identica the other day:
https://github.com/josephwecker/bashrc_dispatch

Offline

#55 2012-08-15 05:40:46

sujoy
Member
From: Finland
Registered: 2008-02-08
Posts: 95

Re: Cool Functions for your .bashrc file

grufo wrote:

Or if you want a generic (but still coloured) function for your .bashrc file...:

feed() {
	# The characters "ç, £, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g') | \
		sed -e 's/&gt;/ç/g' |
		sed -e 's/&lt;\/aç/£/g' |
		sed -e 's/href\=\"/§/g' |
		sed -e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' |
		sed -e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' |
		sed -e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' |
		sed -e 's/&lt;pç/\n/g' |
		sed -e 's/&lt;bç\|&lt;strongç/\\e[01;30m/g' -e 's/&lt;\/bç\|&lt;\/strongç/\\e[00;37m/g' |
		sed -e 's/&lt;a[^§]*§\([^\"]*\)\"[^ç]*ç\([^£]*\)[^£]*£/\\e[01;32m\2\\e[00;37m \\e[01;34m[ \\e[01;35m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' |
		sed -e 's/&lt;liç/\n \\e[01;34m*\\e[00;37m /g' |
		sed -e 's/<[^>]*>/ /g' |
		sed -e 's/&lt;[^ç]*ç//g' |
		sed -e 's/[磧]//g')\n\n"
}

Usage:

$ feed http://www.archlinux.org/feeds/news

this is awsome smile

Offline

#56 2012-08-15 07:27:43

args
Member
Registered: 2012-03-06
Posts: 9

Re: Cool Functions for your .bashrc file

function cd() {
    if [[ ! $PWD =~ ^$HOME ]];then
        builtin cd $1
    elif [[ $# -ne 0 ]];then
        builtin cd $1
    elif [[ -e .git || -e .hg || -e .project_root ]];then
        builtin cd
    else
        while [[ "$PWD" != "$HOME" ]]
        do
            [[ -e .git || -e .hg || -e .project_root ]] && break
            builtin cd ..
        done
    fi
}

but, it contains a bug, When a directory name includes blank or something wired unusual, this function will fail ;-(


Apple hater. Panda lover.

Offline

#57 2012-08-15 11:24:57

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: Cool Functions for your .bashrc file

mhtrinh wrote:
# Change cd function to enable to go back
function cd() {
        if [ $# -lt 1 ]; then
                pushd ~ > /dev/null
        else
                pushd "$@" > /dev/null
        fi
}

alias b='popd > /dev/null; pwd'

BTW : is "function" or "alias" use preferable ?
TODE : change dir forward :-p

huh. `cd -` always takes you back to the previous directory.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#58 2012-08-15 14:19:15

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Cool Functions for your .bashrc file

mhtrinh wrote:

BTW : is "function" or "alias" use preferable ?

Nothing wrong with aliases but from the Bash manual:

For almost every purpose, aliases are superseded by shell functions

I'd be surprised if they ever deprecated them. wink


Edit to add a function. Can't remember where I got it from, but it's not mine.

# nicely formatted mount
nmount() { (echo "DEVICE PATH TYPE FLAGS" && mount | awk '$2=$4="";1') | column -t ; }

Last edited by skanky (2012-08-15 14:26:30)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#59 2012-08-15 14:50:49

gaenserich
Member
From: Michigan
Registered: 2010-06-07
Posts: 119
Website

Re: Cool Functions for your .bashrc file

grufo wrote:

Latest news from Arch Official website (but you can change the source: this script is simply a stupid - but coloured :-D - rss reader):

# The characters "ç, £, §" are used as metacharacters. They should not be encountered in a feed...
echo -e "$(echo $(curl --silent http://www.archlinux.org/feeds/news/ | sed -e ':a;N;$!ba;s/\n/ /g') | \
	sed -e 's/&gt;/ç/g' |
	sed -e 's/&lt;\/aç/£/g' |
	sed -e 's/href\=\"/§/g' |
	sed -e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' |
	sed -e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' |
	sed -e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' |
	sed -e 's/&lt;pç/\n/g' |
	sed -e 's/&lt;bç\|&lt;strongç/\\e[01;30m/g' -e 's/&lt;\/bç\|&lt;\/strongç/\\e[00;37m/g' |
	sed -e 's/&lt;a[^§]*§\([^\"]*\)\"[^ç]*ç\([^£]*\)[^£]*£/\\e[01;32m\2\\e[00;37m \\e[01;34m[ \\e[01;35m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' |
	sed -e 's/&lt;liç/\n \\e[01;34m*\\e[00;37m /g' |
	sed -e 's/<[^>]*>/ /g' |
	sed -e 's/&lt;[^ç]*ç//g' |
	sed -e 's/[磧]//g')\n\n"

Actually, if you want to ostensively optimize this a bit more, just take out all the piped sed invocations and just use the -e extension repeatedly, e.g.:

echo -e "$(echo $(curl --silent http://www.archlinux.org/feeds/news/ | sed -e ':a;N;$!ba;s/\n/ /g') | \
	sed -e 's/&gt;/ç/g' \
	-e 's/&lt;\/aç/£/g' -e \
	-e 's/href\=\"/§/g' -e \
	-e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g'  \
	-e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' \
	-e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' \
	-e 's/&lt;pç/\n/g' \
	-e 's/&lt;bç\|&lt;strongç/\\e[01;30m/g' -e 's/&lt;\/bç\|&lt;\/strongç/\\e[00;37m/g' \
	-e 's/&lt;a[^§]*§\([^\"]*\)\"[^ç]*ç\([^£]*\)[^£]*£/\\e[01;32m\2\\e[00;37m \\e[01;34m[ \\e[01;35m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' \
	-e 's/&lt;liç/\n \\e[01;34m*\\e[00;37m /g' \
	-e 's/<[^>]*>/ /g' \
	-e 's/&lt;[^ç]*ç//g' \
	-e 's/[磧]//g')\n\n"

As a general rule, the fewer pipes, the better the performance, although this script's bottle neck is most likely comes via the network. In general, sed is an expensive operation as well (as I've found out via hostsblock), so I try to use as much grep and coreutils utilities like tr, cut, etc. as possible.


Check out hostsblock for system-wide ad- and malware-blocking.

Offline

#60 2012-08-15 15:49:03

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Cool Functions for your .bashrc file

@gaenserich you probably missed this thread https://bbs.archlinux.org/viewtopic.php?id=146850


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#61 2012-08-23 11:25:15

mhtrinh
Member
Registered: 2010-10-06
Posts: 19

Re: Cool Functions for your .bashrc file

Dieter@be wrote:

huh. `cd -` always takes you back to the previous directory.

I didn't know that. Thanks :-)
But I tested and it seems to work only once, you can go back to previously once then it loop back.

skanky wrote:

Nothing wrong with aliases but from the Bash manual:

Ok thanks. I will use alias when I can put every thing in one command line and in function for more complexe things

Offline

Board footer

Powered by FluxBB