Archive for the Category » shell «

Saturday, October 18th, 2008 | Author: Angelo

How to keep ssh connection alive ?

While working remotely using ssh, you may have experienced ssh connection is closed (disconnected) while you are out for a tea or when you leave sshconnection inactive for few mins. It is very annoying that ssh connection getting disconnected again and again. Here is script which help you to keep ssh connection alive.

Executing following script to keep your ssh connection alive. This script will keep executing commands hostname and date every after 60 seconds so your ssh connection will never inactive more than 60 secs.

Edit the file keepalive.sh using vi editor and add following lines in the same.
[root@linuxserver ~]#vi keepalive.sh

#!/bin/sh

if [ $# -eq 1 ]; then
sleep_time=”$1″
else
sleep_time=60
fi

while true; do echo "`hostname` `date`"; sleep $sleep_time;done

now execute it as given below

[root@linuxserver ~]#sh keepalive.sh 60

When you will return to your work you can terminate this running script by pressing ctrl + c.

Thursday, October 16th, 2008 | Author: Angelo

Defining variable in shell scripting.

For processing data, it must be store in RAM memory. Generally RAM memory is divided into small locations, and each location had unique number called memory address, which is used to hold our data. scripting can give a unique name to this address of ram called memory variable or variable. You can call this as storage location which can have different value but only one at the time.

There are two types of variables:

[A} Variable defined by Linux are known as system variable. All theses variable are defined in capital letters.
[B] Variables defined by user are known as user defined variables. You can define them in capital letters as well as in small letter or can be define using capital or small letter. But it is recommended that small letters should be used while defining user defined variables.

Determine system defined variable (also know as environment variables) by command env it will show you all variable created by Linux with value assigned to the same.

[root@server ~]#env

Exercise: User defined variables.

When variable name is followed by =value, the value of the variable is set to value.

Let’s define variable angelo with value scripting

[root@server ~]#angelo=scripting
[root@server ~]#echo $angelo

scripting

Thursday, October 09th, 2008 | Author: Angelo

Bash shell is command language interpreter which read commands from the standard input or from a file and executes it. Shell scripting is useful tool when someone need to run multiple commands on shell repeatedly. In simple words shell scripting is defining all commands with or without conditions in a file and execute the same file as command.

Sample shell script with name bashscript is given to generate message “Welcome to shell scripting world.”

A. Edit a file using vi editor and add following lines in it.

[Linux server]#vi bashscript

!#/bin/bash
echo "Hi, This is my first shell script"

B. Make the script executable.

[Linux server]#chmod u+x bashscript
Or
[Linux server]#chmod 700 bashscript

C. Executing shell script.

[Linux server]#./bashscript

How to execute shell script as command ?
Just upload bashscript in any directory defined in the environment variable PATH. You can check directories defined in environment variable PATH using command.

[Linux server]#echo $PATH

Now on shell type script name on hash prompt and hit the enter. And you have execute shell script as command.
[Linux server]#bashscript

Script is executed as command.

If you have any question or need any shell script you can query the same as comment for this post. I will update your query or shell script in comment or will write post for the same asap.

Category: shell  | Tags: , , ,  | One Comment