Page 1 of 1

bash functions

Posted: Mon May 04, 2015 6:02 pm
by machinebacon
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'

Re: bash functions

Posted: Tue May 05, 2015 5:26 am
by pidsley
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
}

Re: bash functions

Posted: Tue May 05, 2015 9:44 am
by rhowaldt
that echo is really smart! thanks :)

Re: bash functions

Posted: Sat May 30, 2015 8:53 am
by machinebacon

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.

Re: bash functions

Posted: Sat May 30, 2015 9:02 am
by GekkoP
^ Neat trick.

Re: bash functions

Posted: Sat May 30, 2015 9:08 am
by machinebacon
^ The dirty thing is that it adds a tab and a whitespace before the command, but at least shit gets stored directly :)

Re: bash functions

Posted: Sat May 30, 2015 9:09 am
by GekkoP
^ Well, we can clean it up with Emacs, can't we? ;)

Re: bash functions

Posted: Sat May 30, 2015 9:15 am
by machinebacon
^ haha I wanted to write that indeed! M-x delete-whitespace-rectangle I'd say!

Re: bash functions

Posted: Sat May 30, 2015 12:07 pm
by dkeg
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
}

Re: bash functions

Posted: Sat Jun 20, 2015 2:45 am
by dkeg
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.

Re: bash functions

Posted: Sun Jun 21, 2015 2:51 am
by dkeg
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
}

Re: bash functions

Posted: Sun Jun 21, 2015 6:04 am
by elixir
^ 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 :)

Re: bash functions

Posted: Sun Jun 21, 2015 1:27 pm
by dkeg
excellent!

Re: bash functions

Posted: Sun Oct 04, 2015 11:19 am
by wuxmedia
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

Re: bash functions

Posted: Sun Oct 04, 2015 12:29 pm
by daggoth
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] }'
}

Re: bash functions

Posted: Mon Feb 01, 2016 2:42 pm
by machinebacon

Code: Select all

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

Re: bash functions

Posted: Tue Feb 02, 2016 2:10 pm
by ivanovnegro
Very nice.

Re: bash functions

Posted: Mon Jul 03, 2017 9:42 pm
by elixir
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
}