Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
4 total  XML / RSS feed 

Easy way to zip dirs in zsh

This is just a quick way to zip dirs from zsh, while ignoring unwanted files like .DS_Store and .svn/*.

zipdir() { zip "$@".zip -r "$@" -x "*/.DS_Store" "*/.svn/*" }

Shell keepalive script (zsh)

Disclaimer: I do not use this script on TextDrive, due to resource consumption concerns, and would not recommend anyone else doing so without express permission from TPTB.

#!/usr/bin/env zsh

# keepalive
# 
# This is keepalive, a simple zsh script (may be hackable to work for other
# shells) for keeping things running.
# 
# Usage:
# $ keepalive  [command args]
# $ keepalive ssh -N -L 8080:127.0.0.1:8080
# 
# The next three variables can be edited to adjust the action of the script

# If the command dies ${threshold} times in succession without living more
# than ${min_runtime} seconds the script will quit.
local threshold=5
local min_runtime=60

# The script will sleep ${init_sleep_time} seconds after the first failure,
# ${init_sleep_time}*2 after the second, ${init_sleep_time}*4 times after the 
# third, and so on.
local init_sleep_time=5

# ########################### #
# No need to edit below here. #
# ########################### #
local time_started time_ended 
local sleep_time=${init_sleep_time}
local times_until_fail=${threshold}

until (( times_until_fail <= 0 )); do
  time_started=$(date +%s)
  $*
  time_ended=$(date +%s)

  # reset if it's lived longer than ${min_runtime}
  if (( (${time_ended} - ${time_started}) > ${min_runtime} )); then
    times_until_fail=${threshold}
    sleep_time=${init_sleep_time}
  fi

  (( times_until_fail -= 1 ))
 
  echo
  echo "-- $(date +"%d/%m/%y %H:%M:%S"): \`$*\` died!"
  if (( times_until_fail >= 1 )); then
    echo "-- ${times_until_fail} more executions lasting less than ${min_runtime} seconds will result in job failure."
    echo "-- Sleeping ${sleep_time} seconds..."
    echo
    sleep ${sleep_time}
  fi
  
  (( sleep_time *= 2 ))
done

echo "-- Died too many times in too short a space of time. Exiting!"

Bash function to copy SSH DSA public key to a new server

Or, "How to login over SSH without a password".

First of all, you'll need to have generated an SSH public DSA key using the following commands:

cd ~ && ssh-keygen -t dsa


Then, you can add the following bash function to your .bashrc (or similar) and just type 'scpdsa user@hostname' to add your public key to your 'authorized_keys' file on the server.

function scpdsa {
  if [[ -z "$1" ]]; then
    echo "!! You need to enter a hostname in order to send your public key !!" 
  else
    echo "* Copying SSH public key to server..." 
    ssh ${1} "mkdir -p ~/.ssh && cat - >> ~/.ssh/authorized_keys" < ~/.ssh/id_dsa.pub
    echo "* All done!"
  fi
}


-- updated to use simpler and quicker method.

Marten's .zshrc

export PATH=$PATH:/usr/local/bin:/opt/local/bin

# Aliases and functions {{{1

# Xterm resizing-fu. Note that these are utf-8 fonts
alias hide='echo -en "\033]50;nil2\007"'
alias tiny='echo -en "\033]50;-misc-fixed-medium-r-normal--8-80-75-75-c-50-iso10646-1\007"'
alias small='echo -en "\033]50;6x10\007"'
alias default='echo -e "\033]50;-misc-fixed-medium-r-semicondensed--13-*-*-*-*-*-iso10646-1\007"'
alias medium='echo -en "\033]50;-misc-fixed-medium-r-normal--13-120-75-75-c-80-iso10646-1\007"'
alias large='echo -en "\033]50;-misc-fixed-medium-*-*-*-15-*-*-*-*-*-iso10646-1\007"'
# This is a large font that has a corresponding double-width font for 
# CJK and other characters, useful for full-on utf-8 goodness.
alias larger='echo -en "\033]50;-misc-fixed-medium-r-normal--18-*-*-*-*-*-iso10646-1\007"'
alias huge='echo -en "\033]50;-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso10646-1\007"'
alias normal=default
if [ "$TERM" = "xterm" ] && [ "$LINES" -ge 50 ] && [ "$COLUMNS" -ge 100 ]; then
        large
fi

# Allow for use of =command to test for commands below, w/o error messages.
setopt nonomatch

alias df='df -h -x none'
alias free='free -m'
alias cls=clear
# Do we have GNU ls of a new enough version for color?
(ls --help 2>/dev/null |grep -- --color=) >/dev/null && \
        alias ls='ls -b -CF --color=auto'
alias l=ls
alias dir='ls -lha'
alias du='du -h'
alias f=finger
alias edit=$EDITOR
alias e=$EDITOR
alias ftp=lftp
alias md=mkdir
# Put alive hosts at the bottom.
alias ruptime='ruptime -rt'
alias rd=rmdir
alias k=killall
alias mtr='mtr --curses'
[[ -x =reportbug ]] && alias bug='reportbug -b --no-check-available -x -M --mode=expert'
alias slrn='slrn -n'
alias menu=pdmenu
alias xchat='xchat -C'
alias stardate='date "+%y%m.%d/%H%M"'
if [[ -x =ytalk ]]; then
        alias ytalk='ytalk -x'
        alias talk=ytalk
fi
# Finger macros alias.
if [[ -e /proc/acpi ]]; then
        alias apm=acpi
fi
# I don't like the zsh builtin time command.
[[ -x =time ]] && alias time='command time'
alias dch="dch -p -D UNRELEASED"
alias bc='bc -l -q'
alias ytalk='ytalk -x'

# QX3 toy microscope
alias bottomlight="echo bottomlight: on >!/proc/cpia/video0; echo toplight: off >!/proc/cpia/video0"
alias toplight="echo toplight: on >!/proc/cpia/video0; echo bottomlight: ff >!/proc/cpia/video0";
alias nolight="echo toplight: off >!/proc/cpia/video0; echo bottomlight: off >!/proc/cpia/video0"
alias bothlights="echo toplight: on >!/proc/cpia/video0; echo bottomlight: on >!/proc/cpia/video0"

# Longrun stuff.
alias slow='longrun -f economy; longrun -s 0 50; longrun -p'
alias fast='longrun -f performance; longrun -s 0 100; longrun -p'
# useful for games with auto rate limiting
alias full='longrun -f performance; longrun -s 100 100; longrun -p'

# Stuff that I just don't want to run, because it sucks.
alias eview='echo "eview? No"'
# Argh, mf is so bloody annoying.
alias mf='echo "Er, if you want mv, type mv; if you want mf, I pity you."'

# Calculator
calc () { echo $* |bc -l }

# This fingers @host.
@ () { finger @$1 }


# Not for root.
if [[ "$USER" != root ]]; then
        alias cardctl='sudo cardctl'
        alias fetchnews='sudo fetchnews'
fi

# I don't like this for interactive use.
setopt nomatch

# Prompt {{{1

coreprompt="%n@%m"

# The extraprompt thing can be set when I have chroots and I want to keep
# track of which I am in.
if [ ! -e /etc/zsh.extraprompt ]; then
        baseprompt="$coreprompt:%~"
else
        baseprompt="$coreprompt(`cat /etc/zsh.extraprompt`):%~"
fi

# The prompt shows up in xterm titles too.
case $TERM in
   xterm|color_xterm|screen)
        # Beware: next line has embedded raw control characters..
        PROMPT="%{]0;$baseprompt%}$baseprompt> "

        # Before each command is run, throw the command line up into the
        # titlebar! (The only problem is that very quick commands flicker..)
        preexec () {
                print -nP '\033]0;'
                print -nP "$coreprompt: "
                # Truncate at 60 chars, escape %, escape invisibles
                print -nPR "%60>...>${(V)1//\%/%%}"
                print -n '\007'
                # Also set the screen window title to programs I run.
                if [ "$TERM" = screen ]; then
                        print -nP '\033k%'
                        print -nPR "%60>...>${(V)1//\%/%%}"
                        print -n '\033\\'
                fi
        }
        
        # Add to the prompt to unset screen window title after program
        # exits.
        if [ "$TERM" = screen ]; then
                PROMPT="%{kzsh\\%}$PROMPT"
        fi

  ;;
  *) PROMPT="$baseprompt>"
  ;;
esac
unset baseprompt
[[ UID -eq 0 ]] && PROMPT=${PROMPT/\}%n/\}%U%n%u}

# Misc {{{1

limit core 0          # no core dumps
umask 022        # no user groups any more
setopt nopromptcr # No cr before prompt
#setopt extendedglob      # Lots of cool extra wildcards
#setopt mailwarning       # New mail?
setopt correct            # Correct commands
# Note that the following comment makes me cringe now, but is left in for
# historical amusement value.
setopt autolist automenu nobeep     # Filename completion: the best of csh and 4dos!
setopt autocd             # Exec directory to cd there
setopt noclobber noflowcontrol
#setopt autoresume
setopt print_exit_value
setopt list_packed
unsetopt bgnice

# History setup
setopt share_history
setopt csh_junkie_history
setopt hist_ignore_dups
setopt hist_allow_clobber
setopt hist_reduce_blanks

# Force emacs editing mode.
[[ $TERM == "xterm" ]] && bindkey -e

# ctrl-s will no longer freeze the terminal.
#stty -ixon kill ^K
stty erase "^?"

# Work around ion/xterm resize bug.
#if [ "$SHLVL" = 1 ]; then
# if [ -x `which resize 2>/dev/null` ]; then
#         eval `resize 
# fi
#fi

eval `dircolors 2>/dev/null` # set up color-ls variables
watch=(notme)   # watch for everybody but me
LOGCHECK=120    # check every 2 min for login/logout activity

if [ -e ~/.security-environment ]; then
        printf "Current security environment: "
        cat ~/.security-environment
fi

# Turn on full-featured completion (needs 2.1) {{{2
if [[ "$ZSH_VERSION" == (3.1|4)* ]]; then
        autoload -U compinit
        compinit
        if [[ -e ~/.ssh/known_hosts ]]; then
                # Use .ssh/known_hosts for hostnames.
                hosts=(${${${(f)"$(<$HOME/.ssh/known_hosts)"}%%\ *}%%,*})
                zstyle ':completion:*:hosts' hosts $hosts 
        fi
fi
        
# Key bindings {{{1

bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward
bindkey '^K' kill-whole-line
bindkey -s '^L' "|less\n"         # ctrl-L pipes to less
bindkey -s '^B' " &\n"                        # ctrl-B runs it in the background
bindkey "\e[1~" beginning-of-line # Home (console)
bindkey "\e[H" beginning-of-line  # Home (xterm)
bindkey "\e[4~" end-of-line               # End (console)
bindkey "\e[F" end-of-line                # End (xterm)
bindkey "\e[2~" overwrite-mode            # Ins
bindkey "\e[3~" delete-char               # Delete
« Newer Snippets
Older Snippets »
4 total  XML / RSS feed