LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-25-2009, 10:06 PM   #1
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Rep: Reputation: 15
How can I stream line or optimize a .bashrc file


Hello all,

The code listed below is a copy of my .bashrc file that I use on my HP compaq 6715b laptop I use Suse version 11.0 I have no need to upgrade to version 11.1 for now.

I have been working on my .Bashrc file for some time now and as you can see it is very long.

Is there any way to stream line or optimize this file?

fell free to rip it apart use for your own needs. if you have any cool items (alias or functions) please share them.

Code begins--------->
#######################################################
# your name.bashrc file
# Your web site
# Your Blog site
#
# Last Modified 04-25-2009
# Running on OpenSuSE 11
#######################################################
# EXPORTS
#######################################################
PATH=$PATH:/usr/lib/festival/ ;export PATH
export PS1="[\[\033[1;34m\w\[\033[0m]\n[\t \u]$ "
export EDITOR=/usr/bin/nano
# History Time Format ='%F %T'
HISTTIMEFORMAT='%m-%d %H:%M:%S | '
export HISTSIZE=100000000
export HISTFILESIZE=100000000 # the bash history should save 100000000 commands
export HISTCONTROL=ignoredups #don't put duplicate lines in the history.
alias hist='history | grep $1' #Requires one input
# Define a few Color's
#######################################################
BLACK='\e[0;30m'
BLUE='\e[0;34m'
GREEN='\e[0;32m'
CYAN='\e[0;36m'
RED='\e[0;31m'
PURPLE='\e[0;35m'
BROWN='\e[0;33m'
LIGHTGRAY='\e[0;37m'
DARKGRAY='\e[1;30m'
LIGHTBLUE='\e[1;34m'
LIGHTGREEN='\e[1;32m'
LIGHTCYAN='\e[1;36m'
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m'************* # No Color
# ALIAS'S OF ALL TYPES SHAPES AND FORMS
#######################################################
# Alias's to control hardware
alias cdo='eject /dev/cdrecorder'
alias cdc='eject -t /dev/cdrecorder'
alias dvdo='eject /dev/dvd'
alias dvdc='eject -t /dev/dvd'
alias scan='scanimage -L'
alias playw='for i in *.wav; do play $i; done'
alias playo='for i in *.ogg; do play $i; done'
alias playm='for i in *.mp3; do play $i; done'
alias copydisk='dd if=/dev/dvd of=/dev/cdrecorder' # Copies bit by bit from dvd to cdrecorder drives.
alias dvdrip='vobcopy -i /dev/dvd/ -o ~/DVDs/ -l'
# Alias's to modified commands
#######################################################
alias ps='ps auxf'
alias home='cd ~'
alias pg='ps aux | grep'* #requires an argument
alias mountedinfo='df -hT'
alias ping='ping -c 10'
alias openports='netstat -nape --inet'
alias ns='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
alias du1='du -h --max-depth=1'
alias da='date "+%Y-%m-%d %A*** %T %Z"'
alias ebrc='nano ~/.bashrc'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mkdir='mkdir -p'
alias reload="source ~/.bashrc" # reload bashrc after it is modified
alias packup='/bin/tar -czvf' # Compress a file in tar format
alias unpack='/bin/tar -xzvpf' # Uncompress a a Tar file
alias contents='/bin/tar -tzf' # Can View the contents of a Tar file
alias mktd='tdir=`mktemp -d` && cd $tdir' # make a temp dir, then immediately cd into it
# Alias to multiple ls commands
#######################################################
alias la='ls -Al'************** # show hidden files
alias ls='ls -aF --color=always' # add colors and file type extensions
alias lx='ls -lXB'************* # sort by extension
alias lk='ls -lSr'************* # sort by size
alias lr='ls -lR'************** # recursive ls
alias lt='ls -ltr'************* # sort by date
alias lm='ls -al |more'******** # pipe through 'more'
alias directory='ls --color=auto --format=long' # enable color support of ls and also add handy aliases
# Alias chmod commands
#######################################################
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'
# Alias Shortcuts to graphical programs.
#######################################################
alias kwrite='kwrite 2>/dev/null &'
alias firefox='firefox 2>/dev/null &'
alias kate='kate 2>/dev/null &'
alias suk='kdesu konqueror 2>/dev/null &'
# Alias for xterm and aterm
#######################################################
alias term='xterm -bg AntiqueWhite -fg Black &'
alias termb='xterm -bg AntiqueWhite -fg NavyBlue &'
alias termg='xterm -bg AntiqueWhite -fg OliveDrab &'
alias termr='xterm -bg AntiqueWhite -fg DarkRed &'
alias aterm='xterm -ls -fg gray -bg black'
alias xtop='xterm -fn 6x13 -bg LightSlateGray -fg black -e top &'
alias xsu='xterm -fn 7x14 -bg DarkOrange4 -fg white -e su &'
# Alias for lynx web browser and internet
#######################################################
alias bbc='lynx -term=vt100 http://news.bbc.co.uk/text_only.stm'
alias nytimes='lynx -term=vt100 http://nytimes.com'
alias myip="curl www.whatismyip.org"
alias updates='sudo zypper list-updates' #checks system for updates
# SPECIAL FUNCTIONS
#######################################################
netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'
/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}
spin ()
{
echo -ne "${RED}\b#"
echo -ne "${WHITE}\b%"
echo -ne "${BLUE}\b%"
sleep 1.60
echo -ne "${RED}\b#${NC}"
}
scpsend ()
{
scp -P PORTNUMBERHERE "$@" USERNAME@YOURWEBSITE.com:/var/www/html/pathtodirectoryonremoteserver/;
}
# Weather by us zip code - Can be called two ways # weather 50315 # weather "Des Moines"
weather ()
{
declare -a WEATHERARRAY
WEATHERARRAY=( `lynx -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+${1}&btnG=Search" | grep -A 5 -m 1 "Weather for"`)
echo ${WEATHERARRAY[@]}
}
# Stock prices - can be called two ways. # stock novl (this shows stock pricing) #stock "novell" (this way shows stock symbol for novell)
stock ()
{
stockname=`lynx -dump http://finance.yahoo.com/q?s=${1} | grep -i ":${1})" | sed -e 's/Delayed.*$//'`
stockadvise="${stockname} - delayed quote."
declare -a STOCKINFO
STOCKINFO=(` lynx -dump http://finance.yahoo.com/q?s=${1} | egrep -i "Last Trade:|Change:|52wk Range:"`)
stockdata=`echo ${STOCKINFO[@]}`
if [[ ${#stockname} != 0 ]] ;then
echo "${stockadvise}"
echo "${stockdata}"
else
stockname2=${1}
lookupsymbol=`lynx -dump -nolist http://finance.yahoo.com/lookup?s="${1}" | grep -A 1 -m 1 "Portfolio" | grep -v "Portfolio" | sed 's/\(.*\)Add/\1 /'`
if [[ ${#lookupsymbol} != 0 ]] ;then
echo "${lookupsymbol}"
else
echo "Sorry $USER, I can not find ${1}."
fi
fi
}
#Translate a Word - USAGE: translate house spanish # See dictionary.com for available languages (there are many).
translate ()
{
TRANSLATED=`lynx -dump "http://dictionary.reference.com/browse/${1}" | grep -i -m 1 -w "${2}:" | sed 's/^[ \t]*//;s/[ \t]*$//'`
if [[ ${#TRANSLATED} != 0 ]] ;then
echo "\"${1}\" in ${TRANSLATED}"
else
echo "Sorry, I can not translate \"${1}\" to ${2}"
fi
}
# Define a word - USAGE: define dog
define ()
{
lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 3 -w "*" | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
if [[ -s /tmp/templookup.txt ]] ;then
until ! read response
do
echo "${response}"
done < /tmp/templookup.txt
else
echo "Sorry $USER, I can't find the term \"${1} \""
fi
rm -f /tmp/templookup.txt
}
# lazy man extract - example: ex tarball.tar
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjfv $1 ;;
*.tar.gz) tar xzfv $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xfv $1 ;;
*.tbz2) tar xjfv $1 ;;
*.tgz) tar xzfv $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
}
# clock - a little clock that appeares in the terminal window.
# Usage: clock.
clock ()
{
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done
}
# WELCOME SCREEN
#######################################################
clear
for i in `seq 40 40` ; do spin; done ;echo -ne "${WHITE}JADA ${NC}"; for i in `seq 40 40` ; do spin; done ;echo "";
echo -e ${LIGHTBLUE}`cat /etc/SuSE-release` ;
echo -e "Kernel Information: " `uname -smr`;
echo -e ${LIGHTBLUE}`bash --version`;echo ""
echo -ne "Hello $USER today is "; date
echo -e "${WHITE}"; cal ; echo "";
echo -ne "${CYAN}";netinfo;
mountedinfo ; echo ""
echo -ne "${LIGHTBLUE}Uptime is ";uptime | awk /'up/ {print $3,$4}'
for i in `seq 40 40` ; do spin; done ;echo -ne "${WHITE}http://www.yoursite.com/${NC}"; for i in `seq 40 40` ; do spin; done ;echo "";
echo ""; echo ""
###########################################################
Code Ends------------------>
 
Old 04-26-2009, 02:27 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
I don't see how you could "streamline" anything like this. A bashrc isn't a single integrated script; it's a collection of (mostly one-line) stand-alone commands, and there's not much you can do to optimize something like that.

The only real thing you could do is cut out things you don't need, which pretty much defeats the purpose. There may also be a few minor tweaks you could make to some of the shell functions to make them more efficient, but that would mostly affect the running of the functions themselves, rather than the bashrc.

In short, you already have a very clean and organized bashrc here (with some interesting entries I'm going to have to look at ). I wouldn't call it too long either. Mine's pretty big too, and I'll bet there are people here who could put yours to shame.
 
Old 04-26-2009, 05:34 AM   #3
Nick0.jd
LQ Newbie
 
Registered: Apr 2009
Location: Australia
Distribution: Debian (Desktop), Xubuntu (eee701)
Posts: 11

Rep: Reputation: 1
Something I have found nice is to separated it into a few smaller files.. like place all my alias into a separate file and then all my lil tweaks in another... then call them inside the .bashrc like so:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

I think I got this from my original Ubuntu install and have jsut kept my bashrc as i move around. Hope that helps.

Cheers
 
Old 04-26-2009, 12:08 PM   #4
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
Thanks David the H. and Nick0.jd

My understanding is that if my .bashrc file is to long it would slow down the terminal from opening, because it had to read the file in question.

I just ran into a problem. when I call stock novell from my terminal window I get an error

See below:

[10:01:04 jamtech]$ stock novell
Sorry jamtech, I can not find novell.

No changes were made to the file. It just stopped working I went to Yahoo.com and I was able to make the search request and it worked fine. This is very strange.

Code for stock function within the .bashrc file.

Code begin------>
# Stock prices - can be called two ways. # stock novl (this shows stock pricing) #stock "novell" (this way shows stock symbol for novell)
stock ()
{
stockname=`lynx -dump http://finance.yahoo.com/q?s=${1} | grep -i ":${1})" | sed -e 's/Delayed.*$//'`
stockadvise="${stockname} - delayed quote."
declare -a STOCKINFO
STOCKINFO=(` lynx -dump http://finance.yahoo.com/q?s=${1} | egrep -i "Last Trade:|Change:|52wk Range:"`)
stockdata=`echo ${STOCKINFO[@]}`
if [[ ${#stockname} != 0 ]] ;then
echo "${stockadvise}"
echo "${stockdata}"
else
stockname2=${1}
lookupsymbol=`lynx -dump -nolist http://finance.yahoo.com/lookup?s="${1}" | grep -A 1 -m 1 "Portfolio" | grep -v "Portfolio" | sed 's/\(.*\)Add/\1 /'`
if [[ ${#lookupsymbol} != 0 ]] ;then
echo "${lookupsymbol}"
else
echo "Sorry $USER, I can not find ${1}."
fi
fi
}

Code End------>

Could you please advise.
Thanks once again
 
Old 04-26-2009, 02:22 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could move some items into your ~/.profile script instead. It is sourced when you log in rather than every time you start another shell.
 
Old 04-29-2009, 01:39 AM   #6
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
jschiwal thanks for the information I will try that. I also like what Nick0.jd said about having all my aliases in in file (.bash_aliases).

Thanks.

I'm still working on my stock function I will keep yu posted.
 
Old 04-29-2009, 02:57 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Also, think about this. If you want all these functions to be available to you, they'll have to be sourced in sometime. In the end, it's probably going to take about the same amount of time whether you have it all in one file or in multiple ones. The best you can do is try to arrange it so that the loading doesn't affect your use of the machine.

I suggest you read up on how and when the various login files are read. The INVOCATION section of the bash man page will explain most of them. Note that some of the filenames may vary between distros.
 
Old 04-29-2009, 03:04 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Oh yeah, I almost forgot. Not to toot my own horn here, but you asked for other "cool stuff" to include. Well, a few months ago I wrote a script for a variable-length PS1 prompt that you might just be interested in.
 
Old 04-29-2009, 03:33 AM   #9
Nick0.jd
LQ Newbie
 
Registered: Apr 2009
Location: Australia
Distribution: Debian (Desktop), Xubuntu (eee701)
Posts: 11

Rep: Reputation: 1
Wow, I like that one David the H. using an eeePC I'll use anything that will help save screen real estate

Many thanks
 
Old 04-30-2009, 04:04 PM   #10
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
David the H.

The script you created is sweet. thanks
 
  


Reply

Tags
aliases, bashrc, profile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] command to pen optimize hpgl file gary_in_springhill Programming 3 03-13-2008 07:15 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
cant stream audio from line in allohakdan Linux - Software 2 10-25-2006 09:18 PM
Pop3 Command stream end of file while reading line sesimonsen Linux - Networking 0 05-13-2005 12:29 PM


All times are GMT -5. The time now is 04:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration