8
Writing Scripts: The Writing Scripts: The Basics Basics David Douglas & Peggy L. Lane David Douglas & Peggy L. Lane

Writing Scripts: The Basics

Embed Size (px)

DESCRIPTION

Writing Scripts: The Basics. David Douglas & Peggy L. Lane. Shell Script. A program written to automate a process in the Linux operating system. Written in an editor. What can a script do?. Make decisions based upon conditions. Perform arithmetic operations. - PowerPoint PPT Presentation

Citation preview

Writing Scripts: The BasicsWriting Scripts: The Basics

David Douglas & Peggy L. LaneDavid Douglas & Peggy L. Lane

Shell ScriptShell Script

A program written to automate a process A program written to automate a process in the Linux operating system.in the Linux operating system.

Written in an editor.Written in an editor.

What can a script do?What can a script do?

Make decisions based upon conditions.Make decisions based upon conditions.

Perform arithmetic operations.Perform arithmetic operations.

Create a menu using looping statements.Create a menu using looping statements.

Use functions to perform very specific Use functions to perform very specific tasks.tasks.

Manipulate data using an array (like a Manipulate data using an array (like a table).table).

How do I create a script?How do I create a script?

Ensure you Ensure you are in your are in your home home directory.directory.Type jpico and Type jpico and press enter. press enter. This will place This will place you in edit you in edit mode and it mode and it will look like will look like this:this:

Creating and Running a scriptCreating and Running a script

Enter the script (more on this later).Enter the script (more on this later).

Press Ctrl-X (^X) to Exit the editor and Press Ctrl-X (^X) to Exit the editor and Save the script.Save the script.

When prompted, enter the name of the When prompted, enter the name of the script and press enter.script and press enter.

To run the script, type To run the script, type sh filenamesh filename and and press the enter key.press the enter key.– (filename is the name of the script.)(filename is the name of the script.)

Creating a Simple ScriptCreating a Simple Script

Assume you want to create a script that you will Assume you want to create a script that you will run to display the following information after run to display the following information after logging in:logging in:– Current date and timeCurrent date and time– Pathname of the current working directoryPathname of the current working directory– Who else is currently on the systemWho else is currently on the system

To begin, type To begin, type jpico startjpico start and press enter and press enter

To include comments, place # in front of the text. To include comments, place # in front of the text.

What commands will provide the 3 pieces of What commands will provide the 3 pieces of information listed above?information listed above?

Type the following…….Type the following…….

My First Script!!My First Script!!

#This is my startup batch file that performs #This is my startup batch file that performs

#three functions. It provides me with the date #three functions. It provides me with the date

#and time, the current working directory, #and time, the current working directory,

#and the login names of anyone else who is on #and the login names of anyone else who is on

#the system. The good news is that the #the system. The good news is that the

#backspace key works in the editor!!#backspace key works in the editor!!

datedate

pwdpwd

whowho

Next StepsNext Steps

To exit the editor, press Ctrl-XTo exit the editor, press Ctrl-X

You will see a prompt You will see a prompt – If you typed jpico without a filename, you will If you typed jpico without a filename, you will

be prompted for a filename. Enter be prompted for a filename. Enter startstart. . – If you provided the filename of start, you will If you provided the filename of start, you will

see: Name of file to save (^C to abort): startsee: Name of file to save (^C to abort): start

Press the enter key.Press the enter key.

Type Type sh startsh start and press the enter key to and press the enter key to run the script.run the script.