0

I have an assignment to write a shell script that (among other things) when executed without any arguments, the script will perform the following steps:

Update all system packages Install the Nginx software package Configure nginx to automatically start at system boot up. Copy the website documents to the web document root directory. Start the Nginx service.

I have written the tasks that the script is supposed to do, but I don't know how to structure the script so that it does these tasks ONLY when it is run without any arguments.

Whenever I run the script - argument or not, these tasks are executed. I've wrapped the tasks in a function but I don't know how to prompt it to only run when executed without an argument:

#!/bin/bash
# assign variables
ACTION=${1}
Version=1.0.0

function default(){
sudo yum update -y
sudo yum install httpd -y
sudo yum install git -y
sudo amazon-linux-extras install nginx1.12 -y
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
sudo aws s3 cp s3://index.html /usr/share/nginx/html/index.html
}


...

case "$ACTION" in
        -h|--help)
                display_help
                ;;
    -r|--remove)
        script_r_function
                ;;
        -v|--version)
                ;;
                show_version "Version"
                ;;
        default
        *)
        echo "Usage ${0} {-h|-r|-v}"
        exit 1
esac
redwytnblak
  • 143
  • 1
  • 1
  • 10
  • 1
    Does this answer your question? [How do I find the number of arguments passed to a Bash script?](https://stackoverflow.com/questions/4423306/how-do-i-find-the-number-of-arguments-passed-to-a-bash-script) – kaylum Sep 30 '20 at 02:04
  • 1
    Above post tells you how to get the number of args passed to the script. So can just run the function when that value is 0. – kaylum Sep 30 '20 at 02:05
  • @kaylum Maybe I’m missing something but I wasn’t trying to find out the number of arguments but rather run the function in my code (default) that installs the required package ONLY when run without any arguments. – redwytnblak Sep 30 '20 at 03:08
  • 1
    And that is exactly what checking for no argument will give you. If the number is 0 you run the function, otherwise you execute the case statement. – tink Sep 30 '20 at 03:14
  • "when run without any arguments" == "number of args is 0". Not sure why that isn't clear. If you have a way for getting the number of args the script was called with then you can work out whether there were any args or not. – kaylum Sep 30 '20 at 03:23

1 Answers1

0

Wrapping your "case" code inside if else :

#!/bin/bash
# assign variables
ACTION=${1}
if [ "$#" -eq 0 ]; then
    echo "no arguments"
else
case "$ACTION" in
        -h|--help)
                echo "help"
                ;;
        -r|--remove)
                echo "remove"
                ;;
        -v|--version)
                echo "version"
                ;;
        *)
        echo "wrong aruments"
        exit 1
esac
fi
  • When i give no arguments :
./boo
no arguments
  • when i give wrong argument :
 ./boo -wrong
wrong aruments
  • when i give valid argument (ex: version):
./boo -v
version
confused genius
  • 2,876
  • 2
  • 16
  • 29