Thursday, May 7, 2015

Chain Operators in Linux

1. Ampersand  (&)
   With the help of Ampersand we can run any linux command in background and go and grab 

   abhishek@myfedora:~$ ping ­c5 &

   Run two command in background, simultaneously:

   abhishek@myfedora:/home/abhiz# yum update -y & service httpd status &

2. semi-colon (;)
   With the help of semi-colon we can run, several linux commands in a single go and the 
   execution of command occurs sequentially.

   abhishek@myfedora:/home/abhishek# yum update -y ; service httpd status ; mkdir test

   The above command combination will first execute update instruction, then httpd service 
   status instruction and finally will create a ‘test‘ directory under the current working directory.

3. AND (&&)
   With the help of AND Operator (&&) we can execute the second command only, if the 
   execution of first command success. This command is very useful in checking the execution 
   status of last command.

   For example, I want to visit website using lynx command, in 
   terminal but before that I need to check if the host is live or not.

   abhishek@myfedora:/home/abhishek# ping -c3 && lynx

4. OR (||)
   With the help of OR Operator (||) we can execute second command only if the execution of 
   first command fails, i.e., the exit status of first command is ‘1‘.

   For example, I want to execute ‘yum update -y‘ from non-root account and if the first 
   command fails, then the second ‘lynx‘ command will execute.

   abhishek@myfedora:~$ yum update -y || lynx
5. NOT (!)
   With the help of NOT Operator we can execute all commands command except the condition 
   provided. To understand this, create a directory abhishek in your home directory and ‘cd‘ to it.

   abhishek@myfedora:~$ mkdir abhishek  
   abhishek@myfedora:~$ cd abhishek
   Next, create several types of files in the folder abhishek.

   abhishek@myfedora:~/abhishek$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html
   See we’ve created all the new files within the folder abhishek.

   abhishek@myfedora:~/abhishek$ ls 

   a.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml
   Now delete all the files except ‘html‘ file all at once, in a smart way.

   abhishek@myfedora:~/abhishek$ rm -r !(*.html)
   Just to verify, last execution. List all of the available files using ls command.

   abhishek@myfedora:~/abhishek$ ls 

   a.html  b.html
6. AND – OR operator (&& – ||)
    Its a combination of ‘AND‘ and ‘OR‘ Operator. 

   For example, let’s do ping to, if success echo ‘Verified‘ else echo 
   ‘Host Down‘.

   abhishek@myfedora:~/abhishek$ ping -c3 && echo "Verified" || 
   echo "Host Down"

7. PIPE (|)
   With PIPE output of first command acts as an input to the second command. For example, 
   pipeline the output of ‘ls -l‘ to ‘less‘ and see the output of the command.

   abhishek@myfedora:~$ ls -l | less

8. Command Combination {} 
    Combine two or more commands, the second command depends upon the execution of the 
    first command.

   For example, check if a file ‘linux.txt‘ and ‘unix.txt‘ is available under my Downloads directory 
   or not, and output corresponding output.

   abhishek@myfedora:~$ [ -f /home/abhishek/Downloads/linux.txt ] || echo “The file does not 
   abhishek@myfedora:~$ [ -f /home/abhishek/Downloads/unix.txt ] || echo “The file does not 

   “The file does not exist”

9. Precedence ()
   The Operator makes it possible to execute command in precedence order.

   Command1 && Command2 || Command3 && Command4.

   In the above pseudo command, what if the Command1 fails? Neither of the Command2, 
   Command3, Command4 would executed, for this we use Precedence Operator, as:

   (Command1 && Command2) || (Command3 && Command4)
   In the above pseudo command, if Command1 fails, Command2 also fails but Still Command3 
   and Command4 executes depends upon exit status of Command3.

10. Concatenation (\)
      With the help of  Concatenation Operator (\) as the name specifies, is used to concatenate 
      large commands over several lines in the shell. For example, The below command will open       text file test(1).txt.

      abhishek@myfedora:~/Downloads$ nano test\(1\).txt

No comments: