Skip to main content
added 204 characters in body
Source Link
slm
  • 380k
  • 127
  • 793
  • 897

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit [ $# == 2 ] || exit echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3 

I'll leave the comparison of the 2 directories to you. For learning Bash I'd direct you to the link below for a free online book on Bash.

References

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit [ $# == 2 ] || exit echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3 

I'll leave the comparison of the 2 directories to you.

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit [ $# == 2 ] || exit echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3 

I'll leave the comparison of the 2 directories to you. For learning Bash I'd direct you to the link below for a free online book on Bash.

References

added 95 characters in body
Source Link
slm
  • 380k
  • 127
  • 793
  • 897

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit [ $# == 2 ] || exit  echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3 

I'll leave the comparison of the 2 directories to you.

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

I'll leave the comparison of the 2 directories to you.

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit [ $# == 2 ] || exit  echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

If we're given more than 2 arguments:

$ ./cmd.bash d1 d2 d3 

I'll leave the comparison of the 2 directories to you.

Source Link
slm
  • 380k
  • 127
  • 793
  • 897

Processing arguments

Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script:

$ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" 

Now run with no arguments:

$ ./cmd.bash arg1: arg2: 

With 1 argument:

$ ./cmd.bash hi arg1: hi arg2: 

With 2 arguments:

$ ./cmd.bash hi bye arg1: hi arg2: bye 

Checking the arguments

You can then check if the arguments, 1 & 2, are valid directories or not, and then bail out or proceed as needed. So we introduce 2 checks to see if the 2 arguments are directories or not, if not then exit.

$ cat cmd.bash #!/bin/bash [ -d "$1" ] || exit [ -d "$2" ] || exit echo "arg1: $1" echo "arg2: $2" 

Example

Say we have these directories.

$ mkdir d1 d2 $ ls -l total 12 -rwxrwxr-x. 1 saml saml 89 Oct 14 23:13 cmd.bash drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d1 drwxrwxr-x. 2 saml saml 4096 Oct 14 23:14 d2 

If we are given anything other than 2 directories, the script will simply exit.

$ ./cmd.bash hi bye 

If we're given 2 directories:

$ ./cmd.bash d1 d2 arg1: d1 arg2: d2 

I'll leave the comparison of the 2 directories to you.