Tài liệu Writing a Script File in Linux pptx

4 442 1
Tài liệu Writing a Script File in Linux pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

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/ To see all the files, use the following command: 2 - 4 IT Essentials II v2.0 - Lab 10.4.10 Copyright  2002, Cisco Systems, Inc. tar –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 cd 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 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 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? 4 - 4 IT Essentials II v2.0 - Lab 10.4.10 Copyright  2002, Cisco Systems, Inc. _______________________________________________________________ Step 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 ________________________ . complete this lab: • A lab computer with Linux installed and running. Scenario The members of the Engineering Department are working on some important documents. 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.

Ngày đăng: 18/01/2014, 05:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan