Shell Scripting – 5

  1. Standard I/P and Standard O/P
  2. Redirecting I/P and O/P
  3. Pipes

  1. Standard I/P and Standard O/P
    • Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from a place called standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is also your terminal by default.
    • I/P from standard input i.e. terminal and O/P to standard O/P i,e, terminal
      [shell_test@localhost ~]$ ls -l
      total 32
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Desktop
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Documents
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Download
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Music
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Pictures
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Public
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Templates
      drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Videos
      [shell_test@localhost ~]$

  2. Redirecting I/P and O/P
    • Redirecting O/P to a file i.e. other than terminal

      1. Using ‘>’
      2. [shell_test@localhost ~]$ ls -l > listoffiles
        [shell_test@localhost ~]$ cat listoffiles
        total 32
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Desktop
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Documents
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Download
        -rw-rw-r–. 1 shell_test shell_test 0 2009-11-14 14:35 listoffiles
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Music
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Pictures
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Public
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Templates
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Videos
        [shell_test@localhost ~]$

      3. Append more results using ‘>>’
      4. [shell_test@localhost ~]$ ls >> listoffiles
        [shell_test@localhost ~]$ cat listoffiles
        total 32
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Desktop
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Documents
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Download
        -rw-rw-r–. 1 shell_test shell_test 0 2009-11-14 14:35 listoffiles
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Music
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Pictures
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Public
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Templates
        drwxr-xr-x. 2 shell_test shell_test 4096 2009-11-09 06:29 Videos
        Desktop
        Documents
        Download
        listoffiles
        Music
        Pictures
        Public
        Templates
        Videos
        [shell_test@localhost ~]$

    • Getting I/P from files

      1. use ‘<'
      2. [shell_test@localhost ~]$ wc -l listoffiles
        19 listoffiles
        [shell_test@localhost ~]$

  3. Pipes
    • Pipes are used to redirect O/P of one command to I/P of other command
      see following example

      1. [shell_test@localhost tmp]$ who > users
        [shell_test@localhost tmp]$ cat users
        gpvprasad tty1 2009-11-18 21:38 (:0)
        gpvprasad pts/0 2009-11-18 22:06 (:0.0)
        [shell_test@localhost tmp]$ wc -l users
        2 users
        [shell_test@localhost tmp]$

      which can be executed in single line using ‘|’

      1. [shell_test@localhost tmp]$ who | wc -l
        2
        [shell_test@localhost tmp]$

next

స్పందించండి

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / మార్చు )

Twitter picture

You are commenting using your Twitter account. Log Out / మార్చు )

Facebook photo

You are commenting using your Facebook account. Log Out / మార్చు )

Google+ photo

You are commenting using your Google+ account. Log Out / మార్చు )

Connecting to %s