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.
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âŠ
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.
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
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!)