Haha, that's fine, probably better, because I spend a little too much time thinking about how I could be doing my everyday scripting more efficiently. I'm crazy enough to make an alias for ping -c 5 8.8.8.8 just so I don't have to type it out every time.
Cool! I think I've seen that spawn function before - normally I just disable all output and make it a daemon. There's still a little output from the daemon, but I can still kill it if I want.
There's a lot of interesting sort functions there, too. Nice!
Updated for the month of november, gone are my days of power line(yay).
# /etc/skel/.bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
# Put your fun stuff here.
# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth
# Add to history instead of overriding it
shopt -s histappend
# History lenght
HISTSIZE=1000
HISTFILESIZE=2000
# Window size sanity check
shopt -s checkwinsize
# User/root variables definition
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# Colored XTERM promp
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# Colored prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_prompt=yes
else
color_prompt=
fi
fi
# Prompt
if [ -n "$SSH_CONNECTION" ]; then
export PS1="\[$(tput setaf 1)\]โโโผ \[$(tput setaf 7)\][\w]\n\[$(tput setaf 1)\]\$(if [[ \$? == 0 ]]; then echo \"\[$(tput setaf 1)\]โโโโโโผ \[$(tput setaf 7)\][ssh]\"; else echo \"\[$(tput setaf 1)\]โโผ \[$(tput setaf 7)\][ssh]\"; fi) \[$(tput setaf 7)\]"
else
export PS1="\[$(tput setaf 1)\]โโโผ \[$(tput setaf 7)\][\w]\n\[$(tput setaf 1)\]\$(if [[ \$? == 0 ]]; then echo \"\[$(tput setaf 1)\]โโโโโโผ\"; else echo \"\[$(tput setaf 1)\]โโผ\"; fi) \[$(tput setaf 7)\]"
fi
trap 'echo -ne "\e[0m"' DEBUG
# I this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u: \w\a\]$PS1"
;;
*)
;;
esac
# Color support
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
fi
# Alias definitions.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Auto-completion
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# Advanced directory creation
function mkcd {
if [ ! -n "$1" ]; then
echo "Entrez un nom pour ce dossier"
elif [ -d $1 ]; then
echo "\`$1' existe dรฉjร "
else
mkdir $1 && cd $1
fi
}
# Go back with ..
b() {
str=""
count=0
while [ "$count" -lt "$1" ];
do
str=$str"../"
let count=count+1
done
cd $str
}
# Color man pages
man() {
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 "$@"
}
# Auto cd
shopt -s autocd
# ls after a cd
function cd()
{
builtin cd "$*" && ls
}
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}`
Update to mine, I added a couple of functions to manage daemons.
dae () {
chmod +x $1 # just in case it's not already executable (careful)
daeout=$2
if [ -z $daeout ] ; then daeout=/dev/null ; fi
setsid $1 >$daeout 2>&1 < /dev/null &
}
kill_dae () {
daeproc=$(ps -fade | grep $1 | grep -v grep | awk '{print $2}')
if [ "$(ps --no-headers s $daeproc | wc -l)" == 1 ] ; then
kill $daeproc
echo "agent $daeproc out of service."
else
echo "bad process name - try being more or less specific"
fi
}
I'm also working on something that saves specific files to a server and I can call sync to get up to date for my different computers. I haven't gotten it working yet, though.
Haha, I remember when I first started using Linux I was so confused why it didn't say anything when I changed directories. Now I guess I'm so used to it I don't even think about that anymore. That's useful, thanks.
I never did and still dont really muck around much with my bashrc's (i'm lazy and shit at scripting). but i did start on this to make dealing with different files through the terminal a bit more fluid.
# Fuck my life
open () {
if [ -f $1 ] ; then
case $1 in
*.txt) leafpad $1 ;;
*.mp3) mocp $1 ;;
*.png) ristretto $1 ;;
esac
fi
}
P.S i'm the biggest scripting noob. so if you have any hints or tips(you are the CS major here) it would be gratefully accepted .
I just enjoy mucking around with bash scripts and i thought it would be something i could do to try some new idea's. i have never read any guides on learning about bash scripting so i usually just sit down with a beer and make this shit up as i go along. entertainment at its best eh?
I was procrastinating on my paper, so I wrote a base conversion function. Surprisingly fun!
# convert value $1 from base $2 to base $3 (default 10)
convertbase () {
# Who needs two assoc arrays when you can be clever? Also, makes it easier to add more
terminlist=(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O)
# warning: if expanding with lowercase letters, you should change the third line below
# to deprecate dynamic case binding: "${1^^}" to "$1"
termoutlist () {
for each in ${!terminlist[@]} ; do
if [[ "${terminlist[$each]}" == "${1^^}" ]] ; then
echo $each
return
fi
done
}
# only convert from base10 if user says it is not in base10
val=0
if [[ $2 != 10 ]] ; then
# basically, uh, sum all (column * (base)^(nth from right starting at 0))
# ex: 1101 (in base 2) = 1*2^3 + 1*2^2 + 0+2^1 + 1*2^0 = 8 + 4 + 0 + 1 = 13
for i in $(seq 0 $((${#1}-1))) ; do
let val+=$(($(termoutlist ${1:i:1})*$(($2**$((${#1}-$(($i+1))))))))
done
else
val=$1
fi
# private recursive function to convert val to base$2
# (seems like the easiest way to do it)
tobase () {
if [[ $1 -lt $2 ]] ; then
echo ${terminlist[$1]}
else
echo "$(tobase $((${1}/${2})) $2)${terminlist[$((${1}%${2}))]}"
fi
}
if [[ $# -ge 3 && $3 != 10 ]] ; then
echo $(tobase $val $3)
else
echo $val
fi
}