PDA

Bekijk de volledige versie : [HowTo] bash completion to your needs



newbiefan
26-10-2009, 22:07
Many of us are using bash, because bash has usually more possibilities as the regular shell, but just a few have knowledge about the bash extensions.

Pre-condition is an install according Wengis howto.
ATTENTION: store your old /opt/etc/profile !

After installing bash with


ipkg install bash

and restore your old profile again to /opt/etc/profile, you can extend and adjust your bash to your needs.
For instance: An output to STDOUT with different colors is very easy. Even complete backups can be done with one command.
Of course, any kind of the usual alias is available as well as completions of existing commands.
Instancing completions of existing commands, you'll find as example the regular exit command.

Any kind of extensions or changes for your bash are stored in the file .bashrc in your home directory

Home directory for admin/root is /tmp/local/root



touch .bashrc


and add to the new .bashrc file



#bashrc file, collection of useful bash extensions
#Newbiefans bash, mostly overtaken from different sources
#feel free to copy and modify
#a good place to get more is http://bashscripts.org

# EXPORTS

#export PS1="[\[\033[1;34m\w\[\033[0m]\n[\t \u]$ "
#export HISTFILESIZE=3000 # the bash history should save 3000 commands
export HISTCONTROL=ignoredups #don't put duplicate lines in the history.
#export PATH

# 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
# Sample Command using color: echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"

# Source global definitions
if [ -f /etc/bashrc ]; then
. /opt/etc/bashrc
fi

# enable programmable completion features
if [ -f /etc/bash_completion ]; then
. /opt/etc/bash_completion
fi

# ALIAS
#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 backup='dd if=/dev/discs/disc0/part$1 of=/dev/discs/disc1/part$2' #Copy Partition: backup 2 1 (copy part2 to part1)
alias home='cd /tmp/local/root'
alias pg='ps aux | grep' #requires an argument
alias un='tar -zxvf'
alias mti='df -hT' #mounted_info
alias ping1='ping -c 1'
alias pingx='ping -c $1' #parameter needed, how often to ping syntax: pingx 3 192.168.x.x
alias openports='netstat -nape --inet'
alias ns='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
alias du='du -h --max-depth=1'
alias da='date "+%Y-%m-%d %A %T %Z"'
alias la='ls -Al' # show hidden files
alias lx='ls -lXB' # sort by extension
alias lk='ls -lSr' # sort by size
alias lc='ls -lcr' # sort by change time
alias lu='ls -lur' # sort by access time
alias lr='ls -lR' # recursive ls
alias lt='ls -ltr' # sort by date
alias lm='ls -al |more' # pipe through 'more'
alias ls='ls --color=auto -la'
alias ps='ps axf'
alias pss='ps axf | grep'
alias df='df -h'
alias mc='mc -c'
alias ipt='iptables -nv -L'
alias wi='whereis'
alias n='nano'
export TERMINFO=/opt/share/terminfo

# FUNCTIONS
backup ()
{
ok1=false
ok2=false
if [ -z $1 ] ; then
echo "No source partition determined"
else
ok1=true
fi
if [ -z $2 ] ; then
echo "No target partition determined"
else
ok2=true
fi
if [ $ok1 = true ] && [ $ok2 = true ] ; then
#check it before!!
#dd if=/dev/discs/disc0/part$1 of=/dev/discs/disc1/part$2
echo -e "${GREEN}backup from $1 to $2${NC}"
ok1=false ; ok2=false
else
echo "Syntax; 'backup 2 1' where 2 is partition 2 of the first disc resp. /opt on an asus router"
echo "and 1 is the target partition of the next disc!"
echo "Read at http://wl500g.info Thread 'ultimative bash'"
fi
}

netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
/sbin/ifconfig | awk /'Bcast/ {print $3}'
/sbin/ifconfig | awk /'inet addr/ {print $4}'

#/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}

spin ()
{
echo -ne "${RED}-"
echo -ne "${GREEN}\b|"
echo -ne "${BLUE}\bx"
sleep .02
echo -ne "${RED}\b+${NC}"
}

function _exit() #run upon exit of bash
{
echo -e "${RED}Leaving bash - See you soon honey, take care${NC}"
}
trap _exit EXIT

#start screen
clear
for i in `seq 1 15` ; do spin; done
echo -ne "${GREEN} Bash on Asus-Routers ${NC}"
for i in `seq 1 15` ; do spin; done ; echo
echo -e "Kernel Information: " `uname -a`
echo -e ${LIGHTBLUE}`bash --version`
echo -ne "Hello $USER today is "; date
echo -e "${GREEN}"; cal
echo -ne "${CYAN}"; netinfo
echo -ne "${LIGHTBLUE}Uptime for this router at"; uptime
for i in `seq 1 15` ; do spin; done
echo -ne "${GREEN} http://wl500g.info ${NC}"
for i in `seq 1 15` ; do spin; done
echo


Start your bash with: bash
Leave the bash with: exit
To control which shell is running: echo $0

Save everything to your flash with the usual:


flashfs save
flashfs commit
flashfs enable
reboot


Everybody can extend the bash to his needs.
I would appreciate it when somebody would post some useful extensions

Have a closer look to the file .bashrc in order to find the new commands.
If you have any problem, just ask.

Suggestions and completions, error reports and corrections are always greatly appreciated!

have fun
newbiefan

wpte
26-10-2009, 23:18
looks good newbie fan

this line pops up tho:

bash: cal: command not found
and the + signs are created rather slow:p
still it doesn't take away the fact that colors make your shell a better place to work in;)

newbiefan
27-10-2009, 06:47
looks good newbie fan

this line pops up tho:

bash: cal: command not found
and the + signs are created rather slow:p
still it doesn't take away the fact that colors make your shell a better place to work in;)

Bet, your system is not up to date: my cal works! :cool:

Hehe, regarding signs: make them faster!! You're the man!;):p

helmut