User Tools

Site Tools


Action disabled: revisions
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?do=revisions
/web/htdocs/www.skenz.it/home/data/pages/cs/bash_language/arguments.txt · Last modified: 2020/11/26 23:18 (external edit)