Script Unix Tutorial
Unix is an operating system designed around using the command line and entering text commands to complete tasks. If you have a series of commands you need to use over and over again, you can automate the process by putting them together into a script. Unix scripting lets you write a series of commands into a text file and run them all in sequence by executing the file. Scripts save time and hassle by automating processes, increasing your productivity.
Instructions
-
-
1
Create a blank file. An easy way to create a new empty file is the "touch" command. For example, typing "touch myscript.sh" at the command prompt will create a file called "myscript.sh." The ".sh" is a file extension commonly used to indicate the file is a shell script.
-
2
Open the file with any text editor. If you only have access to the command line, the editor "vi" or "vim" is commonly available on Unix-based operating systems. Enter "#!/bin/sh" on the first line. This specifies what shell to use. Below this line will be the series of commands that actually do things in your script.
-
-
3
Write a command on each line for your script. For example, if you want your script just to display some text, type "echo 'hello world'" below the header you added in Step 2.
-
4
Execute your script using the command "./myscript.sh" in the directory where you saved your script. If you are using the short example script, the text "hello world" should appear.
-
5
Add more advanced capabilities to your script. Any Unix command can be used as part of the script. There are also functions found in most programming languages, such as looping, variables and other methods for controlling the flow of your script.
-
1
References
- Photo Credit laptop image by jedphoto from Fotolia.com