Basic BASH Command
I/O Redirection
1. Input/Output Redirection in BASH( Again Shell) - Most command in Linux reads input and write output.By default input is given by Keyboard and output is given to terminal(monitor).
Every command has three file descriptor : STDIN, STDOUT, STDERR.
STDIN : 0 - Comes from keyboard
STDOUT : 1 - goes to console
STDERR : 2 - goes to console
Not all command listen for STDIN(standard Input) some of them only have argument. for example ls command , it is not waiting for input, it optionally work with argument
Redirection operator
it is possible to redirect STDIN, STDOUT, and STDERR to other places.Input Redirection with 0< or < - for redirect STDIN
Redirection is done using 0< or <(less than symbol) operator. 0 is optional.
[yogesh@lap ~]$ read fromkb
hello i am reading from keyboard and save it fromkb variable, use echo $fromkb to view my content [yogesh@lap ~]$ echo $fromkb hello i am reading from keyboard and save it fromkb variable, use echo $fromkb to view my contentNow we use < or 0< for redirect STDIN, I create a message file message.txt using vi editor
$ vi message.txtPreviously we saw that read command read from KEYBOARD we use STDIN redirect , now it read from message.txt file and store it into variable fromfile, we can see the value of fromfile variable using echo command.
[yogesh@lap ~]$ cat message.txt read command by default read from keyboard now < or 0< is used for redirect STDIN, now it read content form file and store it into variable tempfile [yogesh@lap ~]$ read fromfile < message.txt [yogesh@lap ~]$ echo $fromfile read command by default read from keyboard now < or 0< is used for redirect STDIN, now it read content form file and store it into variable tempfile [yogesh@lap ~]$
Output Redirection with 1> or > - for redirect STDOUT
> or 1> is used to redirect STDOUT, For example ls command list the content of directory at terminal but if we want to redirect it to a file the we use > or 1>. In 1> ,No need to type 1 , it is presumed.
[yogesh@lap test]$ ls file1, file2 message.txt [yogesh@lap test]$ ls > ls.out [yogesh@lap test]$ cat ls.out file1, file2 ls.out message.txt /*****we can use 1> produce same result******/ [yogesh@lap test]$ ls 1>ls.out [yogesh@lap test]$ ls file1, file2 ls.out message.txt [yogesh@lap test]$ cat ls.out file1, file2 ls.out message.txt [yogesh@lap test]$
Output Redirection 2> - for redirect STDERR
[yogesh@lap test]$ ls file1 file2 ls.out message.txt [yogesh@lap test]$ ls file1 file3 ls: cannot access file3: No such file or directory file1 [yogesh@lap test]$ ls file1 file3 2>errorlog.out file1 [yogesh@lap test]$ cat errorlog.out ls: cannot access file3: No such file or directory
& - Ampersand is an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to.
[yogesh@lap test]$ ls file1 file2 message.txt [yogesh@lap test]$ ls 1>test.out 2>&1 [yogesh@lap test]$ ls file1 file2 message.txt test.out [yogesh@lap test]$ cat test.out file1 file2 message.txt test.out [yogesh@lap test]$>> Operator - >> operator append text to a file instead of overwriting it.
[yogesh@lap test]$ ls file1 file2 message.txt test.out [yogesh@lap test]$ echo hello test file1 > file1 [yogesh@lap test]$ cat file1 hello test file1 [yogesh@lap test]$ echo hello i am test file2 > file2 [yogesh@lap test]$ cat file2 hello i am test file2 [yogesh@lap test]$ echo i am write some content to file1 > file1 [yogesh@lap test]$ cat file1 i am write some content to file1 [yogesh@lap test]$ echo i am append some content to file2 using append operator >> file2 [yogesh@lap test]$ cat file2 hello i am test file2 i am append some content to file2 using append operator [yogesh@lap test]$By default | (pipe symbol above Enter Key US Keyboard) - Pipe the result of one command to other command. i.e. output of one command become input of another command. for example
[yogesh@lap test]$ cat itemlist 1 2 4 5 6 7 19 3 8 12 9 [yogesh@lap test]$ cat itemlist | sort -n 1 2 3 4 5 6 7 8 9 12 19 [yogesh@lap test]$
Writing to STDOUT and File simultaneously
tee command is used in this case, tee command send output to one or more file in one move.tee - a is used to append.
for example.
[yogesh@lap test]$ date | tee f1 f2 f3 Fri Jan 15 14:21:17 IST 2016 [yogesh@lap test]$ cat f1 Fri Jan 15 14:21:17 IST 2016 [yogesh@lap test]$ cat f2 Fri Jan 15 14:21:17 IST 2016 [yogesh@lap test]$ cat f3 Fri Jan 15 14:21:17 IST 2016 [yogesh@lap test]$ cat teetest | tee -a f1 f2 f3 cat: teetest: No such file or directory [yogesh@lap test]$ echo content added to f1 f2 and f3 | tee -a f1 f2 f3 content added to f1 f2 and f3 [yogesh@lap test]$ cat f1 && cat f2 && cat f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3 [yogesh@lap test]$ cat f1 f2 f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3 Fri Jan 15 14:21:17 IST 2016 content added to f1 f2 and f3
No comments:
Post a Comment