User Tools

Site Tools


cs:bash_language:arguments
Return to Home page

Arguments

Concepts:
How to pass arguments from a shell to a bash script

Text:
Implement a bash script that:

  • prints the number of arguments passed to the script
  • checks the number of arguments passed to the script and, in the case the number is different than 3, the script must print an error message before stopping the execution
  • prints the name of the script
  • prints the arguments passed to the script (in two different ways)

Solution:

arguments.sh
#!/bin/bash
 
echo "Number of arguments: $#"
if [ $# -ne 3 ]
then
  echo "Usage: $0 arg1 arg2 arg3"
  exit 1
fi
 
echo $0
echo $1 $2 $3
echo $*

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/bash_language/arguments
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_language/arguments.txt · Last modified: 2019/02/26 14:35 (external edit)

Privacy Policy