Shell or bash script to open multiple URLs saved in a .TXT file in a browser from the terminal?

iincognito15

New Member
Joined
Sep 25, 2022
Messages
8
Reaction score
1
Credits
104
Is there a shell or bash script to open multiple URLs saved in a .TXT file in a browser from the terminal?

I know that can be done with "firefox $(cat url.txt)", but:

  • I need something that saves me time by opening tabs of a maximum of 10 to 10 urls (because of RAM) from a list of 100 urls or more.
  • Which has sequential order, that they not repeat themselves and that it notify me when it reaches the end of the list.
  • That it only repeats or start again from number 1, when I close and open the terminal?
I appreciate your help!!
 


I did something that required running through a list of urls for monitoring purposes. Not going to share that work with you but doing a quick search of the Internet I found a web page that you could possibly derive a solution from... Good luck..

 
This script will open multiple urls from a json file with fzf in your default browser. jq, fzf and xdg-open must be installed on your system. xdg-open can be replaced by the browser bin name (firefox, chromium, etc)
 
Last edited:
This Bash script try to work, but the problem is the "access token"

I got this message on terminal: % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 194 100 166 100 28 194 32 --:--:-- --:--:-- --:--:-- 227 Error al obtener la información del usuario: The access token is invalid or not found in the request.

#!/bin/bash

# Nombre de usuario de TikTok a verificar
username="123abc456def"

# Realizar la solicitud a la API para obtener la información del usuario
response=$(curl -L -X POST 'https://open.tiktokapis.com/v2/research/user/info/?fields=is_private' \
-H 'Content-Type: application/json' \
-d '{"username": "'"$username"'"}')

# Verificar si la solicitud fue exitosa
if [ "$(echo "$response" | jq -r '.error.code')" == "ok" ]; then
# Obtener el valor de "is_private" del resultado de la solicitud
is_private=$(echo "$response" | jq -r '.data.is_private')

# Verificar si el usuario está transmitiendo en vivo
if [ "$is_private" == "false" ]; then
echo "El usuario $username está transmitiendo en vivo."
else
echo "El usuario $username no está transmitiendo en vivo."
fi
else
# Error al obtener la información del usuario
error_message=$(echo "$response" | jq -r '.error.message')
echo "Error al obtener la información del usuario: $error_message"
fi
 
So let me get this straight:
  • You want to read a bunch of URL's from a file. Possibly over 100.
  • You want to load them into firefox, no more than 10 at a time.

The best way to do this would be to read each line of the file into an array, then determine how many sets of 10 URL's are in the file and how many odd URL's are left to load.
Then load each URL into firefox in batches of 10. Finally load the final set of left-over tabs/links.

Which off the top of my head would look something like this:
fftabs:
Bash:
#!/usr/bin/env bash

# fftabs - a script to read URL's from a text file and load them
# 10 at time into firefox.
# Takes a path to a text file as a parameter, containing URL's

# Global list of URL's
declare -g UrlList

# Build a firefox command that opens a number of tabs at once
runff()
{
ffcmd="Firefox"
for (( i=$1; i<$2; i++)); do
    url="${UrlList[$i]}"
  ffcmd+=" -new-tab -url $url"
done

echo -e "${ffcmd}\n"
# run the firefox command in the background
"$ffcmd" &>/dev/null &
}

# If we have a valid file as a parameter, we'll read it.
if [[ -f $1 ]]; then
  readarray -t UrlList < $1
  numUrls=${#UrlList[@]}
  echo "Read $numUrls URLs from file $1.\n"
  if [[ $numUrls -gt 0 ]]; then
    # determine how many sets of 10 URLs are in the list
    numSets=$(bc <<< "$numUrls/10")
    # determine the number of remaining URLs at the end of the list
    remainder=$(bc <<< "$numUrls%10")
    start=0
    end=10
    # Run each set of 10
    for (( set=0; set<numSets; set++)); do
      runff $start $end
      # Prompt user to press a key when they're ready to load the next 10 tabs
      read -p "Press any key to load the next tabs..." -n1 -s
      echo -e "\n"
      start=$((end));
      if (( $set<$numSets )); then
        end=$((end+10));
      fi
    done
    # Open the remaining URLs
    if [[ $remainder -ne 0 ]]; then
      end=$((start+remainder))
      runff $start $end
    fi
    echo "Finished!"
    exit 0
  fi
  echo -e "$file does not contain any data\n"
fi
echo -e "Usage:\n    fftabs /path/to/file\n"
echo -e "Where file contains URL's on separate lines.\n"
exit 1

It reads each line in the file into an array called UrlList.
It uses bc (GNU Terminal based calculator) to determine how many sets of 10 URL's are in the file and to determine how many tabs are left-over at the end.
Then we use a loop to run the appropriate number of times, using the runff function to open the URL's in firefox, in sets of 10, before opening the remaining set of URLs.
After loading a set of tabs, the script currently prompts you to "Press any key to load the next tabs". So when you're ready to load the next 10 tabs - you focus the terminal and press any key and it will load the next 10 tabs into firefox.
Alternatively, if you don't want to press a key to get the script to load the next 10 tabs - you could use the sleep command, to make the script sleep for a set amount of time, before loading the next set of tabs.
e.g.
sleep 120 to make the script sleep for 2 minutes, instead of using the read -p "Press any key....." command.

Hopefully this is close to what you want.
Note:
The script DOES NOT validate the URL's in the file - it just blindly reads each line from the file into an array and sends each complete line to Firefox.
So your file will need to look like this:
Code:
https://www.linux.org/
https://www.someothersite.com/
etc.

Also, the script uses bc to calculate how many sets of 10 URL's are in the file.
So in order for the script to work properly - you must have bc installed.
 
Last edited:
This Bash script try to work, but the problem is the "access token"

I got this message on terminal: % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 194 100 166 100 28 194 32 --:--:-- --:--:-- --:--:-- 227 Error al obtener la información del usuario: The access token is invalid or not found in the request.

#!/bin/bash

# Nombre de usuario de TikTok a verificar
username="123abc456def"

# Realizar la solicitud a la API para obtener la información del usuario
response=$(curl -L -X POST 'https://open.tiktokapis.com/v2/research/user/info/?fields=is_private' \
-H 'Content-Type: application/json' \
-d '{"username": "'"$username"'"}')

# Verificar si la solicitud fue exitosa
if [ "$(echo "$response" | jq -r '.error.code')" == "ok" ]; then
# Obtener el valor de "is_private" del resultado de la solicitud
is_private=$(echo "$response" | jq -r '.data.is_private')

# Verificar si el usuario está transmitiendo en vivo
if [ "$is_private" == "false" ]; then
echo "El usuario $username está transmitiendo en vivo."
else
echo "El usuario $username no está transmitiendo en vivo."
fi
else
# Error al obtener la información del usuario
error_message=$(echo "$response" | jq -r '.error.message')
echo "Error al obtener la información del usuario: $error_message"
fi
I don't see a password anywhere. I don't know anything about tiktok but I see a post without full credentials. I wouldn't expect that to work.
 

Staff online


Top