Basic Commands and Introduction to Shell Scripting



Objective:
·         Understand and Practice some basic commands of Linux
·         Learn about shell scripting
·         Learn basic shell commands in comparison of C language
·         Practice some basics programs in shell

Shell:
Shell is a UNIX term for the interactive user interface with an operating system. The shell is the layer of programming that understands and executes the commands a user enters. In some systems, the shell is called a command interpreter.
Command
Description
ls
List the files in the working directory
ls /bin
List the files in the /bin directory (or any other directory you care to specify)
ls -1
List the files in the working directory in long format

Cd:
To change your working directory (where you are standing in the maze) you use the cd command. To do this, type cd followed by the pathname of the desired working directory. A pathname is the route you take along the branches of the tree to get to the directory you want.
Command
Description
cd ~
To go directly to the home directory, no matter where you are right now
cd ..
Change directory to the last directory you were in before changing to wherever you are now

Command
Description
mkdir mydir
makes a directory called mydir
rmdir mydir
removes directory called mydir. mydir must be empty
cp –r dir newdir
copies the whole directory dir to newdir. –r must be specified to copy directory contents recursively
rm mydir
this will delete directory mydir along with all its content
mv dir subdir
moves the directory called dir to the directory called subdir
Command//File
Description// File
touch myfile
creates a file called myfile. updates the timestamp on the file if it already exists, without modifying its contents
cp myfile myfile2
copies myfile to myfile2. if myfile2 exists, this will overwrite it!
rm myfile
removes file called myfile
cat new.txt
displays the contents of new.txt
more new.txt
displays the contents of new.txt screen by screen. spacebar to pagedown, q to quit
head new.txt
displays first 10 lines of new.txt
tail new.txt
displays last 10 lines of new.txt
mv myfile newlocdir
moves myfile into the destination directory newlocdir
mv myfile newname
renames file to newname. if a file called newname exists, this will overwrite it!

Command// Extra
Description// Extra Command
Date
Show the current data and time
anycommand > myfile
redirects the output of anycommand writing it to a file called myfile
date > timestamp
redirects the output of the date command to a file in the current directory called timestamp
anycommand >> myfile
appends the output of anycommand to a file called myfile
date >> timestamp
appends the current time and date to a file called timestamp. creates the file if it doesn’t exist
cal -y
free calendar, courtesy unix


Q1: Practice all the commands that we have discussed above in this lab and compare these terminal operations with the GUI operations.

Shell Script:
Computer understand the language of 0's and 1's called binary language, In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in O/s there is special program called Shell. Shell accepts your instruction or commands in English and translates it into computers native binary language.
 How to write a shell script
·         Use any editor like vi or mcedit to write shell script. You can also use built in terminal editor by using nano command.
·         After writing shell script set execute permission for your script as follows syntax:
chmod permission your-script-name
Examples:
chmod +x your-script-name
chmod 755 your-script-name
Note: This will set read write execute permission for owner, for group and other permission is read and execute only.
·         ls -l command earlier presented a long of listing file with a line like the following for eac file:
-rw-r--r-- 1 root user 0 2009-04-28 08:26 newfile.txt
Here the first character in the first column (-) indicates that the file is a normal file. The next 9 characters indicate the access permissions for the file. The next set of 9
characters is divided into3 groups of 3 cha- racters. Purpose of these characters is as under:
(-) represents no permission
(r) represents 'read' permission
(w) represents 'write' permission
(x) represents 'execute' permission
Permission
Octal Number
Equivalent Symbol
Read
4
r--
Write
2
-w-
Execute
1
--x

You can use the chmod command to change the access permissions of a file or a directory. To specify permissions for a file with chmod, any of the following two methods can be used.
Symbol
Meaning / Description
u
User
g
Group
o
Other
a
All (equals to ugo)
+
Add permission
-
Remove permission
r
Read permission
w
Write permission
x
Execute permission
·         Execute your script as syntax:
bash your-script-name
sh your-script-name
./your-script-name
Examples:
bash bar
sh bar
./bar
4: Text processing
catconcatenate files and print on the standard output
Usage: cat [OPTION] [FILE]...
eg. cat file1.txt file2.txt
cat when supplied with more than one file will concatenate the files without any header information.
cat ­ used to display the contents of a small file on terminal
usage: cat [file name]
cat- To create file
Usage: cat > [file name]
NOTE: Press and hold CTRL key and press D to stop or to end file (CTRL+D)
cat – to apend text at the end of file
 Usage: cat >> [file name]
 NOTE: Press and hold CTRL key and press D to stop or to end file (CTRL+D)
 echo – display a line of text
 Usage: echo [OPTION] [string] ...
 eg. echo I love pakistan
 echo $HOME
read- inputs a value in a variable
read num1
echo $num1
wc - command is used to count lines, words and characters, depending on the option used.
 Usage: wc [options] [file name]
You can just print number of lines, number of words or number of charcters by using following options:
 ­l : Number of lines
 ­w : Number of words
 ­c : Number of characters
 Eg. wc file.txt count number of lines, words and character (including whitespaces , newline etc) in a file.
grep ­ print lines matching a pattern
Usage: grep [OPTION] PATTERN [FILE]...
eg. grep ­i apple sample.txt
Options:
-i case-insensitive search
-n show the line# along with the matched line
-v invert match, e.g. find all lines that do NOT match
-w match entire words, rather than substrings
6: Variables
When a script starts all environment variables are turned into shell variables. New variables can be instantiated like this:
name=value
You must do it exactly like that, with no spaces either side of the equals sign, the name must only be made up of alphabetic characters, numeric characters and underscores; it cannot begin with a numeric character.
7: Arithmetic Expression
Arithmetic expansion is also allowed and comes in the form: $((expression))
The value of the expression will replace the substitution. Eg: echo $((1 + 3 + 4)) Will echo "8" to stdout
Example:
echo “Hello World”
Output:
Hello World



Example:
echo “Enter 1st  Number”
read $n1
echo “Enter 2nd Number”
read $n2
echo $(($n1 + $n2))



Q2: Write a Shell Script that enters two numbers and perform all mathematical operations on those. (Addition, Subtraction, Multiplication and Division)
Q3: Write a Shell Script that will enter width and length of a rectangle from user and will calculate its area. NOTE: Area of rectangle = width x length

Post a Comment

0 Comments