Home > Software design >  How to run a function only if it matches the specified URL in bash script?
How to run a function only if it matches the specified URL in bash script?

Time:11-25

I am doing a project which kinda behaves like autologin. Below is the bash script:

DISPLAY=:0.0 xdotool type $WUSER
DISPLAY=:0.0 xdotool key Tab
DISPLAY=:0.0 xdotool type $DECPASS
DISPLAY=:0.0 xdotool key Return

There will be a default URL for login page (eg: https://github.com/login), which will:

  • automatically type in the username
  • press tab key
  • type in the password
  • click enter upon the chromium startup.

The problem is, I want the function to run only if the login page matches the URL https://github.com/login. So, if I wanted to set any other website URLs as default it will not run, unless I navigate to the github login page. Then only it runs. The thing I am trying to explain is like this line from a .json file.

"matches": ["https://github.com/login"]

CodePudding user response:

In bash, if can be used to check it your URL is valid or not. Since you have not posted any code, I assume a lot of things, but it will demonstrate the idea.

#!/bin/bash
#
url="$1"
if [[ "$url" == "https://github.com/login" ]]
then
    echo "The url is valid, proceding..."
    # PUT YOUR CODE HERE
else
    echo "The url is not valid, stopping the script..."
    exit 1
fi

But since only "https://github.com/login" is valid, you could not ask it at all and hard code it in the script.

#!/bin/bash
#
url="https://github.com/login"

# rest of the script follows
......

This is pretty basic in bash, you should read through a bash tutorial to get you started.

  • Related