Creating A Unix Korn Shell Script
Korn Shell (.ksh) is a Unix scripting programing language that can be used to automate a variety of tasks
The first line of any Korn Shell Script should be:
A comment can be made by using "#":
It is considered to be best practice to leave comments in your script. Your comments should denote what particular functions or sections of your script are doing. In general, it is good to standardize the header of your script with
Purpose/Description of Script
*I personally like to write out any variables that I will be defining, or any variables that may be passed through the invokation of the script
######################## # # Script Name: Example Unix Korn Shell Script # Script Author: Daniel Robles # Date of Creation: 2013-07-09 # Description: This script illustrates basic Korn Schell Scripting # Variables: TBD # ########################
Time for some code! In this piece of code we will be sending an email to a user entered email address
#### #Tell the user what you would like them to do echo "Enter an Email Address: " #### #Tell the system to expect input from the user and assign the input to EMAIL_ADDRESS (referenced later by $EMAIL_ADDRESS) read EMAIL_ADDRESS #### #Create a variable called DATE (referenced later by $DATE) and assign it the value of of today DATE=$(date) #### #Create a variable called MESSAGE (referenced later by $MESSAGE) and assign it the string below with the referenced variables MESSAGE="Hello $email_address, The date is: $thedate" #### #Save the message in a file called MESSAGE.txt echo $MESSAGE > MESSAGE.txt #### #Invoke the Unix command "mail" to send an email # -s indicates a subject line will follow # the next parameter is the email address entered by the user # the next parameter tells the mail command to send the contents of MESSAGE.txt as the body of the email mail -s "Automated Email" $EMAIL_ADDRESS < MESSAGE.txt
What sort of tasks can be performed using a script like this?
Setup a cron job to run the script during specific time periods
Create notifications for completed jobs or system information
Automate repeated SQL jobs