How to count and verify passed arguments?

How can I translate the following Ruby code to Bash?

if ARGV.length == 0 abort "\nError: The project name is required. Aborting...\n\n" elsif ARGV.length > 2 abort "\nError: The program takes two arguments maximum. Aborting...\n\n" end 
+6
source share
3 answers
 #!/bin/bash USAGE="$0: <project name> [subproject attribute]" if [ $# -lt 1 ]; then echo -e "Error: The project name is required.\n$USAGE" >&2; exit 1; fi if [ $# -gt 2 ]; then echo -e "Error: Two arguments maximum.\n$USAGE" >&2; exit 1; fi 
+5
source

What follows is what you need:

 #!/bin/bash if [ $# -eq 0 ]; then echo -e "\nError: The project name is required. Aborting...\n\n" exit 1 elif [ $# -gt 2 ]; then echo -e "\nError: The program takes two arguments maximum. Aborting...\n\n" exit 1 fi 

The TLDP bash guide is very good if you want to learn bash, see the TDLP bash guide .

+4
source

May be:

 #!/bin/bash function functionName { if [ $# = 0 ] then echo "\nError: The project name is required. Aborting...\n\n"; exit 1 fi if [ $# \> 2 ] then echo "\nError: The program takes two arguments maximum. Aborting...\n\n"; exit 1 fi } functionName a 
+2
source

Source: https://habr.com/ru/post/889781/


All Articles