Shell Programming 1
Shell Programming 1
com 1
UNIX/LINUX
SHELL PROGRAMMING - 1
--by Nataraja Rao
http://studyhacktivism.blogspot.com
Objectives
2
5
http://studyhacktivism.blogspot.com
Creating Files
6
$ touch file1
This creates a file called ‘file1’.
$ cat > file1 then press enter and type data whatever
you wants to write in that “file1”.
http://studyhacktivism.blogspot.com
To save & close that ‘file1’ press enter and then press
Ctrl d.
In Unix the keys Ctrl d indicate the EOF or end of file
character.
To see the contents of the created ‘file1’
$ cat file1
cat command can concatenate the contents of two files
and store them in third file.
$ cat file1 file2 > file3
To append data into a file, we will replace ‘>’ with
‘>>’.
http://studyhacktivism.blogspot.com
File Manipulation
8
$ cp file1 file2
This will copy the contents of ‘file1’ into a file ‘file2’.
$ ls p*
The above command will list the files, which is starts
with letter “p”.
$ ls ?ile
When the shell comes across a ‘?’. It understands
that the symbol signifies any single character.
The * is interpreted by the shell as presence of any
number of characters.
http://studyhacktivism.blogspot.com
$ ls [aeiou]*
This indicates that the first character of the filename
to be listed must be any one of the letters given
within the square brackets, and the remaining can
be anything.
$ ls [!aeiou]*
The ! Symbol will exclude specified characters
http://studyhacktivism.blogspot.com
$ ls [a-m][c-z][4-9]??
This will list all 5 character filenames in the current
directory whose first character is in the range a to
m, the second character is in the range c to z, the
third character is in the range 4 to 9, whereas the
fourth and fifth are any valid characters.
http://studyhacktivism.blogspot.com
$ ls –l (long listing)
Unix treats all entities – files, directories, devices as
files.
To differentiate between all of them it uses file
types.
http://studyhacktivism.blogspot.com
File Permissions
22
5 = read(4) + execute(1)
Third digit is Other users permissions
5 = read(4) + execute(1)
http://studyhacktivism.blogspot.com
How come that the file permissions for file1 file have
been set to 644 ?
How come that the folder permissions for folder1 have
been set to 755?
What unix does is it uses the value stored in a variable
called umask to decide the default permissions.
http://studyhacktivism.blogspot.com
Useful commands
31
Unix knows each user not only by the login name, but
also by two numbers called the user and group identity
numbers.
“id” to show identity numbers
“uname” to find the name of the Unix system.
“tty” to find out the name of your terminal file
“whoami” to find some information about yourself.
“who” to see who all are currently sharing the network
with you.
“date” to display the current date and time.
http://studyhacktivism.blogspot.com
Reference books
33
Questions ?
34