• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Monday, August 10, 2009

Posted by venu k
4 comments | 8:57 AM
#************************************************************************************
#Locking a terminal using a shell script.
#This script locks your terminal until you enter correct password (open2world)
#It also traps signals and interrupts
#You can't terminate the script by Ctrl+c or Ctrl+\ or Ctrl+z and Ctrl+d
#************************************************************************************

clear
trap "" 1 2 3 20
lines=`tput lines`
b=`expr $lines / 2 - 4 `

## center function

center()
{
columns=`tput cols`
until [ -z "$1" ]
do
for ((i=1;i<$(((columns-${#1})/2));i++)) do echo -n " " done echo -e "\033[1m \033[5m \033[42m $1 \033[0m " shift done } ## End of center function while true do clear tput cup $b 0
center " TERMINAL LOCKED "
center "Press any key to unlock"
read key
echo -n "enter password : "
read -s password
while true
do
if [ $password = "open2world" ]
then
clear
break 2
# breaks second outer loop
else
echo -e "\n You are an illegal user "
echo -e "\n Enter any key"
read
break
fi
done
done

Analysis :


The trap command allows you to execute a command when a signal is received by your script. It works like this:

trap arg signals

"signals" is a list of signals to interrupt and "arg" is a command to execute when one of the signals is received. If "arg" is not supplied script doesn't do any thing after receiving signal.

Note: Two signals "SIGKILL" "SIGSTOP" are unable to trap

Ex: trap "echo signal received" 1 2 3 15
(or use SIGHUP SIGINT SIGQUIT SIGTERM instead of 1 2 3 15 )

What is signal:


A signal is a message which can be sent to a running process. Some times called
software interrupts.

Some of signals and its value:

       Signal             Value     Action   Comment
-------------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT (Ctrl+c) 2 Term Interrupt from keyboard
SIGQUIT (Ctrl+\) 3 Core Quit from keyboard
SIGTERM 15 Term Termination signal
SIGTSTP (Ctrl+z) 20 Stop Stop typed at tty


Note: There are different standards(POSIX,SUSv) to determine signals and its values.
I tested Above signals in Fedora 9, Other systems may support them or not depending on system standard(I think almost all standards supports above signals and values).

More stuff:


I used "tput cols" and "tput lines" commands to collect number of columns and lines.
You can also directly use bash variables $LINES ,$COLUMNS to get columns and lines.But
you should to run your script in current shell only. If you run your script in sub shell
current shell will not export $LINES and $COLUMNS variable values.

4 comments:

  1. @anonymous: It seems just copy/paste formatting issues

    # Insert breaks after
    ...
    ## End of center function
    while true
    do
    clear tput cup $b 0
    ...

    ReplyDelete
  2. Most of the times we feel that searching for a locksmith online is probably a smart decision. However, we could become a victim of fake or scamming locksmiths who are imitating legitimate ones on the online directories. The case is these fake locksmiths impersonate the local locksmiths and they don't even have any local workshop. Sometimes these so-called locksmiths are not even properly trained and are operated through out-of-state call centers. www.flatratelocknkey-denver.com/

    A few cautious steps taken by you can help in avoiding a scamming company or an untrained impersonator.

    ReplyDelete