1 - 4 IT Essentials II v2.0 - Lab 10.4.10 Copyright 2002, Cisco Systems, Inc.
Lab 10.4.10: Writing a Script File in Linux
Estimated Time: 25 minutes
Objective
Upon completion of this lab, the student will be able to create a script file and run it in the
Linux environment.
Equipment
The following equipment is needed in order to complete this lab:
• A lab computer with Linux installed and running.
Scenario
The members of the Engineering Department are working on some important documents
that need to be backed up frequently. This involves a repetitive process that requires
them to type a long list of commands every time they need to perform a backup. Instead
of typing all these different commands individually each time, a script file can be written to
execute all of them with one command.
Procedures
Basic knowledge of the command line and the vi editor will be utilized in this lab. In the
first step of this lab the student will log on to the system with the root account and create
the script. In the second step of the lab, the student will assign permissions on the script
so that only the specified users can execute it. Then in the third step of this lab, the
student will log in with the studentXX account and execute the script.
Background on the Linux .tar and .gz Extensions
When a .tar (tape archive) file extension is seen, someone has bundled two or more files
together (usually for backup purposes). When a .gz extension is seen, the file has been
compressed (similar to the .zip extension in DOS).
For example, to archive a folder of WordPerfect files in a wp directory, use the following
command:
tar –cvf mywpdocs.tar wp/
Were file1, file2, file3 created in the mybkup directory? Y/N
__________________
Return to the home directory:
cd
3 - 4 IT Essentials II v2.0 - Lab 10.4.10 Copyright 2002, Cisco Systems, Inc.
2. Create a vi script that will automate the backup process. From the command line,
type:
vi/home/studentA5/backup
This will launch the vi text editor and a file called “backup” will be created and saved
in the home directory.
3. After the vi Editor is open, type the letter “i” on the keyboard to enter the text insert
mode.
4. Type the following text into the text editor:
#!/bin/sh
#
ls -R mybkup
tar –cvf mybkup > mybkup.tar
ls –l
#
To exit and save the file, press the ESC key and “:” on the keyboard and type:
wq
5. To verify that the backup script exists, at the command prompt type:
ls
Does the file “backup” exist in this directory? Y/N
__________________
6. To verify that the contents of the backup script, at the command prompt type:
cat backup
Do the contents of the backup file match step 4 above? Y/N
__________________
Step 2: Assigning Permissions
5. To verify that the original directory has been restored with the backup, type:
ls –R mybkup
6. Is the directory there and are file1, file2 and file3 there? Y/N. In the space below list
the directories and files in the home directory:
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
Did the student get the directory and files back? Y/N
________________________