80 likes | 185 Views
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.
E N D
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. • Create a menu using looping statements. • Use functions to perform very specific tasks. • Manipulate data using an array (like a table).
How do I create a script? • Ensure you are in your home directory. • Type jpico and press enter. • This will place you in edit mode and it will look like this:
Creating and Running a script • Enter the script (more on this later). • Press Ctrl-X (^X) to Exit the editor and Save the script. • When prompted, enter the name of the script and press enter. • To run the script, type sh filename and press the enter key. • (filename is the name of the script.)
Creating a Simple Script • Assume you want to create a script that you will run to display the following information after logging in: • Current date and time • Pathname of the current working directory • Who else is currently on the system • To begin, type jpico start and press enter • To include comments, place # in front of the text. • What commands will provide the 3 pieces of information listed above? • Type the following…….
My First Script!! • #This is my startup batch file that performs • #three functions. It provides me with the date • #and time, the current working directory, • #and the login names of anyone else who is on • #the system. The good news is that the • #backspace key works in the editor!! • date • pwd • who
Next Steps • To exit the editor, press Ctrl-X • You will see a prompt • If you typed jpico without a filename, you will be prompted for a filename. Enter start. • If you provided the filename of start, you will see: Name of file to save (^C to abort): start • Press the enter key. • Type sh start and press the enter key to run the script.