Makeinfo Blog

Follow

Makeinfo Blog

Follow

Creating Dynamic URLs: A Step-by-Step Guide to Automatically Generating Incrementing Website Links

Dorothi Viki's photo
Dorothi Viki
·Jan 11, 2023·

3 min read

In this blog post, we will walk you through the step-by-step process of creating dynamic URLs that automatically generate incrementing website links using a Bash script.

By using bash, you can easily generate the links you need for your website, with the added benefit of being able to automate the process. We will provide a script that you can follow along with, as well as tips on how to troubleshoot any issues you may encounter.

So, let's get started and learn how to create dynamic URLs that automatically generate incrementing website links!

What is Bash scripting?

Bash scripting is a type of scripting in which the Bash command-line shell is used to execute commands. Bash scripts typically have the file extension .sh and are used to automate tasks on Linux and macOS systems. These scripts can include a combination of shell commands, built-in Bash commands, and command-line utilities, and can be used to perform tasks such as file management, system administration, and software building.

Requirements

In order to run a Bash script, you need a system that has the Bash shell installed. Bash is the default shell on most Linux and macOS systems, so it is likely already installed on your system.

To run a Bash script, you need to do the following:

  1. Make the script executable: You can make a script executable by running the command chmod +x scriptname.sh.

  2. Run the script: You can run the script by providing the path to the script file to the Bash shell, like this: bash scriptname.sh.

Alternatively, you could also execute the script using ./scriptname.sh if the script is in the current directory and you have permission to execute it.

You need also to check if the script uses any external command not included on your system default. In this case, you need to install the package containing it or use the full path.

You can also include the path to the interpreter in the first line of the script (#!/bin/bash)

Script

#!/bin/bash
for i in {1..9}
do
    echo "https://www.makeinfo.co/example/$i"
done

The first line #!/bin/bash is called the "shebang" line, it's an instruction to the shell indicating that the script should be executed by /bin/bash.

The second line for i in {1..9} is a for loop that is used to iterate over a sequence of numbers. The {1..9} is a range of numbers that the loop will iterate over. The variable i will take on the value of each number in the range, starting with 1 and ending with 9.

The third line is the command echo which prints a message on the console, in this case, it prints a URL using the variable i from the loop. This line is indented, indicating that it's a part of the loop and it will execute for each iteration of the loop.

The last line done is used to indicate the end of the loop.

In summary, this script will create a for loop that starts at 1 and ends at 9, for every iteration, it will output a URL with the form https://www.makeinfo.co/example/ concatenated with the variable i. The URLs generated will be: https://www.makeinfo.co/example/1

https://www.makeinfo.co/example/2

https://www.makeinfo.co/example/3

...

https://www.makeinfo.co/example/9

It is a simple script but it could be useful when combined with others utilities like wget or curl to download something, or with other elements of your website to generate specific URLs.

Output Screenshot

References

https://stackoverflow.com/questions/17984809/how-do-i-create-an-incrementing-filename-in-python

Let's connect,

 
Share this