Posts

Showing posts with the label bash

[ Level 2 ] Get last argument in bash.

You could use the following methods to get the last argument that you pass to script. #!/bin/bash getLastArg1() { for last do true done echo $last } getLastArg2() { echo $@ | awk '{print($NF)}' } getLastArg3() { eval echo `echo \\$${#@}` } getLastArg1 $@ getLastArg2 $@ getLastArg3 $@ $ ./test.sh a b c c c c $ Wish this helps. regards, Stanley Huang

[ Level 3 ] Bash script for color ansi.

#!/bin/bash ## Get ansi color code ## Usage: ## getAnsiColorCode (f)orward_ground/(b)ack_ground color_name ## ex. ## getAnsiColorCode f/b black/red/green/yellow/blue/magenta/cyan(purple)/white/default getAnsiColorCode() { sReturnCode="" fb='f' color='default' if [ $# -eq 1 ] then color="$1" elif [ $# -gt 1 ] then fb="$1" color="$2" fi if [ "$fb" == "b" ] then sReturnCode='4' else sReturnCode='3' fi case $color in black) sReturnCode="${sReturnCode}0" ;; red) sReturnCode="${sReturnCode}1" ;; green) sReturnCode="${sReturnCode}2" ;; yellow) sReturnCode="${sReturnCode}3" ;; blue) sReturnCode="${sReturnCode}4" ;; magenta) sReturnCode="${sReturnCode}5" ;; cyan|purple) sReturnCode="${sReturnCode}6" ;; white) ...

[Level 2] Scan wifi channel in Ubuntu.

#!/bin/bash iwlist wlan0 scanning | grep Channel: | grep -v grep | cut -d: -f2 | sort Wish this helps. regards, Stanley Huang

[ Level 2 ] Color for man page.

I forget where to get this information, just try it. export PAGER="`which less` -s" export BROWSER="$PAGER" export LESS_TERMCAP_mb=$'\E[38;5;167m' export LESS_TERMCAP_md=$'\E[38;5;39m' export LESS_TERMCAP_me=$'\E[38;5;231m' export LESS_TERMCAP_se=$'\E[38;5;231m' export LESS_TERMCAP_so=$'\E[38;5;167m' export LESS_TERMCAP_ue=$'\E[38;5;231m' export LESS_TERMCAP_us=$'\E[38;5;167m' Wish this helps. regards, Stanley Huang

[ Level 3 ] Bash auto completion

Bash auto complete Wish this helps. regards, Stanley Huang

[Level 1] autocomplete in shell script

#!/bin/bash read -e -p 'input file name ( for auto complete): ' input_file echo $input_file $ ./autocomplete.sh input file name ( for auto complete): /etc/ho<tab> input file name ( for auto complete): /etc/host<enter> /etc/host $ Wish this helps. regards, Stanley Huang

[Level 2] Bash completion in Ubuntu

As we know, we can use "tab" for word completion. And now, you can also use "tab" for bash completion. That means when you finished typing your command, ex. # apt-get and you press "tab", then you will get the options for apt-get. # apt-get autoclean        clean            purge            upgrade autoremove       dist-upgrade     remove           build-dep        dselect-upgrade  source           check            install          update How could you enable this feature? add the following command in your ~/.bashrc, if not exists. if [ -f /etc/bas...

[Level 2] Math exam generator

This morning, my son ask me to give him a 50 question math exam. So I wrote a math exam generator to produce the exam. PS. You need OpenOffice to open result file. #!/bin/bash set -f declare -i nQ=${1:-50} declare    sF=$0.csv; [ -f $sF ] && rm $sF; declare -i i=0 declare    aOperator=(+ - * /); declare -i nOperator=4 declare    aOperator=(+ -); declare -i nOperator=2 getRandomNumber() {   declare -i n=$RANDOM*89/32767+10   echo $n } getRandomOperator() {   declare -i n=$RANDOM*$nOperator/32767   echo ${aOperator[$n]} } for i in `seq 1 $nQ` do   declare -i n1=`getRandomNumber`   declare -i n2=`getRandomNumber`   declare    sO=`getRandomOperator`   echo "$n1,$sO,$n2,=,,=if(isblank(e$i);0;if(e$i=(a$i${sO}c$i);1;0))" >> $sF done echo ",,,correct,=,=sum(f1:f$i),/$i" >> $sF echo ",,,score,=,=sum(f1:f$i)/$i*100" >> $sF soffice $sF Wish ...