Command chaining Or multiple commands at same time
#pwd;echo "Hello World"
[davsingh@noisecover-dr Scripts]$ ls -l && pwd ---> If first run then second will execute otherwise it will not work
total 16
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 a
-rw-r--r-- 1 davsingh users 0 Jan 20 14:18 ABC.txt.txt
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 b
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 c
-rw-r--r-- 1 davsingh users 0 Jan 20 11:29 junk1.data.txt.txt
-r--r--r-- 1 davsingh users 5956 Jan 20 11:29 passwd.txt.txt
drwxr-xr-x 2 davsingh users 4096 Jan 21 12:58 test
-rwxr-xr-x 1 davsingh users 166 Jan 20 14:21 upper-lower.sh.txt
/home/davsingh/Scripts
[davsingh@noisecover-dr Scripts]$ cat abc && pwd ----> first fails so second command not work
cat: abc: No such file or directory
[davsingh@noisecover-dr Scripts]$ cat abc || pwd --> If first fails then second runs
cat: abc: No such file or directory
/home/davsingh/Scripts
[davsingh@noisecover-dr Scripts]$ ls -l && pwd ---> If first run then second will execute otherwise it will not work
total 16
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 a
-rw-r--r-- 1 davsingh users 0 Jan 20 14:18 ABC.txt.txt
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 b
-rw-r--r-- 1 davsingh users 0 Jan 21 12:52 c
-rw-r--r-- 1 davsingh users 0 Jan 20 11:29 junk1.data.txt.txt
-r--r--r-- 1 davsingh users 5956 Jan 20 11:29 passwd.txt.txt
drwxr-xr-x 2 davsingh users 4096 Jan 21 12:58 test
-rwxr-xr-x 1 davsingh users 166 Jan 20 14:21 upper-lower.sh.txt
/home/davsingh/Scripts
[davsingh@noisecover-dr Scripts]$ cat abc && pwd ----> first fails so second command not work
cat: abc: No such file or directory
[davsingh@noisecover-dr Scripts]$ cat abc || pwd --> If first fails then second runs
cat: abc: No such file or directory
/home/davsingh/Scripts
It basically works on the exit status of command and we can group it with multiple commands.
ReplyDeletee.g
It can help us in decision making to great deal.
[anil@indian ~]$ [ 5 == 5 ] && echo "yes" || echo "no"
yes
[anil@indian ~]$ [ 5 == 7 ] && echo "yes" || echo "no"
no
Same way we can group multiple commands in [] , based on exit status of group of commands we can execute desired command