YouSigma- the web’s most extensive resource for information
$6.99 Domain Names at Network Solutions®!
Shell Scripts

Shell scripts are nothing but a set of commands held in a flat file, and the file can be run as a program by invoking it. A script file allows you to put the commands in a file, called a shell script, and then execute the file. A shell script is much like a batch file: It is a list of UNIX commands typed in a file, and then the file is executed. More sophisticated scripts contain programming constructs for making decision's, looping, file testing, and so forth.

 

Sample Shell Scripts

 

1. User Defined Menu

#!/bin/ksh

# This Product is Designed and Developed by Deepak Chebbi

# The purpose of the script is to illustrate the use of case statement

# The script pops us a menu, with two options 1 and Q

# If the user chooses any other option He/She gets and error message

# The UI allows users to choose from the two presented options

# The script can be tailored to meet your needs – all additional options if required

# You can use /n or /t combinations with the echo command to provide

# Better Indentation

 

while ( true )

do

echo "\n\n\t\tMy First Program"

echo "\n\t\t1 ->Welcome"

echo "\n\t\tq -> QUIT"

echo "\n\n\t\tTo select any of the options, enter the number or Q to Quit"

echo "\n\n\t\tEnter your choice :\c "

read option

 

case $option in

 

1) echo "Welcome to www.YouSigma.com"

;;

'q'|'Q') exit 0

;;

 

*) echo "Invalid option."

echo "Hit return for the menu ...\c"

line

;;

esac

done


 

2. Checking Existence of a file in a Directory

#!/bin/ksh

# This Product is Designed and Developed by Deepak Chebbi

# The purpose of the script is to illustrate the use of "if" statement

# The script checks in the directory assigned to the variable LOCALDIR

# If it finds the file ".profile", the appropriate message is displayed

# The script can be tailored to meet your needs

# Replace the text and/or variable defined in < > to meet your requirements

 

LOCALDIR=</home/Deepak>

 

if [ -f ${LOCALDIR}/.profile ]; then

echo ".profile exists"

exit 0

else

echo ".profile Does Not exists"

exit 0

fi

3. Print File Name

#!/bin/ksh

# This Product is Designed and Developed by Deepak Chebbi

# The purpose of the script is to illustrate the use of "for" statement

# The script checks in the directory assigned to the variable LOCALDIR

# for every file it finds in the directory, it displays the file name

# The script can be tailored to meet your needs

# Replace the text and/or variable defined in < > to meet your requirements

 

LOCALDIR=</home/Deepak>

for file in `ls -lrt ${LOCALDIR} | awk {'print $9'}`

do

echo ${file}

done

4. FTP a File to a Remote Server

#!/bin/ksh

# This Product is Designed and Developed by Deepak Chebbi

# The purpose of the script is to illustrate the use of FTP

# This script will help you to ftp a file “a.out” to a server 100.0.000.00

# The script can be tailored to meet your needs

# Replace the text and/or variable defined in < > to meet your requirements

 

ftp -nvd <100.0.000.00> <<-END

user <username> <password>

ascii

lcd <LOCAL_CURRENT_DIRECTORY>

cd <CURRENT_DIRECTORY>

put <a.out>

bye

END

 

 

5. Strip Special Characters from a File

#!/bin/ksh

#

# This Product is Designed and Developed by

#

# Deepak Chebbi

#

# The purpose of the script is to Strip Special Character ^M from a file called abc.txt

# The outputfile is written with a date stamp as a suffix

# The script can be tailored to meet your needs

#

# Replace the variables and/or text defined in < > to meet your requirements

 

export INPUT_DIRECTORY=</usr/Deepak>

 

export OUTPUT_DIRECTORY=</usr/Deepak>

 

 

if [ -f ${INPUT_DIRECTORY}/abc.txt ]; then

OUTPUT_FILE=`date '+%y%m%d'`.txt

sed 's/^M//g' ${INPUT_DIRECTORY}/abc.txt | tee ${OUTPUT_DIRECTORY}/${OUTPUT_FILE}

exit 0

fi

6. Cleanup old File

#!/bin/ksh

# This Product is Designed and Developed by

#

# Deepak Chebbi

#

# The purpose of the script is to Clean up old file Beyond a particular day

# In this case 10 days older

# The script can be tailored to meet your needs

# You can use /n or /t combinations with the echo command to provide

# Replace the variables and/or text defined in < > to meet your requirements

 

export DIRECTORY_NAME=<DIRECTORY_NAME>

 

find ${DIRECTORY_NAME} -name "*.trc" -ctime +10 -mtime +10 -exec rm -f {} \; 2>/dev/null

7. Backup Files

#!/bin/sh

#

# Purpose: Create backup files and store them in a backup directory

#

# Replace the variables and/or text defined in < > to meet your requirements

dir=</home/deepak/backupscripts>

 

for file in memo[1-5]

do

if [ -f $file ]

then

cp $file $dir/$file.bak

echo "$file is backed up in $dir"

fi

done

8. Welcome Script

#!/bin/sh

# Echo a Greeting when user logs on

#

echo "Hello $LOGNAME, it's nice talking to you."

echo "Your present working directory is 'pwd'."

echo "You are working on a machine called 'uname -n'."

echo "Here is a list of your files."

ls # list files in the present working directory

echo "Bye for now $LOGNAME. The time is 'date +%T'!"

underline
About YouSigma Please Donate Using PayPal, to help us Develop Content
Copyright and Disclaimer Iridium rentals
Search Thousands of Interesting Articles on YouSigma! Just Enter You Search Word!
underline
Loading
underline
underline
Try a free sample Destiny Reading! Executive Openings! $80,000 to $500,000+
underline
= "Shell Scripts";