glugt-mgl

 

Advanced shell operation

Page history last edited by yazhini 2 yrs ago

<filters, sort, cut, paste, uniq>

 

 

Filters

Filters are commands which accept input from the standard input, manipulate it and print the result at the standard output. Redirection and pipelining can be combined with filters to provide more functionality. Some of the most useful filters are listed here.

Cut - to slice a file vertically, with a space as the default delimiter. Suppose a file called mylist contains the following data.

cs10501 12 ; 5.6

cs10502 14 ; 5.7

cs10503 12 ; 5.8

cs10504 13 ; 5.9

cs10505 11 ; 5.1

The command "cut -c 3-7 mylist" will output what's below.

10501

10502

10503

10504

10505

Output ends with the line before this one. Columns are numbered starting from 1 and the "-c" option has to be followed by a list of columns you want printed. All these commands are valid: "cut -c 1-10 mylist", "cut -c 1-10,13-15 mylist" and "cut -c 3 mylist".

The "-d" option lets you specify the delimiter. It must be used along with "-f". The list of fields to be printed follows "f". The command "cut -d ';' -f 1 mylist" will print the following.

cs10501 12

cs10502 14

cs10503 12

cs10504 13

cs10505 11

Paste -

 

 

Redirection and Pipelining

Filters take input from the standad input but input can be supplied to them through files too. The ouput of a filter can also be supplied as the input to a filter. This is achieved using redirection and pipelining. The redirection operators are < and >. The command "file1 > file2" simply makes a copy of file1 called file2. The command "date" prints the date at the terminal. The command "date > mydate" writes the date into a file and names it mydate. If a file called mydate already exists, it will be overwritten. The command "cut -c 1-10 < mylist" does the same as the command "cut -c 1-10 mylist".

<pipelining here>

 

Shell scripts

A shell script is a set of commands written in a text file. The file mostly has ".sh" as its extension with execute permissions but shell scripts can have any/no extension and be run even without an execute permission.

Can take inputs from the user or a file and output them.

Useful to create our own commands.

Save lots of time.

Can be used to automate some tasks in day-to-day life.

System Administration can be automated.

 

Writing a Shell script

Let us consider a shell script which takes a backup of your home folder and stores the backup in a different location.

1. Write the shell commands in the script, one in a line using your favourite editor. Let the file have a ".sh" extension. Example - backup.sh.

2. Set execute permission for your script

 yazhini@localhost:~/chmod a+x backup.sh

                       or

 yazhini@localhost:~/chmod 755 backup.sh

 

3. Execute your script 

yazhini@localhost:~/ sh backup,sh

        or

yazhini@localhost:~/ ./backup,sh

 

What to write in backup.sh?

Let us assume that you want to take a backup of all the files in your home folder and store it in a different place (say /backup)

1. Let us create a zip of all the files in home

So the command "zip /backups/backup.zip ~/" will create a backup called backup.zip in /backups. But no, it's a boring name to give to a backup file. If you'd read through the section on the commands cut, tr and pipelining, you'll know that the command "date|cut -d ' ' -f 2,3,6" will output

 

/* Have to think about what script to write . Backup is very simple :-( and finish this */

 

Comments (0)

You don't have permission to comment on this page.