Вы находитесь на странице: 1из 12

UNIX HANDS-ON

1. ACCESS PERMISSIONS
Change the permissions for a file, created in a directory

Perform the below activities at the unix terminal:

Create a directory named as PermissionsEx in the default logged in directory


(/home/scrapbook/tutorial/). Create a file named as sample in the directory and Provide the
below permissions on the file(/home/scrapbook/tutorial/sample) .

1.Read, write and execute to owner.

2.Read and execute permissions for Group and Others.

Instructions:

1.The file name and folder name, to be created should be same as mentioned in the problem
statement.

2.After completion of the above activities, click on SUMMARY or CONTINUE button to


proceed to next assignment.

ANSWERS
mkdir PermissionsEx

cd PermissionsEx

touch sample

chmod 755 sample

2. DIRECTORY AND FILE CREATION


Perform the below activities at the unix terminal:

Create a directory with the name as TATA_Ltd in the default logged in


directory(/home/scrapbook/tutorial ).
Create a file with the name as welcome.txt inside the directory(TATA_Ltd ). Add the below line
of text into the file(welcome.txt)

Welcome to TCS
Note: The file name and folder name, to be created should be same as mentioned in the above
problem statement
After performing the above activities ,click on the SUMMARY or CONTINUE button to know
the completion status

ANSWERS
mkdir TATA_Ltd

cd TATA_Ltd

vi welcome.txt

[press I , type Welcome to TCS, press esc, :wq, enter, press summary]

3. DIRECTORY AND FILE OPERATIONS


Directory and File operations -

Perform the below activities at the unix terminal:

Create two Directories named as Directory1 and Directory2 in the Home directory ( default
logged in directory )

Create three files named as file1.txt, file2.txt and file3.txt in the Directory1 and perform the
below operations .

1.Copy the file1.txt to Directory2 from Directory1

2.Rename the file2.txt with new name as newfile.txt in the Directory1

3.Move the file file3.txt to Directory2

Instructions:

1.Your default logged in directory: /home/scrapbook/tutorial/

2.The file names and folder names, to be created should be same as mentioned in the problem
statement

3.After performing the above operations, click on SUMMARY or CONTINUE button to know
the completion status

ANSWERS
mkdir Directory1
mkdir Directory2

cd Directory1

touch file1.txt

touch file2.txt

touch file3.txt

mv file2.txt newfile.txt

cp file1.txt ~/Directory2

mv file3.txt ~/Directory2

4. DIRECTORY AND FILE OPERATIONS - STEP BY STEP


EXECUTION
Step 1 of 5

Directory creation

Perform the below activities at the unix terminal:

Create two Directories named as Directory1 and Directory2 in the default logged-in directory (
/home/scrapbook/tutorial/ )

Instructions:

1.The folder name, to be created should be same as mentioned in the problem statement.

2.After completion of the above activities, click on CONTINUE or SUMMARY button to


proceed to next step.

ANSWERS

mkdir Directory1

mkdir Directory2

Step 2 of 5
Copy files from Directory1 to Directory2

In continuation previous step, perform the below activities:


Create three files named as file1.txt, file2.txt and file3.txt in the Directory1(created in step1)

Instructions:

1.The file names, to be created should be same as mentioned in the problem statement.

2.After completion of the above activities, click on CONTINUE or SUMMARY button to


proceed to next step.

ANSWERS
cd Directory1

touch file1.txt

touch file2.txt

touch file3.txt

Step 3 of 5
In continuation to previous steps, perform the below activities:

Copy all files from the Directory1 to Directory2

Instructions:

After completion of the above activities, click on CONTINUE or SUMMARY button to proceed
to next step.

ANSWERS
cp file1.txt ~/Directory2

cp file2.txt ~/Directory2

cp file3.txt ~/Directory2

Step 4 of 5
In continuation previous steps, perform the below activities:

Remove all the files from the Directory1

Instructions:
After completion of the above activities, click on CONTINUE or SUMMARY button to proceed
to next step.

ANSWERS
rm file1.txt

rm file2.txt

rm file3.txt

Step 5 of 5
In continuation to previous steps, perform the below activities:

Remove the Directory1

Instructions:

After completion of the above activities, click on SUMMARY or CONTINUE button to proceed
to next step.

ANSWERS

cd ..

rm -r Directory1

5. COUNT OF LINES AND WORDS


Display number of lines and words in a given input file

Write a command/logic, which will read the content from a given input file and display the
number of lines and number of words in the file.

Your default logged in directory: /home/scrapbook/tutorial/

Input file location: /home/scrapbook/tutorial/unix_countproject/

Input file Name: input.txt

Script file location: /home/scrapbook/tutorial/

Script file Name: script.sh


Instructions:

1.You can view content of input file(input.txt) in the


folder(/home/scrapbook/tutorial/unix_countproject/) .

Note that this folder and file already exists and are located in the path mentioned above.

2.You can open the script.sh file using vi editor, Write the logic inside the file to display the
number of lines and number of words in the input file(input.txt), After writing the logic , save the
file and quit from vi editor .

3.To test your command/logic, Run the shell script file(script.sh) at the terminal using the below
command

sh /home/scrapbook/tutorial/script.sh

4.If any issues while running the above command, Modify the script.sh file and repeat the
point#:3

5.Don't use any echo statement in the script.sh file, even in the commented line/code

6.On completion of the task ,click on SUMMARY or CONTINUE to proceed to next


assignment.

Example : Contents of sample Input file named as input.txt - starting from following line

Hello all
Welcome to all of you

Expected Output:
27

ANSWERS ($ indicates terminal pointer don’t type that)


$ cat>script.sh (enter)

wc –lw /home/scrapbook/tutorial/unix_countproject/input.txt (enter)

ctrl+d

$ sh script.sh (enter)
6. LINES WITH GIVEN PATTERN HANDS-ON
ANSWER

7. DISPLAY THE STUDENT RECORDS


Write a command or logic , which will read the contents from the given input file and fetch the
Students of School named as Sangamithra

Input file format:

The sequence of columns in the input file(input.txt ) is as below, columns separated by


"|"(excluding the double quotes).

StudentName|Class|SchoolName

Output Format:

The sequence of Column in the output is as below, columns separated by "|"(excluding the
double quotes) ,

SchoolName|Class|StudentName

Your default logged in directory: /home/scrapbook/tutorial/

Input file location: /home/scrapbook/tutorial/unix_studentproject/

Input file Name: input.txt

Script file location: /home/scrapbook/tutorial/

Script file Name: script.sh


Instructions:

1.You can view content of input file(input.txt) in the


folder(/home/scrapbook/tutorial/unix_studentproject/) .

Note that this folder and file already exists and are located in the path mentioned above.

2.You can open the script file (/home/scrapbook/tutorial/script.sh) file using vi editor, Write the
logic inside the file to Display the students from the school named as Sangamithra from the input
file (input.txt) in the desired order mentioned above . After writing the logic in the script file,
save the file and quit from vi editor .

For More clarity if required, please refer the example below,

3.To test your command/logic in script.sh, run the script file at the terminal using the below
command,

sh /home/scrapbook/tutorial/script.sh

4.If any issues while running the above command, Modify the script.sh file and repeat the
point#:3

5.Don't use any echo statement in the script.sh file, even in the commented line/code

6.After completion of the above task , Click on SUMMARY or CONTINUE button to proceed
for the next assignment.

Example :

Contents of sample input file(input.txt) - starting from following line,

Deepu|First|Meridian

Neethu|Second|Meridian

Sethu|First|DAV

Theekshana|Second|DAV

Teju|First|Sangamithra

Note:
The column values in each of the above records/row represents one student data in the order of
StudentName,Class and SchoolName respectively and each of these values are separated by "|"
(excluding double quotes )

Expected Output:

Sangamithra|First|Teju

Note: The column values in each of the above records/row represents one student data in the
order of SchoolName,Class and StudentName respectively and each of these values are separated
by "|" (excluding double quotes )

ANSWERS
8. AVERAGE OF MARKS
Average of marks

Write the logic / commands, which will read the contents from the given input file and display
the records of students, whose average of 'two subject marks' is more than 90.

Input file format:

The sequence of columns/contents in the input file(Input.txt) as below and the column values are
separated by "#"(excluding the double quotes)

StudentName # ClassName #SchoolName # Subject1Marks # Subject2Marks

Output format:

The sequence of column in the output is same as input columns sequence but there is an
additional column for average of marks as the last column.

The output column values needs to be separated by "|"(excluding double quotes).

Your default logged in directory: /home/scrapbook/tutorial/


Script file location: /home/scrapbook/tutorial/

Script file name: script.sh

Input file location: /home/scrapbook/tutorial/unix_subjectaverage/

Input file name: Input.txt

Instructions:

1.You can view content of input file(Input.txt) in the


folder(/home/scrapbook/tutorial/unix_subjectaverage/) .

Note that this folder and file already exists and are located in the path mentioned above.

2.You can open the script file (/home/scrapbook/tutorial/script.sh) file using vi editor, Write the
logic / commands to read the data from input file(Input.txt) , calculate the average of two subject
marks and display the student details, whose average of marks is more than 90. After writing the
logic save the file and quit from vi editor.

3.To test your command/logic in script.sh, run the script file at the terminal using the below
command,

sh /home/scrapbook/tutorial/script.sh

4.If any issues while running the above command, Modify the script.sh file and repeat the
point#:3

5.Don't use any echo statement in the script.sh file, even in the commented line/code

6.After completion of the above task, click on SUMMARY or CONTINUE button to proceed for
the next assignment.

Example:

Contents of Input file - starting from following line,

Deepu#First#Meridian#95#90

Neethu#Second#Meridian#80#75

In the above input content, The values in each row represents one student data in the order of
StudentName,Class,SchoolName,Subject1Marks and Subject2Marks respectively and separated
by "#" (excluding double quotes)
Expected Output:

Deepu|First|Meridian|95|90|92.5

In the above output, The values in each row represents one student data in the order of
StudentName,Class,SchoolName,Subject1Marks,Subject2Marks and Average respectively and
these values are separated by "|"(excluding double quotes).

ANSWERS
vi script.sh
(AFTER ENTERING INTO VI EDITOR PRESS i FOR INSERTION OF TEXT INTO
SCRIPT.SH , then type as below)
awk ‘BEGIN{FS=”#”;OFS=”|”}{s=($4+$5)/2if( s > 90 ) print $1,$2,$3,$4,$5,s;}’
$HOME/unix_subjectaverage/Input.txt
(AFTER COMPLETION OF THIS CODE PRESS ESC AND TYPE :wq AND PRESS ENTER,
the type like this in terminal) sh script.sh

9. CONTENT DISPLAY
ANSWERS (Paste this in script.sh)
cat $1

ls $1

10. SKILL UPGRADE


ANSWER
(Type this code in script.sh using vi editor)
cd /home/scrapbook/tutorial/unix_skillupgradeproject/
for file in *
do if [ -f $file ]
then
sed –e
's/CPP/PYTHON/;s/ORACLE/MYSQL/;s/DB2/MYSQL/;s/JAVA/PYTHON/;s/DOTNET/PYTHON/'
$file > $file
fi
done
AFTER THIS EXIT THE VI EDITOR AND RUN SCRIPT.SH FILE IN TERMINAL (sh script.sh)

Вам также может понравиться