dialog --title "Linux Dialog Utility Infobox" --backtitle "Linux Shell Script\ Tutorial" --infobox "This is dialog box called infobox, which is used \ to show some information on screen. Press any key. . . " 7 50 -------------------------- output dialog --title "Alert : Delete File" --backtitle "Linux Shell Script\ Tutorial" --yesno "\nDo you want to delete '/usr/letters/jobapplication'\ file" 7 60 sel=$? case $sel in 0) echo "User select to delete file";; 1) echo "User select not to delete file";; 255) echo "Canceled by user by pressing [ESC] key";; esac -------------------------- input dialog --title "Inputbox - To take input from you" --backtitle "Linux Shell\ Script Tutorial" --inputbox "Enter your name please" 8 60 2>/tmp/input.$$ sel=$? na=`cat /tmp/input.$$` case $sel in 0) echo "Hello $na" ;; 1) echo "Cancel is Press" ;; 255) echo "[ESCAPE] key pressed" ;; esac rm -f /tmp/input.$$ -------------------------- #How to create small menu using dialog # dialog --backtitle "Linux Shell Script Tutorial " --title "Main\ Menu" --menu "Move using [UP] [DOWN],[Enter] to\ Select" 15 50 3\ Date/time "Shows Date and Time"\ Calendar "To see calendar "\ Editor "To start vi editor " 2>/tmp/menuitem.$$ menuitem=`cat /tmp/menuitem.$$` opt=$? case $menuitem in Date/time) date;; Calendar) cal;; Editor) vi;; esac -------------------------- remove files (including names with spaces) find /home -type f -name "*.txt" -print0 | xargs -0 rm -f ------------------ testing for command-line parameters case "$1" in "") echo "Usage: ${0##*/} "; exit 65;; # No command-line parameters, # or first parameter empty. # Note that ${0##*/} is ${var##pattern} param substitution. Net result is $0. -*) FILENAME=./$1;; # If filename passed as argument ($1) starts with a dash, # replace it with ./$1 # so further commands don't interpret it as an option. * ) FILENAME=$1;; # Otherwise, $1. esac ------------------ # Using command substitution to generate a "case" variable. case $( arch ) in # "arch" returns machine architecture. i386 ) echo "80386-based machine";; i486 ) echo "80486-based machine";; i586 ) echo "Pentium-based machine";; i686 ) echo "Pentium2+-based machine";; * ) echo "Other type of machine";; esac exit 0 ------------------ match_string () { MATCH=0 NOMATCH=90 PARAMS=2 # Function requires 2 arguments. BAD_PARAMS=91 [ $# -eq $PARAMS ] || return $BAD_PARAMS case "$1" in "$2") return $MATCH;; * ) return $NOMATCH;; esac } a=one b=two c=three d=two match_string $a # wrong number of parameters echo $? # 91 match_string $a $b # no match echo $? # 90 match_string $b $d # match echo $? # 0 exit 0 ------------------ # isalpha.sh: Using a "case" structure to filter a string. SUCCESS=0 FAILURE=-1 isalpha () # Tests whether *first character* of input string is alphabetic. { if [ -z "$1" ] # No argument passed? then return $FAILURE fi case "$1" in [a-zA-Z]*) return $SUCCESS;; # Begins with a letter? * ) return $FAILURE;; esac } # Compare this with "isalpha ()" function in C. isalpha2 () # Tests whether *entire string* is alphabetic. { [ $# -eq 1 ] || return $FAILURE case $1 in *[!a-zA-Z]*|"") return $FAILURE;; *) return $SUCCESS;; esac } isdigit () # Tests whether *entire string* is numerical. { # In other words, tests for integer variable. [ $# -eq 1 ] || return $FAILURE case $1 in *[!0-9]*|"") return $FAILURE;; *) return $SUCCESS;; esac } check_var () # Front-end to isalpha (). { if isalpha "$@" then echo "\"$*\" begins with an alpha character." if isalpha2 "$@" then # No point in testing if first char is non-alpha. echo "\"$*\" contains only alpha characters." else echo "\"$*\" contains at least one non-alpha character." fi else echo "\"$*\" begins with a non-alpha character." # Also "non-alpha" if no argument passed. fi echo } digit_check () # Front-end to isdigit (). { if isdigit "$@" then echo "\"$*\" contains only digits [0 - 9]." else echo "\"$*\" has at least one non-digit character." fi echo } a=23skidoo b=H3llo c=-What? d=What? e=`echo $b` # Command substitution. f=AbcDef g=27234 h=27a34 i=27.34 check_var $a check_var $b check_var $c check_var $d check_var $e check_var $f check_var # No argument passed, so what happens? # digit_check $g digit_check $h digit_check $i exit 0 ================== CLI calculator function calc { echo "${1}"|bc -l; } ----- ? () { echo "$*" | bc -l; } ? 10+586/654 ================== Arrays unset v v=(one two three) echo -e "${v[@]}" one two three echo -e "${v[*]}" one two three echo -e "${#v[@]}" 3 echo -e "${v[0]}" one echo -e "${v[1]}" two ------------------ declare -a array array[0]="zero" array[1]="one" array[2]="two" array[3]="three" for i in `seq ${#array[@]}` do echo $array[$i-1] done ================== eliminate need for let declare -i i i=5; while (( $i > 1 )); do i=i-1 echo $i done ------------------ ================== spinner spinner() { case $SPIN in "|") SPIN="/" ;; "/") SPIN="-" ;; "-") SPIN="\\" ;; "\\") SPIN="|" ;; *) SPIN="|" ;; esac echo -en "\r$1: $SPIN" } ================================= #!/bin/bash var1=unset previous=$var1 while echo "previous-variable = $previous" echo previous=$var1 [ "$var1" != end ] # Keeps track of what $var1 was previously. # Four conditions on "while", but only last one controls loop. # The *last* exit status is the one that counts. do echo "Input variable #1 (end to exit) " read var1 echo "variable #1 = $var1" done # Try to figure out how this all works. # It's a wee bit tricky. exit 0 ================================= #!/bin/bash END_CONDITION=end until [ "$var1" = "$END_CONDITION" ] # Tests condition here, at top of loop. do echo "Input variable #1 " echo "($END_CONDITION to exit)" read var1 echo "variable #1 = $var1" echo done exit 0 ================================= ------------------ dd #!/bin/bash # self-copy.sh # This script copies itself. file_subscript=copy dd if=$0 of=$0.$file_subscript 2>/dev/null # Suppress messages from dd: ^^^^^^^^^^^ exit $? ------------------ dd sending a SIGUSR1 signal to a running 'dd' process makes it print to standard error the number of records read and written so far, then to resume copying dd if=/dev/zero of=/dev/null& pid=$! $ kill -USR1 $pid; sleep 1; kill $pid ------------------ socket communication bash can perform basic socket communication via /dev/tcp, /dev/udp. These are pseudo devices that bash uses to communicate with network sockets. In fact if you did ls -l /dev/tcp /dev/udp you will get a 'No such file or directory' error message. quickly check headers from a HTTP Server. Here is a simple function you can put in your $HOME/.bashrc that will check for headers of HTTP server. headers() { server=$1; port=${2:-80} exec 5<> /dev/tcp/$server/$port echo -e "HEAD / HTTP/1.0\nHost: ${server}\n\n" >&5 cat <5 exec 5<&- } headers [] The port number defaults to 80 if not provided. check if a port is open or not. testPort() { server=$1; port=$2; proto=${3:-tcp} exec 5<>/dev/$proto/$server/$port (( $? == 0 )) && exec 5<&- } testPort [] The protocol can be either tcp or udp and defaults to tcp. ================================= while read LINE; do LINE=${LINE%%#*} [ -z "$LINE" ] && continue : do what you want here done < config-file