Shell Script accept password using read commnad

in Categories Script last updated April 10, 2008
#!/bin/bash
# Script accept password using read commnad
# Not *very secure*, this script is for learning purpose only 
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
PASS="abc123"
read -s -p "Password: " mypassword
echo ""
[ "$mypassword" == "$PASS" ] && echo "Password accepted" || echo "Access denied"

Script to logoff KDE/Gnome desktop user session; you can use it from command line or from desktop via Lanucher

in Categories Desktop last updated April 9, 2008
#!/bin/bash
# Script to logoff KDE/Gnome user session. 
# With proper permission via sudo it is possible that rest of the users 
# can use the same script. You can create Launcher to use this script 
# from Linux desktop. Tested on Debian Linux 3.x, but it should work with
# other Linux distros and FreeBSD
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# Bins, adjust them as per your UNIX/Linux/BSD system (default is Debian)
 
PS="/bin/ps"
AWK="/usr/bin/awk"
GREP="/bin/grep"
XARGS="/usr/bin/xargs"
KILL="/bin/kill"
 
# Send HUP signal to X server :)
$PS aux | $GREP X | $GREP -v "grep" | $AWK '{ print $2}' | $XARGS $KILL -HUP $1

Shell Script to read price of an article and calculate discount

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Script to read price of an article. If the price is less than 100
# then display "No discount" else give a discount of 10%. Display
# the price of article after discount
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter price of an article : "
read price
 
if [ $price -lt 100 ]
then
   echo "No discount "
   d=0 # 0 means no discount
else
   echo "10% discount "	
   d=$(( $price  * 10 / 100 )) # 10% discount
fi
# how much user need to pay? after discount
pay=$(( $price - $d ))
echo "You need to pay INR. $pay"

Script to display sum of two number and to do calculations such as +, -, / etc

in Categories Shell Math last updated April 15, 2008
#!/bin/bash
# Script to display sum of two number and to do calculations such as +, -, / etc
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo "**** My calculator ****"
echo "M A I N - M E N U"
echo "1. Multiplication"
echo "2. Subtraction"
echo "3. Remainder"
echo "4. Divide"
echo -n "Please select your choice (1-4) : "
read choice
 
echo -n "Enter your first number : "
read n1
echo -n "Enter your second number : "
read n2
 
if [ $choice -eq 1 ]
then
	answer="$n1 x $n2 = $(( $n1 * $n2 ))"
elif [ $choice -eq 2 ]
then
	answer="$n1 - $n2 = $(( $n1 - $n2 ))"
elif [ $choice -eq 3 ]
then
	answer="$n1 % $n2 = $(( $n1 % $n2 ))"
elif [ $choice -eq 4 ]
then
	answer="$n1 / $n2 = $(( $n1 / $n2 ))"
else
	echo "Sorry please select number between 1-4 only"
	exit 1
fi
echo $answer

Script to Store given command and execute them

in Categories Academic last updated April 3, 2008

Write script to store the following command and execute them:
a) date
b) echo “Message”
c) cal

#!/bin/bash
# Script to Store given command and execute them
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
date
cal
echo "Hello Friends"