Unix: combine commands with control operators








  • Run the second command only if the first command return a zero exit status
    • cd a/b/c && tar zxvf ~/archive.tar.gz
      
    • the command above extracts archive.tar.gz to a/b/c directory if a/b/c exists.
  • Run the second command only if the first command return a non-zero exit status
    • cd a/b/c || mkdir -p a/b/c
    • the command above create a/b/c directory, if it does not exist
  • You can also chain the commands with both operators: 
    • cd a/b/c || mkdir -p a/b/c && tar zxvf ~/archive.tar.gz
    • the command above make directory a/b/c and extracts archive.tar.gz into it.



No comments:

Post a Comment