- /dev/null - Use to send unwanted output of program
Syntax: command > /dev/null
$ls > /dev/null - Normally all our variables are local. Local variable can be used in same shell, if you load another copy of shell (by typing the /bin/bash at the $ prompt) then new shell ignored all old shell's variable.
- Conditional execution
command1 && command2, command2 is executed if, and only if, command1 returns an exit status of zero.
command1 || command2, command2 is executed if and only if command1 returns a non-zero exit status.
Ex: $ rm myfile && echo "File is removed successfully" || echo "File is not removed" - By default in Linux every program has three files (stdin, stdout, stderr) associated with it, (when we start our program these three files are automatically opened by your shell).
- Redirect the output from a file descriptor directly to file with following syntax
Syntax: file_descriptor_number>filename
Ex: $rm bad_file 2> errlog - Redirect one standard io to another standard io.
Syntax: form>&destination /* use & get the address of destination */
Ex: 1>&2, directs the standard output (stdout) to standard error (stderr) device.
Ex:
#!/bin/sh
if [ $# -ne 2 ]
then
echo "Error : Number are not supplied" 1>&2 /* print error message on stderr and not on stdout */
echo "Usage : $0 number1 number2" 1>&2
exit 1
fi
ans=`expr $1 + $2`
echo "Sum is $ans" - Function is series of instruction/commands.
Syntax:
function_name ()
{
command1
command2
…
commandN
return
}
Ex: Type SayHello() at $prompt
$SayHello()
>{
>echo “Hello $LOGNAME, have a nice day”
>}
$SayHello /* execute SayHello() function in the command line */
Ex: Show a message when logout the shell, edit .bash_logout
$vi ~/.bash_logout
SayBuy()
{
echo “Buy $LOGNAME! Life never be the same, until you login again!”
echo “Press a key to logout…”
read
return
} - User interface
Ex:
# Script to create simple menus and take action according to that selected menu item
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Today’s date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ;read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0;;
*) echo "Oops!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esac
done - dialog utility /* apt-get install dialog */
Syntax:
dialog --title {title} --backtitle {backtitle} {Box options}
where Box options can be any one of following
--yesno {text} {height} {width}
--msgbox {text} {height} {width}
--infobox {text} {height} {width}
--inputbox {text} {height} {width} [{init}]
--textbox {file} {height} {width}
--menu {text} {height} {width} {menu} {height} {tag1} item1}...
Ex:
dialog \
--backtitle "Linux Shell Script" \
--title "Alert: Delete File" \
--yesno "\nDo you want to delete '~/hello' file" 7 60
sel=$? /* the answer is exit status */
case $sel in
0) echo "User select to delete file";; /* press yes button */
1) echo "User select not to delete file";; /* press no button */
255) echo "Canceled by user by pressing [ESC] key";; /* press Escape key, cancel dialog box */
esac
Ex:
dialog \
--backtitle "Linux Shell Script" \
--title "Inputbox - To take input from you" \
--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 pressed";;
255) echo "[ESCAPE] key pressed";;
esac
rm -f /tmp/input.$$
Ex:
dialog \
--backtitle "Linux Shell Script" \
--title "Main Menu" \
--menu "Move using [UP] [DOWN] [ENTER] to Select" 15 50 3 \ /* 15/50: height/width of box, 3: height of menu */
Date/time "Shows Date and Time" \ /* tag item */
Calendar "To see calendar" \
Editor "To start vi editor" 2>/tmp/menuitem.$$ /* send selected tag to temporary file */
menuitem=`cat /tmp/menuitem.$$`
opt=$?
case $menuitem in
Date/time) date;;
Calendar) cal;;
Editor) vi;;
esac - trap command
Syntax: trap {commands} {signal number list}
Signal number
When occurs
0
Shell exit
1
Hangup
2
Interrupt (CTRL+C)
3
Quit
9
Kill (cannot be caught)
# Signal is trapped to delete temporary file
del_file()
{
echo "*** CTRL + C trap occurs (removing temporary file) ***"
rm -f /tmp/input0.$$
exit 1
}
Take_input1()
{
recno=0
clear
echo "Appointment Note keeper Application for Linux"
echo -n "Enter your database file name: "
read filename
if [ ! -f $filename ]; then
echo "Sorry, $filename does not exit, creating $filename database"
echo "Appointment Note keeper Application database file" > $filename
fi
echo "Data entry start data: `date`" > /tmp/input0.$$
# Set a infinite loop
while :
do
echo -n "Appointment Title:"
read na
echo -n "Time:"
read ti
echo -n "Any remark:"
read remark
echo -n "Is data okay (y/n)?"
read ans
if [ $ans = y -o $ans = Y ]; then
recno=`expr $recno + 1`
echo "$recno. $na $ti $remark" >> /tmp/input0.$$
fi
echo -n "Add next appointment (y/n)?"
read isnext
if [ $isnext = n -o $isnext = N ]; then
cat /tmp/input0.$$ >> $filename
rm -f /tmp/input0.$$
return # terminate loop
fi
done
}
# Set trap for CTRL+C interrupt i.e. Install our error handler, when occurs it first calls del_file() and then exit
trap del_file 2 /* trap CTRL+C signal and do the corresponding action */
# Call our user define function: Take_input1
Take_input1 - The shift command moves the current values stored in the positional parameters (command line args) to the left one position.
Syntax: shift {offset}
Ex:
#!/bin/sh
# convert a decimal number (-n) to another number with specified basesystem (-b)
# usage: convert –b base –n number
while [ "$1" ]
do
if [ "$1" = "-b" ]; then
ob="$2"
case $ob in
16) basesystem="Hex";;
8) basesystem="Oct";;
2) basesystem="bin";;
*) basesystem="Unknown";;
esac
shift 2
elif [ "$1" = "-n" ]; then
num="$2"
shift 2
else
echo "Program $0 does not recognize option $1"
exit 1
fi
done
output=`echo "obase=$ob; ibase=10; $num;" | bc`
echo "$num Decimal number = $output in $basesystem number system(base=$ob)" - getopts command
Syntax: getopts {optstring} {variable}
Ex:
#!/bin/sh
# Usage: ani -n -a -s -w -d
# help_ani() To print help
help_ani()
{
echo "Usage: $0 -n -a -s -w -d"
echo "Options: These are optional argument"
echo " -n name of animal"
echo " -a age of animal"
echo " -s sex of animal "
echo " -w weight of animal"
echo " -d demo values (if any of the above options are used their values are not taken)"
exit 1
}
#Start main procedure
#Set default value for variable
isdef=0
na=Snoopy
age="2 Months"
sex=Male
weight=3Kg
#if no argument
if [ $# -lt 1 ]; then
help_ani
fi
while getopts n:a:s:w:d opt
do
case "$opt" in
n) na="$OPTARG";;
a) age="$OPTARG";;
s) sex="$OPTARG";;
w) weight="$OPTARG";;
d) isdef=1;;
\?) help_ani;;
esac
done
if [ $isdef -eq 0 ]; then
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"
else
na="Pluto Dog"
age=3
sex=Male
weight=20kg
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
fi
全站熱搜
留言列表