My bash script is not doing what i expect

I have a bash script and i made it myself, however it seems to be riddles with errors as i the flags are not working right
if -d is in command then say filenames and dont download, otherwise commence download

this is an iso downloader and it will pull down roughly 10-16 GB, i am very proud of it, however i need help

#!/bin/bash

while getopts "hf:" flag; do
 case $flag in
   h) # Handle the -h flag
   # Display script help information
   ;;
   d) # debug mode
   SIMULATE=TRUE
   ;;
   \?)
   SIMULATE=FALSE
   ;;
 esac
done


function downloadFileAs() {
  if [ $SIMULATE == "FALSE" ]
  then
    wget $1 -NO $2
  else
    echo $1 with name $2
  fi

  
}


mkdir openSUSE
mkdir openSUSE/Leap
mkdir openSUSE/Tumbleweed



leapfirsthalf='https://download.opensuse.org/distribution/openSUSE-current/iso'
wget "$leapfirsthalf" -O 'leapscan.html'
LEAPLOAD=`grep -oP '\.\/openSUSE-Leap-[0-z][0-z]\.[0-z]-DVD-x86_64-Current\.iso">' leapscan.html`
LEAPLOAD="${LEAPLOAD:1}"
LEAPLOAD="${LEAPLOAD::-2}"
rm 'leapscan.html'
downloadFileAs "$leapfirsthalf$LEAPLOAD" 'openSUSE/Leap/X86_64-leap-current.iso'

downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-RESCUE.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-live(KDE).iso'


mkdir kolibri
downloadFileAs 'https://builds.kolibrios.org/rus/kolibri.iso' 'kolibri/kolibriOS-ru.iso'
downloadFileAs 'https://builds.kolibrios.org/eng/kolibri.iso' 'kolibri/kolibriOS-en.iso'


downloadFileAs 'http://www.tinycorelinux.net/14.x/x86/release/CorePlus-current.iso' 'X86-tinyCORE_linux.iso'


wget 'https://www.kali.org/get-kali/#kali-live' -O 'kaliscan.html'
KALILOAD=`grep -oP 'https:\/\/cdimage\.kali\.org\/kali-[0-z][0-z][0-z][0-z]\.[0-z]\/kali-linux-[0-z][0-z][0-z][0-z]\.[0-z]-live-amd64\.iso\>' kaliscan.html`
KALILOAD="${KALILOAD::-1}"
rm 'kaliscan.html'
downloadFileAs "$KALILOAD" 'X86_64-kali-live.iso'

First off, BASH scripts are read top to bottom, function definitions should be at the top of the file

Next, you don’t define d for getops so it will never be passed to the case and evaluated

Also, your script relies on SIMULATE being defined, but it never is. First because you don’t handle -d at all, and second because ? isn’t a default case so unless you pass -? to the script (and handle that) that case is never called.

Either write your if statement to handle SIMULATE with a default value of FALSE or define SIMULATE as FALSE before the case statement so that when you handle -d it will simply be overwritten if you call the script with that.

#!/bin/bash



function downloadFileAs() {
  if [ SIMULATE == TRUE ]
  then
    wget $1 -NO $2
  else
    echo $1 with name $2
  fi

  
}



while getopts "hd?:" flag; do
 case $flag in
   h) # Handle the -h flag
   # Display script help information
   ;;
   d) # debug mode
   SIMULATE=TRUE
   ;;
   \?)
   SIMULATE=FALSE
   ;;
 esac
done





mkdir openSUSE
mkdir openSUSE/Leap
mkdir openSUSE/Tumbleweed



leapfirsthalf='https://download.opensuse.org/distribution/openSUSE-current/iso'
wget "$leapfirsthalf" -O 'leapscan.html'
LEAPLOAD=`grep -oP '\.\/openSUSE-Leap-[0-z][0-z]\.[0-z]-DVD-x86_64-Current\.iso">' leapscan.html`
LEAPLOAD="${LEAPLOAD:1}"
LEAPLOAD="${LEAPLOAD::-2}"
rm 'leapscan.html'
downloadFileAs "$leapfirsthalf$LEAPLOAD" 'openSUSE/Leap/X86_64-leap-current.iso'

downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-RESCUE.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-live-KDE.iso'


mkdir kolibri
downloadFileAs 'https://builds.kolibrios.org/rus/kolibri.iso' 'kolibri/kolibriOS-ru.iso'
downloadFileAs 'https://builds.kolibrios.org/eng/kolibri.iso' 'kolibri/kolibriOS-en.iso'


downloadFileAs 'http://www.tinycorelinux.net/14.x/x86/release/CorePlus-current.iso' 'X86-tinyCORE_linux.iso'


wget 'https://www.kali.org/get-kali/#kali-live' -O 'kaliscan.html'
KALILOAD=`grep -oP 'https:\/\/cdimage\.kali\.org\/kali-[0-z][0-z][0-z][0-z]\.[0-z]\/kali-linux-[0-z][0-z][0-z][0-z]\.[0-z]-live-amd64\.iso\>' kaliscan.html`
KALILOAD="${KALILOAD::-1}"
rm 'kaliscan.html'
downloadFileAs "$KALILOAD" 'X86_64-kali-live.iso'

is this good? what else did i miss?

Enabling shellcheck in your code editor / IDE is a good first step to catching any major mistakes (and less obvious ones too). I use it in VSCode.

Fur such 4 liners most user don‘t use a fully fledged code editor/IDE as it is a total overkill. A simple text editor is used in most cases, but these don‘t support spellcheck. But such short bash scripts don‘t even need such gimmicks. And you learn much more by doing a research and troubleshooting yourself


It’s IntelliSense for shell. Check it out before dismissing it :face_holding_back_tears:

I am using Kate where should i check?

I don’t think it has a plugin for Kate.
You can simply do shellcheck <your-script> from the terminal.

It’s available in the official repos:

Information for package ShellCheck:
-----------------------------------
Repository     : base-oss
Name           : ShellCheck
Version        : 0.9.0-4.6
Arch           : x86_64
Vendor         : openSUSE
Installed Size : 21.4 MiB
Installed      : No
Status         : not installed
Source package : ShellCheck-0.9.0-4.6.src
Upstream URL   : https://hackage.haskell.org/package/ShellCheck
Summary        : Shell script analysis tool
Description    : 
    The goals of ShellCheck are:

    * To point out and clarify typical beginner's syntax issues, that causes a
    shell to give cryptic error messages.

    * To point out and clarify typical intermediate level semantic problems, that
    causes a shell to behave strangely and counter-intuitively.

    * To point out subtle caveats, corner cases and pitfalls, that may cause an
    advanced user's otherwise working script to fail under future circumstances.

i have “fixed” the script but behavior is still the same

#!/bin/bash


function downloadFileAs() {
  if [ "$SIMULATE" == TRUE ]
  then
    wget "$1" -NO "$2"
  else
    echo "$1" with name "$2"
  fi

  
}



while getopts "hd?:" flag; do
 case $flag in
   h) # Handle the -h flag
   # Display script help information
   ;;
   d) # debug mode
   SIMULATE=TRUE
   ;;
   \?)
   SIMULATE=FALSE
   ;;
 esac
done





mkdir openSUSE
mkdir openSUSE/Leap
mkdir openSUSE/Tumbleweed



leapfirsthalf='https://download.opensuse.org/distribution/openSUSE-current/iso'
wget "$leapfirsthalf" -O 'leapscan.html'
LEAPLOAD=$(grep -oP '\.\/openSUSE-Leap-[0-z][0-z]\.[0-z]-DVD-x86_64-Current\.iso">' leapscan.html)
LEAPLOAD="${LEAPLOAD:1}"
LEAPLOAD="${LEAPLOAD::-2}"
rm 'leapscan.html'
downloadFileAs "$leapfirsthalf$LEAPLOAD" 'openSUSE/Leap/X86_64-leap-current.iso'

downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-RESCUE.iso'
downloadFileAs 'https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-x86_64-Current.iso' 'openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-live-KDE.iso'


mkdir kolibri
downloadFileAs 'https://builds.kolibrios.org/rus/kolibri.iso' 'kolibri/kolibriOS-ru.iso'
downloadFileAs 'https://builds.kolibrios.org/eng/kolibri.iso' 'kolibri/kolibriOS-en.iso'


downloadFileAs 'http://www.tinycorelinux.net/14.x/x86/release/CorePlus-current.iso' 'X86-tinyCORE_linux.iso'


wget 'https://www.kali.org/get-kali/#kali-live' -O 'kaliscan.html'
KALILOAD=$(grep -oP 'https:\/\/cdimage\.kali\.org\/kali-[0-z][0-z][0-z][0-z]\.[0-z]\/kali-linux-[0-z][0-z][0-z][0-z]\.[0-z]-live-amd64\.iso\>' kaliscan.html)
KALILOAD="${KALILOAD::-1}"
rm 'kaliscan.html'
downloadFileAs "$KALILOAD" 'X86_64-kali-live.iso'

the -d flag is not active therefore it should be downloading iso images, not echoing their location as it would in test mode

usr_40476@localhost:/run/media/usr_40476/Ventoy> iso.bash
 iso.bash: command not found
usr_40476@localhost:/run/media/usr_40476/Ventoy> bash iso.bash
iso.bash: line 1: ï»ż#!/bin/bash: No such file or directory
mkdir: cannot create directory ‘openSUSE’: File exists
mkdir: cannot create directory ‘openSUSE/Leap’: File exists
mkdir: cannot create directory ‘openSUSE/Tumbleweed’: File exists
--2024-03-10 08:21:28--  https://download.opensuse.org/distribution/openSUSE-current/iso
Resolving download.opensuse.org (download.opensuse.org)... 2a07:de40:b250:131:10:151:131:30, 195.135.223.226
Connecting to download.opensuse.org (download.opensuse.org)|2a07:de40:b250:131:10:151:131:30|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: /distribution/openSUSE-current/iso/ [following]
--2024-03-10 08:21:28--  https://download.opensuse.org/distribution/openSUSE-current/iso/
Reusing existing connection to [download.opensuse.org]:443.
HTTP request sent, awaiting response... 200 OK
Length: 34627 (34K) [text/html]
Saving to: ‘leapscan.html’

leapscan.html                                                       100%[==================================================================================================================================================================>]  33.82K  --.-KB/s    in 0.1s    

2024-03-10 08:21:29 (243 KB/s) - ‘leapscan.html’ saved [34627/34627]

https://download.opensuse.org/distribution/openSUSE-current/iso/openSUSE-Leap-15.5-DVD-x86_64-Current.iso with name openSUSE/Leap/X86_64-leap-current.iso
https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso with name openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed.iso
https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-x86_64-Current.iso with name openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-RESCUE.iso
https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-x86_64-Current.iso with name openSUSE/Tumbleweed/X86_64-openSUSE_tumbleweed-live-KDE.iso
mkdir: cannot create directory ‘kolibri’: File exists
https://builds.kolibrios.org/rus/kolibri.iso with name kolibri/kolibriOS-ru.iso
https://builds.kolibrios.org/eng/kolibri.iso with name kolibri/kolibriOS-en.iso
http://www.tinycorelinux.net/14.x/x86/release/CorePlus-current.iso with name X86-tinyCORE_linux.iso
--2024-03-10 08:21:29--  https://www.kali.org/get-kali/
Resolving www.kali.org (www.kali.org)... 2606:4700::6812:49f, 2606:4700::6812:59f, 104.18.4.159, ...
Connecting to www.kali.org (www.kali.org)|2606:4700::6812:49f|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘kaliscan.html’

kaliscan.html                                                           [ <=>                                                                                                                                                               ] 103.42K  --.-KB/s    in 0.06s   

2024-03-10 08:21:29 (1.57 MB/s) - ‘kaliscan.html’ saved [105905]

https://cdimage.kali.org/kali-2024.1/kali-linux-2024.1-live-amd64.iso with name X86_64-kali-live.iso
usr_40476@localhost:/run/media/usr_40476/Ventoy> 

It “should do” only what you programmed it to do and you programmed it to download the iso images if the value of $SIMULATE is TRUE. And SIMULATE is set to TRUE if you pass -d flag.

Now you’ve messed up your logic, and SIMULATE still isn’t getting set reliably. Fix your if statement, change the case and you probably don’t want the ? case at all
It’s also good behaviour to add a default case.

SIMULATE=FALSE
while getopts "hd?:" flag; do
 case $flag in
   h) # Handle the -h flag
   # Display script help information
   ;;
   d) # debug mode
   SIMULATE=TRUE
   ;;
   *) # Default case displays script usage
      # echo usage here
   ;;
 esac
done

OR

if [ ${SIMULATE:=FALSE} == "FALSE" ]
  then
    # do stuff
  fi
1 Like

THANK YOU!!!
the script works now, i will keep this knowledge in mind and go back to this thread when i have problems, (I also am terrible at bash but I am doing my best to get better!)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.