• No results found

Writing a Script File in Knoppix

Estimated Time: 30 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 required for this exercise:

A computer system with Knoppix 3.9 or greater 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 a user account and execute the script.

Background on the Linux .tar and .gz Extensions

The .tar (tape archive) file extension indicates that 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 from a wp directory, use the tar command:

tar –cvf mywpdocs.tar wp/

To see all the files, use the following command:

tar –tvf mywpdocs.tar (the –t will list all the files, a table of contents) To extract all the files, use the following command:

tar –xvf mywpdocs.tar (the –x extracts the contents)

The following is a list of switches 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 The gzip and gunzip commands:

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.

Preliminary step for Knoppix users

First, click the Knoppix Penguin icon, to get to the root shell.

Assign yourself a root password with the passwd command, and enter the password twice.

passwd

Changing password for user root.

Enter new UNIX password:

Retype new UNIX password:

The password will be successfully updated.

Now, create a user account, based on your name. For example, root could create an account for Maria Chavez like this:

useradd –m mchavez

As root, assign your new account a password in this way. For Maria’s case, she would type (while logged in as root):

passwd mchavez

Changing password for user mchavez.

Enter new UNIX password:

Retype new UNIX password:

Type it twice and it will be accepted.

In Knoppix, passwords are locked by default. That is why mchavez couldn’t create her own password immediately after her user account was created. Root has to do it.

Step 1: Creating the Script

1. Login as root and navigate to the home directory. mchavez will be used as an example in this lab. 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

Verify that you are in the user’s home directory. Type pwd

2. Create a vi script that will automate the backup process. From the command line, type:

vi backup

This will launch the vi text editor and a file called backup will be created and eventually 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.tar mybkup 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?

Step 3: Executing the Script

1. At the command prompt, execute the script by typing:

/home/mchavez/backup or ./backup

Note: Usually, an absolute path needs to be specified to run a script.

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/mchavez directory, locate the original mybkup directory. Type ls

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

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

______________________

Related documents