bash functions

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

bash functions

Unread post by machinebacon » Mon May 04, 2015 6:02 pm

Not sure if we had this before or not, sorry if yes - please merge.

Code: Select all

function pac() { links https://packages.debian.org/en/sid/"$@" ;}
function bug() { links http://bugs.debian.org/"$@" ;}
pac <packagename> shows package information on the Debian site
bug <packagename> opens the bug tracker. For those who don't have apt-listbugs.

Code: Select all

transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi 
tmpfile=$( mktemp -t transferXXX ); if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" >> $tmpfile; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" >> $tmpfile ; fi; cat $tmpfile; rm -f $tmpfile; }; alias transfer=transfer 
needs curl -- transfer <filename> puts a file onto a hoster and gives a shortcut link back.

Code: Select all

function encode() { echo -n $@ | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'; }
function goo() { links http://www.google.com/search?q="`encode $@`" ;}
function wikide() { links http://de.wikipedia.org/w/index.php?search="`encode $@`" ;}
function wikien() { links http://en.wikipedia.org/w/index.php?search="`encode $@`" ;}
function wikihu() { links http://hu.wikipedia.org/w/index.php?search="`encode $@`" ;}
needs perl -- quite self-explaining, add your language if you wish.

A classic:

Code: Select all

export MARKPATH=$HOME/.marks
function jump { 
    cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark { 
    mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark { 
    rm -i "$MARKPATH/$1"
}
function marks {
    ls -l "$MARKPATH" | sed 's/  / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
}
put bookmarks into those folders you often use ('mark' and 'unmark' them) and access them with 'jump', list them with 'marks'
..gnutella..

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

Re: bash functions

Unread post by pidsley » Tue May 05, 2015 5:26 am

I think everyone already has this one -- cd then ls in the new directory:

Code: Select all

cd() {
    builtin cd "$@" && ls
}
I use this next one on irc when I want to see a config file -- some systems don't have pastebinit, others don't have wgetpaste, almost everyone has curl and sprunge.

Upload a config to sprunge and give me a link:

Code: Select all

spr() {
    echo "cat $1 | curl -F 'sprunge=<-' http://sprunge.us"
    cat $1 | curl -F 'sprunge=<-' http://sprunge.us
}
The "echo" line is of course not required, but I often add an echo to my bash functions so I can see what they are doing (because I sometimes forget what I have aliased and like to see what is happening).

I use the "marks" functions a lot (so much that I renamed them "mk" and "go") and I modified the "marks" function to use awk instead of sed and cut:

Code: Select all

function mks {
    ls -o "$MARKPATH" | awk '/^lrw/ { print $8,$9,$10 }'
}
And one I don't use often enough on irc -- give me a tinyurl:

Code: Select all

tiny() { 
    curl -sf http://tinyurl.com/api-create.php?url="$1";
    echo
}

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

Re: bash functions

Unread post by rhowaldt » Tue May 05, 2015 9:44 am

that echo is really smart! thanks :)
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: bash functions

Unread post by machinebacon » Sat May 30, 2015 8:53 am

Code: Select all

function lb() { fc -lnr -1 >> ~/logbook ;}
function lc() { rm ~/logbook ;}
The 'lb' function stores the last command in a logbook. This is useful if you want to try a set of commands and put them into a file to create a bash script.
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: bash functions

Unread post by GekkoP » Sat May 30, 2015 9:02 am

^ Neat trick.

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

Re: bash functions

Unread post by machinebacon » Sat May 30, 2015 9:08 am

^ The dirty thing is that it adds a tab and a whitespace before the command, but at least shit gets stored directly :)
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: bash functions

Unread post by GekkoP » Sat May 30, 2015 9:09 am

^ Well, we can clean it up with Emacs, can't we? ;)

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

Re: bash functions

Unread post by machinebacon » Sat May 30, 2015 9:15 am

^ haha I wanted to write that indeed! M-x delete-whitespace-rectangle I'd say!
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash functions

Unread post by dkeg » Sat May 30, 2015 12:07 pm

its funny, I alway's forget I have this. Think I actually picked it up on /r/unixporn

Code: Select all

# up directory; replace cd ../..; usage up, up 2, so on
up() {
    local d=""
    limit=$1
    for ((i=1; i <= limit; i++))
    do
        d=$d/..
    done
    d=$(echo $d | sed 's/^\///')
    if [ -z "$d" ]; then
        d=..
    fi
    cd $d
}

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash functions

Unread post by dkeg » Sat Jun 20, 2015 2:45 am

stupid simple, but helpful git function

Code: Select all

function gitpush() {
    # usage: gitpush [ . |'files'] ['commit message'] [branchname]  
    # gitpush newfile 'add new file' master
    args=("$@")
        git add "${args[0]}" && git commit -m "${args[1]}" && git push -u origin ${args[2]}
}
Edit: wrapped quotes around args 0 and 1. Sorry.

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash functions

Unread post by dkeg » Sun Jun 21, 2015 2:51 am

kind of been messing around with bash functions this past week. Added a function for adding a new wall to .xinintc.

finds the current; deletes it; adds the new
EDIT: forgot to add the option of keeping current; commenting it out.
Note: Unfortunately, the function only works with one hsetroot b/c $line is based on locating hsetroot. When multiple are returned; it breaks. I tried to cut out the first return, but just could not get it to work

Code: Select all

# quickly add a wall to .xinitrc;adjust as needed
# usage: wallput [tile|fill] [wall] [option] [option]
# example: wallput fill wall.jpg '-blur 10'
function wallput() {
    file=$HOME/.xinitrc
    path=/data/walls/
    cur=$(cat $file|grep hsetroot|awk '{print $1}')
    line=$(awk '/hsetroot/{ print NR; }' $file)
    args=("$@")
         # use this to replace current
             sed -i "/$cur/d" $file  
         # use this to keep and comment current                
             #sed -i "/$cur/s/^/# /" $file       
         wall=$(hsetroot -${args[0]} $path${args[@]:1} &)
         sed -i "${line}i hsetroot -${args[0]} $path${args[@]:1} &" $file
         $wall
}

Work hard; Complain less

User avatar
elixir
Weight Watcher
Posts: 357
Joined: Fri Feb 21, 2014 8:25 am

Re: bash functions

Unread post by elixir » Sun Jun 21, 2015 6:04 am

^ Thank you for the share, dkeg. That is awesome! I am working on something similar in a way... I will post it on the command line utilities thread when finished :)
Out of the corner of your eye you spot him... Shia LaBeouf.

https://www.youtube.com/watch?v=o0u4M6vppCI

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bash functions

Unread post by dkeg » Sun Jun 21, 2015 1:27 pm

excellent!

Work hard; Complain less

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

Re: bash functions

Unread post by wuxmedia » Sun Oct 04, 2015 11:19 am

resurrect!
re-hashed this one from optik, just made it easier, requires youtube-dl and ffmpeg or avconf

Code: Select all

function getmp3 () {
youtube-dl -x --audio-format mp3 --audio-quality 0 "$1"
}
useage:
$ getmp3 https://youtu.be/z4UDNzXD3qA
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
daggoth
runs Stable
Posts: 36
Joined: Mon Mar 10, 2014 8:16 am

Re: bash functions

Unread post by daggoth » Sun Oct 04, 2015 12:29 pm

Two more from my ~/.bash_alias...

popsort() takes the output of "apt-cache search" or "aptitude search", and sorts the listed packages according to their ranking on Debian's popularity-contest.

Code: Select all

##  Typical usage :  apt-cache search curses | popsort | less -S
##                   aptitude  search curses | popsort | grep -v ^lib
popsort()  {
    cd /tmp && [[ -s by_inst.gz ]] || wget http://popcon.debian.org/by_inst.gz 2>/dev/null
    [[ -s by_inst.gz ]] || { echo "* No Internet? Exiting..." ; return 1 ; }
    gawk ' ARGIND == 1 { if ($1 ~ /^[0-9]+$/) x[$2] = $1 ; next }
           $2 != "-"   { sub(/^\w+\s+/, "") }
           $2 == "-"   { if ($1 in x) y[x[$1]] = $0 }
           END { PROCINFO["sorted_in"] = "@ind_num_asc"
                 for (i in y) print y[i] } '  <(zcat by_inst.gz ) -
}
mtoc() prints out a manpages table of contents.

Code: Select all

function mtoc() { man --ascii "$1" 2>/dev/null | awk '
            BEGIN { a[""]++ }
            /^\s{0,4}\S/ && !($0 in a) { a[$0]++ ; b[i++]=$0 }
            END { for (j=0 ; j<i ; j++) print b[j] }'
}

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

Re: bash functions

Unread post by machinebacon » Mon Feb 01, 2016 2:42 pm

Code: Select all

function show() { wget -c http://$@ -O-|html2text|less ;}
needs html2text. shows content of a website (entered without http://) in less
..gnutella..

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

Re: bash functions

Unread post by ivanovnegro » Tue Feb 02, 2016 2:10 pm

Very nice.

User avatar
elixir
Weight Watcher
Posts: 357
Joined: Fri Feb 21, 2014 8:25 am

Re: bash functions

Unread post by elixir » Mon Jul 03, 2017 9:42 pm

Reviving this thread. Simple bash function I would use to create new text files for notes. It was very useful for my university courses.

nn = new note

Code: Select all

function nn {
        datestr=$(date '+%Y%m%d')

        if [ -f ${datestr}.txt ]; then
                timestr=$(date '+%H%M')
                touch "${datestr}_${timestr}.txt"
        else
                touch "${datestr}.txt"
        fi
}
Out of the corner of your eye you spot him... Shia LaBeouf.

https://www.youtube.com/watch?v=o0u4M6vppCI

Post Reply