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 e
Trang 1Lab 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/
To see all the files, use the following command:
Trang 2tar –tvf mywpdocs.tar (the –t will list all the files)
To extract all the files, use the following command:
tar –xvf mywpdocs.tar (the –x extracts the contents)
The following is a list of flags that are used with the tar command:
-c Create a new archive
-t List the contents of an archive
-x Extract the contents
-f Write the archive to file
-M Span multiple floppies if the archive is too big for one floppy
-v List the files as they are being processed
-u Add files to the archive
-z Compress or decompress automatically
gzip and gunzip
It is very common for files to be compressed when a tar archive is created
• gzip mywpdocs.tar will create a compressed file called mywpdocs.tar.gz and the original file will be deleted
• gunzip mywpdocs.tar.gz will decompress the file
Step 1: Creating the Script
1 Login as a Root and make sure that to be in the home directory StudentA5 will be used as an example in this lab Ask the instructor for the correct login for the
computer At the command prompt, type:
mkdir mybkup
touch file1 file2 file3
This will create a small subdirectory in the /home directory called mybkup and it will contain three files Verify the creation of the three files with the ls command:
ls
Were file1, file2, file3 created in the mybkup directory? Y/N Return to the home directory:
cd
Trang 32 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
1 For a script to be executable, the file permissions need to be changed At the
command prompt, type:
chmod 700 backup
2 To check the permissions of the backup file, type:
ls –l backup
Write the results in the space below:
_
Is the file now executable? How can this be verified?
Trang 4Step 3: Executing the Script
1 At the command prompt, execute the script:
/home/studentA5/backup
2 To verify that a new file was created after the script was executed, type:
ls
Does a file called mybkup.tar now exist in the home directory? Y/N
Step 4: Delete and recover the mybkup directory
1 From the /home/studentA5 directory locate the original mybkup file, type:
ls –R mybkup
Is the directory mybkup there? Y/N
2 To delete the mybkup directory, type:
rm –fr mybkup
3 To verify that the mybkup directory has been deleted, type:
ls mybkup
Is the directory gone? Y/N
4 To recover the mkbkup directory with the tar command, type:
tar –xvf mybkup.tar
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