Home > Back-end >  Check if input matches string argument in a bash script
Check if input matches string argument in a bash script

Time:09-23

I need to check if a commandline argument matches the one in a bash script, here is my code.

#!/bin/bash

# lets call this script.sh

myFunction() {
if [[ $2 == '--log' ]]; then
    echo "hello world" >> file.log
else
    echo "Unknown argument"
fi
}

myFunction

Sample input:

bash script.sh --log

But doesn't seem to write anything into file.log

CodePudding user response:

You will need to pass the script's arguments to myFunction:

myFunction "$@"

The "$@" means "all the arguments passed to this script."

  • Related