Lab Guide #11

Department of Computer Technology and Information Systems
CTIS156 – Information Technologies II
Fall 2008 -2009
Lab Guide #11
OBJECTIVE: Advanced File Processing, Bash Programming
Instructor
Assistant
: Prof. Mustafa AKGUL
: Baran TOPAL
Date : 21.07.2008
Week : 13
1.
Name 2 commands to compress and uncompress files in Linux operating system. (Linux
commands: compress, uncompress; GNU tools: gzexe, gzip, gunzip, zcat,zforfe, zmore, and zgrep)
2.
In your home directory, using cat command or in vi editor, have a file named as batman and write
the followings:
<File Content>
Batman: The Animated Series (often shortened Batman: TAS or BTAS) is a two-time Emmy Awardwinning American animated series adaptation of the comic book series featuring the DC Comics
superhero, Batman.
The visual style of the series is based on the artwork of producer Bruce Timm. The original episodes,
produced by Bruce Timm and Eric Radomski, were first aired on the Fox Network from 1992 to 1995.
</File Content>
(cd ~, cat > batman or vi batman, be in insert mode)
3.
In the file content, be careful about the bold characters. Give a command to make the characters
bold.
(:s/search_pattern or /search_pattern or ?search_pattern other than those, there is no such command in
vi editor or using cat command, you cannot make the characters seen as bold)
4.
Now, compress the file you have just created above. (gzip batman, batman.gz will be created)
5.
Now search for batman file you have created above. (ls –l batman or find will conclude that there
isn’t the original file, only the compressed file resides in home directory or find . -name batman)
6.
Which command can be used to perform the reverse operation and bring compressed files back?
Uncompress the above file. (gunzip batman.gz or gzip –d batman.gz)
7.
Have directory named as anime in your home directory and compress it using gzip command.
(mkdir anime, you cannot compress a directory using gzip command, you archive the directory to obtain
the corresponding archive file. Then you compress this file by the gzip command.)
8.
Archive your directory above you created and then compress it. (tar [-]pcvf anime.tar ~/anime,
gzip anime.tar, gzip anime.tar)
9.
Display your history file. What kind of file is this? (echo $HISTFILE, ~/.bash_history, hidden file)
10.
Display the complete list of history and view it with more command. (history | more)
11.
Display your last 3 commands you executed in 2 ways. (history 3 or history | tail -3)
12.
Execute the 6th command you executed before. (!6)
13. Guess the location of the cat command. Which command is used for locating the cat command?
(Since it is a command, the file may be in bin directory, whereis cat, /bin/cat /usr/bind/cat and the man
pages of the cat command or whereis –b cat can be used to show the commands executable path)
14. Create a file using cat command as calculator and have “5*6” and using bc command have the
operation by taking the calculator file’s input. (cat > calculator, 5*6, bc < calculator)
15. Again using bc command, have the operation by taking the calculator file’s input, but this time,
have the output to result file with single command and view the file with cat command. (bc < calculator
> result, cat result)
16. Apply cat < batman and if the operation is successful, redirect it batman.success file, if there is
an error, redirect to batman.error file. (cat < batman > batman.success 2> batman.error)
17. In your home directory, have a file named as spiderman. Write the content using any editor or
command.
<File Content>
Spider-Man (Peter Benjamin Parker) is a Marvel Comics superhero, created by writer-editor Stan Lee
and artist Steve Ditko.
</File Content>
(cat > spiderman or vi spiderman or less spiderman or pico spiderman)
18.
In octal mode, have the access privilege of the spiderman file is as following:



The user has no right to read the file. Other rights are granted to the user.
The group has no right to write to the file. Other rights are granted to the group.
The others has no right to execute the file. Other rights are granted to the others.
Apply cat < spiderman and if the operation is successful, redirect it spiderman.success file, if there is
an error, append the error redirection to spiderman.error file. (cat > spiderman, chmod 356 spiderman
cat < spiderman > spiderman.success >> spiderman.error, spiderman.success has no file content since
the user has no right to read the file as above, so the spiderman.error file will have the content)
19. Give read permission to the user and apply cat < spiderman. Examine the result. (chmod u+r
spiderman, there will be no error)
20. What is the difference between cat < spiderman and cat spiderman? (< character takes the input
from the file, spiderman, however, in the second one, spiderman is passed is passed as command line
argument to cat command)
21.
Locate the executable file for more command. (whereis –b more, b is for binaries (executables))
22. Change your working directory to root directory. Search in the root directory and all sub
directories for wc command. Do not use whereis command to find the path of wc command. (cd /, find .
-name ["]wc["] –print, do not bother permission denied results, wc will come in the end. The -print option
will print out the path of any file that is found with that name. In general -print will print out the path of
any file that meets the find criteria, you do not need to use print option)
23. Download the coffee jpeg file in my web site and save as coffee.jpg in your home directory. Now
search for the file and display the output to the console. (find ~ –name coffee.jpg -print, you do not need
to use print option)
24. Search for the file that has an extension of “.h” in /usr/include directory in a recursive manner
and print the absolute pathname of the file. (find /usr/include –name *.h -print, you do not need to use
print option)
25. Search for files in tmp directory whose owner is “ctis” user which is/are newer than stdio.h where
reside(s) in /usr/include directory. Be sure you are in the root directory before executing the command.
(cd /, find /tmp –newer /usr/include/stdio.h -user ctis)
26. In your home directory, create a file named as yourname1.sh with touch command and in the
file declare an yourid and yourname and print the output to the console and run it. Do not forget to
change access permission as execution permitted.
(touch baran1.h, write the content:
#!/bin/bash
declare –i id=1234
declare –rx NAME=BARAN
echo $id
echo $NAME
chmod u+x baran1.h and run by the command: ./baran1.sh If fails give the necessary rights to the file to
run by chmod u+x baran1.h,
Output: 1234 BARAN
)
27.
Modify yourname1.sh in your home directory and run it.
#!/bin/bash
declare –i id=1234
declare –rx NAME=BARAN
echo ${name:=$NAME}
echo ${name:+Ulvi} ${id:-$id}
echo $name $id
(vi baran1.sh, ./baran1.sh,
Output:
BARAN
ULVI 1
BARAN 1
)
28.
Modify yourname1.sh in your home directory and run it.
<script>
#!/bin/sh
echo What is your name?
read MY_NAME
echo "Hello $MY_NAME - hope you will pass IT 156 course."
</script>
(vi baran1.sh, ./baran1.sh,
Output:
What is your name?
Baran
Hello Baran – hope you will pass IT 156 course.
)
29.
Modify yourname1.sh with the following code segment and run it.
<script>
#!/bin/bash
echo Please, enter your firstname and lastname
read FN LN
echo "Hi! $LN, $FN !"
</script>
(vi baran1.sh, ./baran1.sh,
Output:
Please, enter your firstname and lastname
baran topal
Hi! topal, baran !
)
30. Create a directory as yournameyoursurname and create a file named as helloworld.sh with cat
command or vi editor and write a bash program that outputs “Hello World”.
(Traditional 2 hello world script:
<script>
#!/bin/bash
echo Hello World
</script>
<script>
#!/bin/sh
MY_MESSAGE="Hello World"
echo $MY_MESSAGE
</script>
mkdir barantopal, cd barantopal, cat > helloworld.sh, chmod u+x helloworld.sh, ./helloworld.sh, in the first
script, the script has only two lines. The first indicates the system which program to use to run the file.
The second line is the only action performed by this script, which prints 'Hello World' on the terminal.
If you get something like ./hello.sh: Command not found. Probably the first line '#!/bin/bash' is wrong,
issue whereis bash or how to find where bash is located. 'locate bash' is preferred, but not all machines
have locate. 'find ./ -name bash' from the root dir will work, usually.
Suggested locations to check:
ls -l /bin/bash
ls -l /sbin/bash
ls -l /usr/local/bin/bash
ls -l /usr/bin/bash
ls -l /usr/sbin/bash
ls -l /usr/local/sbin/bash
In the second script, there is an assignment of the string "Hello World" to the variable MY_MESSAGE
then echoes out the value of the variable. Note that, we need the quotes around the string Hello World.
Whereas we could get away with echo Hello World because echo will take any number of parameters, a
variable can only hold one value, so a string with spaces must be quoted to that the shell knows to treat it
all as one. Otherwise, the shell will try to execute the command World after assigning
MY_MESSAGE=Hello)
In bash programming, just like any other programming language, there are test operators.
Test Operators in bash programming:
operator produces true if...
number of operands
-n
operand non zero length
1
-z
operand has zero length
1
-d
there exists a directory whose name is operand
1
-f
there exists a file whose name is operand
1
-eq
the operands are integers and they are equal
2
-neq
the opposite of -eq
2
=
the operands are equal (as strings)
2
!=
opposite of =
2
-lt
operand1 is strictly less than operand2 (both operands
2
should be integers)
-gt
operand1 is strictly greater than operand2 (both operands
2
should be integers)
-ge
operand1 is greater than or equal to operand2 (both
2
operands should be integers)
-le
operand1 is less than or equal to operand2 (both operands
2
should be integers)
Variables:
You can use variables as in any programming languages. There are no data types. A variable in bash can
contain a number, a character, a string of characters.
4
You have no need to declare a variable, just assigning a value to its reference will create it.
31.
Create a file named as helloIT.sh with cat in your yournameyoursurname directory and run it.
<script>
#!/bin/bash
STR="Hello IT!"
echo $STR
</script>
(cat > helloIT.sh, chmod u+x helloIT.sh, ./helloIT.sh, line 2 creates a variable called STR and assigns the
string "Hello World!" to it. Then the VALUE of this variable is retrieved by putting the '$' in at the
beginning. Please notice (try it!) that if you don't use the '$' sign, the output of the program will be
different, and probably not what you want it to be)
Local variables:
Local variables can be created by using the keyword local.
32.
Modify helloIT.sh as following and run it.
<script>
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=IT
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
</script>
(vi helloIT.sh, ./helloIT.sh, local variable is treated in its own defined scope,
Output:
Hello
IT
Hello
)
Conditionals:
Conditionals let you decide whether to perform an action or not, this decision is taken by evaluating an
expression.
Conditionals have many forms. The most basic form is: if expression then statement where 'statement' is
only executed if 'expression' evaluates to true. '2<1' is an expresion that evaluates to false, while '2>1'
evaluates to true.
Conditionals have other forms such as: if expression then statement1 else statement2. Here 'statement1'
is executed if 'expression' is true,otherwise 'statement2' is executed.
Yet another form of conditionals is: if expression1 then statement1 else if expression2 then statement2
else statement3. In this form, there's added only the "ELSE IF 'expression2' THEN 'statement2' which
makes ‘statement2’ being executed if ‘expression2’ evaluates to true. The rest is as you may imagine
About syntax:
The base for the 'if' constructions in bash is as follows:
<script>
if [expression];
then
5
code if 'expression' is true.
fi
</script>
33. Create a file named as conditional.sh in your yournameyoursurname directory and run the
following.
<script>
#!/bin/bash
if [ "foo" = "foo" ]; then
echo “Expression evaluated as true”
fi
</script>
(cd barantopal, vi conditional.sh, chmod u+x conditional.sh, ./conditional.sh, the code to be executed if
the expression within braces is true can be found after the 'then' word and before 'fi' which indicates the
end of the conditionally executed code, be careful about blanks.
Output:
Expression evaluated as true
)
34.
Modify the conditional.sh file with the following code segment and run it.
<script>
#!/bin/bash
if [ "foo" = "foo" ]; then
echo “Expression evaluated as true”
else
echo “Expression evaluated as false”
fi
</script>
(vi conditional.sh,./conditional.sh, be careful about blanks.
Output:
Expression evaluated as true
)
35.
Modify the conditional.sh file with the following code segment and run it.
<script>
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo “Expression evaluated as true”
else
echo “Expression evaluated as false”
fi
</script>
(vi conditional.sh, ./conditional.sh, be careful about blanks.
Output:
Expression evaluated as false
)
Loops for, while and until:
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a
series of 'words' within a string.
The while executes a piece of code if the control expression is true, and only stops when it is false (or a
explicit break is found within the executed code.
6
The until loop is almost equal to the while loop, except that the code is executed while the control
expression evaluates to false.
If you suspect that while and until are very similar you are right.
36.
Create a file named as loop.sh in your yournameyoursurname directory and run the following.
<script>
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
</script>
(vi loop.sh, chmod u+x loop.sh, ./loop.sh, on the second line, we declare i to be the variable that will take
the different values contained in $( ls ). The third line could be longer if needed, or there could be more
lines before the done (4). 'done' (4) indicates that the code that used the value of $i has finished and $i
can take a new value, Output should vary related with ls command result)
(This raises a natural question: why doesn't bash allow the C like for loops for (X=1,X<10; X++) As it
happens, this is discouraged for a reason: bash is an interpreted language, and a rather slow one for that
matter. For this reason, heavy iteration is discouraged)
37.
Modify the loop.sh file with the following code segment and run it.
<script>
#!/bin/bash
for i in `seq 1 10`;
do
echo $i
done
</script>
(vi loop.sh, chmod u+x loop.sh, ./loop.sh
Output:
123456789
with new lines for each element, for single quotation use Alt Gr+comma key on your keyboard)
38.
Modify the loop.sh file with the following code segment using variable and run it.
<script>
#!/bin/bash
myvar=0
while [ $myvar -ne 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
</script>
(vi loop.sh, ./loop.sh, the output will be a variable looping exactly 10 times, starting from 0 to 9 with new
lines, be careful about blanks)
39.
Modify the loop.sh file with the following code segment using until and run it.
<script>
#!/bin/bash
myvar=0
until [ $myvar -eq 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))
7
done
</script>
(vi loop.sh, ./loop.sh, the output will be a variable looping exactly 10 times, starting from 0 to 9 with new
lines, be careful about blanks)
40.
Modify the loop.sh file with the following code segment and run it.
<script>
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ];
do
echo “The counter” is $COUNTER
let COUNTER=COUNTER+1
done
</script>
(vi loop.sh, ./loop.sh, the output will be a variable looping exactly 10 times, starting from 0 to 9 with new
lines, be careful about blanks)
41.
Modify the loop.sh file with the following code segment and run it.
<script>
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ];
do
echo COUNTER $COUNTER
let COUNTER-=1
done
</script>
(vi loop.sh, ./loop.sh, the output will be a variable looping exactly 10 times,
Output:
COUNTER 20
COUNTER 19
…
COUNTER 10
)
42.
Modify the loop.sh file with the following code segment and run it.
<script>
#!/bin/bash
X=0
while [ $X -le 20 ]
do
echo $X
X=$((X+1))
done
</script>
(vi loop.sh, ./loop.sh, the output will be a variable looping exactly 21 times, starting from 0 to 20 with new
lines, be careful about blanks)
43.
Modify the loop.sh file with the following code segment and run it.
<script>
#!/usr/bin/env bash
for x in one two three four
do
echo number $x
8
done
</script>
(vi loop.sh, ./loop.sh
Output:
number one
number two
number three
number four
What exactly happened? The "for x" part of our "for" loop defined a new environment variable (also called
a loop control variable) called "$x", which was successively set to the values "one", "two", "three", and
"four". After each assignment, the body of the loop (the code between the "do" ... "done") was executed
once. In the body, we referred to the loop control variable "$x" using standard variable expansion syntax,
like any other environment variable. Also notice that "for" loops always accept some kind of word list after
the "in" statement)
44. Write the following script to a file named as myfile.sh in your yournameyoursurname directory
and run it.
<script>
#!/usr/bin/env bash
for myfile in /etc/r*
do
if [ -d "$myfile" ]
then
echo "$myfile (dir)"
else
echo "$myfile"
fi
done
</script>
(vi myfile.sh, chmod u+x myfile.sh, ./myfile.sh
Output:
/etc/rc.d (dir)
/etc/resolv.conf
/etc/resolv.conf~
/etc/rpc
The above code looped over each file in /etc that began with an "r". To do this, bash first took our
wildcard /etc/r* and expanded it, replacing it with the string /etc/rc.d /etc/resolv.conf /etc/resolv.conf~
/etc/rpc before executing the loop. Once inside the loop, the "-d" conditional operator was used to perform
two different actions, depending on whether myfile was a directory or not. If it was, a " (dir)" was
appended to the output line)
9