default value for a variable in bash script - bash

I want to give my variable in bash script a default value.
Here is the script :
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]]; then
DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi
kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}
mv KAGGLE_LOCATION .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/
cd data/
unzip *.zip
rm *.zip
When I do echo $DEFAULT_LOCATION, the correct value comes up. But when I do $KAGGLE_LOCATION and dont enter any cmd argument, I get DEFAULT_LOCATION as output instead of the actual value. What is wrong with my code?
PS: I have never used a Mac, so I am not sure if location is same for unix as Mac. If its different, please comment.

You are not expanding the variable, try expanding the variable like "$KAGGLE_LOCATION"
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]];then
DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi
kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}
mv "$KAGGLE_LOCATION" .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/
cd data/
unzip *.zip
rm *.zip

Related

What am I missing in this simple Bash script?

#!/bin/bash
#Toggle Script
# $dirserver/A -> $dirproject/{trunk|branches}/A
if [[ "$1" == "dw" || -z "$1" ]]; then
echo "[+] Delete old link ( $dirserver/A )... "
rm "$dirserver/A"
if [[ "$(readlink -f $dirserver/A)" == *"branches"* ]]; then
ln -s "$dirproject/trunk/A" "$dirserver/A"
echo "[+] Done. You are now in TRUNK"
else
ln -s "$dirproject/branches/A" "$dirserver/A"
echo "[+] Done. You are now in BRANCH."
fi
fi
Expected functionality: Do toggle between symlinks, BRANCH or TRUNK .
Error: ./toggle.sh dw Always end in BRANCH.
Notes: No. There is no word "branches" when it points to trunk.
Thank you in advance.
You are deleting the old link before checking it. Just do not delete it at all, only overwrite.
if [[ "$1" == "dw" || -z "$1" ]]; then
if [[ "$(readlink -f "$dirserver/A")" == *branches* ]]; then
ln -f -s "$dirproject/trunk/A" "$dirserver/A"
else
ln -f -s "$dirproject/branches/A" "$dirserver/A"
fi
fi

Bash double condition: "Yy" or free string but not "Nn"

I'm trying for my update script to find a way to get a double if with conditions: I can type "Yy" to download the latest version, or type anything else (another version number) but which is not "Nn" (basically cancelling the task).
Tried that, it's working for the "Yy" and the free text but I still can type "Nn" and it downloads me the n.zip file (it should stop the task)
if [[ "$latest_confirm" =~ ^[Yy]$ ]] || [[ ! "$latest_confirm" =~ ^[Nn]$ ]]; then
I think you want something like this:
latest_version=4.9
read latest_confirm
if [[ $latest_confirm =~ ^[Yy]$ ]]; then
version=$latest_version
elif [[ $latest_confim =~ ^[Nn]$ ]]; then
...
else
version=$latest_confirm
fi
...
Instead of the else, you might want another reg-ex comparison to make sure the value is a valid version number.
Finally worked this way if anyone wanting to know about the solution to update my installed softwares:
upgrade_pma() {
old="pma-old"
new="pma-new"
header "PHPMYADMIN UPGRADE"
latest_version=$(check_version pma)
ask_latest "PHPMyAdmin" $pma_version $latest_version true
# Detect version to install
if [[ "$install_version" =~ ^[Yy]$ ]]; then
install_version_nb=$latest_version
version_url="https://files.phpmyadmin.net/phpMyAdmin/$latest_version/phpMyAdmin-$latest_version-all-languages.zip"
else
install_version_nb=$install_version
version_url="https://files.phpmyadmin.net/phpMyAdmin/$install_version/phpMyAdmin-$install_version-all-languages.zip"
fi
# Download file and process install
if [[ ! "$install_version" =~ ^[Nn]$ ]]; then
cd $tools/phpmyadmin
if wget -O pma.zip "$version_url"; then
unzip pma.zip && rm $_
mkdir $new && mv phpMyAdmin-$install_version_nb-all-languages/* $new
cp -R www/config.inc.php $new/
mv www $old && mv $new www
chowwwn . && cmf644 . && cmd755 .
# Check if everything works, then delete the old version
section "PHPMyAdmin upgraded successfully"
ask_deleteold
if [[ "$deleteold" =~ ^[Yy]$ ]]; then
rm -rf $old phpMyAdmin-$install_version-all-languages/
fi
fi
fi
exit 0
}

BASH - Safe check before running "rm -fr $FOLDER"

I have a script which really needs an rm -fr on a specific folder
I'd like to make this as safe as possible. I started this script below but I was wondering if there's anything else I missed.
folder=""
if [[ ! -d "$folder" ]]; then
echo "Error: is not a folder"
elif [[ "$folder" == "/" ]]; then
echo "Error: folder points to root"
elif [[ "$folder" == "../"* ]]; then
echo "Error: folder start with ../"
elif [[ "$folder" == *"/.."* ]]; then
echo "Error: folder contains /.."
elif [[ "$folder" == *"/*"* ]]; then
echo "Error: folder ends with /*"
else
rm -fr "$folder"
fi
Update: added the check for "/"
If you want to be as safe as possible, you could perhaps...
Make sure any globbing is done first :
shopt -s nullglob
declare -a folders=(folder_or_glob)
Iterate over each element of the array, one at a time, and operate on the canonical path.
for f in "${folders[#]-}"
do
[[ $f ]] || continue
candidate="$(realpath -e -s "$f")" || continue
ok_to_delete "$candidate" || continue
rm -rf "$candidate"
done
Use function ok_to_delete to test :
ok_to_delete()
{
[[ -d $1 ]] || continue # Is directory
[[ $1 != / ]] || continue # Not root
[[ "${1%/*}" ]] || continue # At least two levels deep
(... add any test you want ...)
}
There is a bit of redundancy here (e.g. not root + 2 levels deep), but this is just to give you ideas.
Instead of checking the path name of folder, I would rather to check the contents in that folder, file's size/user/timestamp/keywork/extension, etc, or whatever you care most about. This is a more safe method for you, this's just my two cents.

Why am I getting unbound variable i bash?

I have the following script which works for the most part till it hits a specific line:
#!/usr/bin/env bash
set -eux
# Go Home.
cd /vagrant/Freya/
CLEANED_ASSETS=false
## Clean up time!
## Remove all vendor and composer.lock folders - just because.
for f in *; do
if [[ -d $f ]]; then
if [[ $f != ".git" ]] && [[ $f != "bin" ]] && [[ $f != "docs" ]]; then
if [[ $f == "Loader" ]] && [[ $CLEANED_ASSETS == false ]]; then
cd "$f/"
if [[ -d "Assets" ]]; then
cd Assets/
rm -rf vendor composer.lock docs
let $CLEANED_ASSETS=true
cd ../../
fi
fi
cd "$f/"
rm -rf vendor composer.lock docs
cd ../
fi
fi
done
The issue is when it hits let $CLEANED_ASSETS=true I am not sure the proper way to set this variable to true, so it never enters this loop again. I keep getting:
+ let false=true
bin/clean-directories: line 21: true: unbound variable
CLEANED_ASSETS=true
No let, no $.
In particular, the let causes true to be treated as a variable name (searched for a numeric value), and referring to variable names that don't exist gets you flagged by set -u.

How To Move Files Into Directories based on filename and Count Files

I have seen other topics that are somewhat similar to my question, however I am still a beginner and cannot really understand some of the code that is posted.
I have a list of directories that follow either one of the formats, however the numbers at the end are not the same and the abbreviation (UVM) can be from a list of abbreviations.
jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0/
jhu-usc.edu_UVM.HumanMethylation450.aux.1.0.0/
jhu-usc.edu_UVM.HumanMethylation450.mage-tab.1.0.0/
I am hoping to make a script to move the directories and their files recursively from the current directory to a new directory (and create the directory if not already there) based off of the abbreviation (ex.UVM).
Then I want to be able count the number of files ending in *idat in the directories, and output a .txt file that says " For abbreviation there are this many idat files.
I haven't had much time to vest in figuring this out as of late and my deadline is coming up very soon. If anyone could help me out with this I would greatly appreciate it.
Please forgive me if the question is worded or formatted incorrectly, this is my first post so I tried my best.
Thank you!
Something like this ?
#!/bin/sh
#Definition of where you want the directory to be moved
destination_root="/tmp/stuff"
function abbreviation() {
#Default
destination="${destination_root}/UNKNOWN"
if [[ $1 == UVM* ]]; then
destination="${destination_root}/UVMFOLDER"
fi
if [[ $1 == BRCA* ]]; then
destination="${destination_root}/BRCAFOLDER"
fi
#Ensure folder is present
mkdir -p ${destination}
}
#Loop through all folders matching the prefix
for instance in jhu-usc.edu_*
do
#Count instances of *idat files and put the result in a.txt file
echo "${instance} - `find ${instance} -type f -name "*idat" | wc -l`" | tee -a a.txt
abbreviation `echo "${instance}" | sed s/jhu-usc.edu_//g`
#Move the folder to the destination
mv ${instance} ${destination}
done
This will create a.txt with content like:
jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0 - 3
jhu-usc.edu_UVM.HumanMethylation450.aux.1.0.0 - 2
Where 3 and 2 is the number of instances of files ending with idat in the folder.
Edit
Changed the output folder where it moves the files - i removed the prefix jhu-usc.edu_. So for example jhu-usc.edu_UVM.HumanMethylation450.Level_1.1.0.0 will be moved to /tmp/stuff/UVM.HumanMethylation450.Level_1.1.0.0
#!/bin/bash
#Definition of where you want the directory to be moved
destination_root="/data/nrnb01_nobackup/agross/TCGA_methylation"
#Delete tar.gz files
find . -type f -name '*tar.gz' -exec rm {} +
#Move all files within cancersubtype folders into current directory to allow ease of moving to specific directories
find . -mindepth 2 -type f -print -exec mv {} . \;
function abbreviation() {
#Default
destination="${destination_root}/UNKNOWN"
if [[ $1 == UVM* ]]; then
destination="${destination_root}/UVMFOLDER"
fi
if [[ $1 == BRCA* ]]; then
destination="${destination_root}/BRCAFOLDER"
fi
if [[ $1 == ACC* ]]; then
destination="${destination_root}/ACCFOLDER"
fi
if [[ $1 == BLCA* ]]; then
destination="${destination_root}/BLCAFOLDER"
fi
if [[ $1 == CESC* ]]; then
destination="${destination_root}/CESCFOLDER"
fi
if [[ $1 == CHOL* ]]; then
destination="${destination_root}/CHOLFOLDER"
fi
if [[ $1 == COAD* ]]; then
destination="${destination_root}/COADFOLDER"
fi
if [[ $1 == DLBC* ]]; then
destination="${destination_root}/DLBCFOLDER"
fi
if [[ $1 == ESCA* ]]; then
destination="${destination_root}/ESCAFOLDER"
fi
if [[ $1 == GBM* ]]; then
destination="${destination_root}/GBMFOLDER"
fi
if [[ $1 == HNSC* ]]; then
destination="${destination_root}/HNSCFOLDER"
fi
if [[ $1 == KICH* ]]; then
destination="${destination_root}/KICHFOLDER"
fi
if [[ $1 == KIRC* ]]; then
destination="${destination_root}/KIRCFOLDER"
fi
if [[ $1 == KIRP* ]]; then
destination="${destination_root}/KIRPFOLDER"
fi
if [[ $1 == LAML* ]]; then
destination="${destination_root}/LAMLFOLDER"
fi
if [[ $1 == LGG* ]]; then
destination="${destination_root}/LGGFOLDER"
fi
if [[ $1 == LIHC* ]]; then
destination="${destination_root}/LIHCFOLDER"
fi
if [[ $1 == LUAD* ]]; then
destination="${destination_root}/LUADFOLDER"
fi
if [[ $1 == LUSC* ]]; then
destination="${destination_root}/LUSCFOLDER"
fi
if [[ $1 == MESO* ]]; then
destination="${destination_root}/MESOFOLDER"
fi
if [[ $1 == OV* ]]; then
destination="${destination_root}/OVFOLDER"
fi
if [[ $1 == PAAD* ]]; then
destination="${destination_root}/PAADFOLDER"
fi
if [[ $1 == PCPG* ]]; then
destination="${destination_root}/PCPGFOLDER"
fi
if [[ $1 == PRAD* ]]; then
destination="${destination_root}/PRADFOLDER"
fi
if [[ $1 == READ* ]]; then
destination="${destination_root}/READFOLDER"
fi
if [[ $1 == SARC* ]]; then
destination="${destination_root}/SARCFOLDER"
fi
if [[ $1 == SKCM* ]]; then
destination="${destination_root}/SKCMFOLDER"
fi
if [[ $1 == STAD* ]]; then
destination="${destination_root}/STADFOLDER"
fi
if [[ $1 == TGCT* ]]; then
destination="${destination_root}/TGCTFOLDER"
fi
if [[ $1 == THCA* ]]; then
destination="${destination_root}/THCAFOLDER"
fi
if [[ $1 == THYM* ]]; then
destination="${destination_root}/THYMFOLDER"
fi
if [[ $1 == UCEC* ]]; then
destination="${destination_root}/UCECFOLDER"
fi
if [[ $1 == UCS* ]]; then
destination="${destination_root}/UCSFOLDER"
fi
#Ensure folder is present
mkdir -p ${destination}
}
#Loop through all folders matching the jhu prefix
for instance in jhu-usc.edu_*
do
#Count instances of *idat files and put the result in idat_count.txt file
echo "${instance} - `find ${instance} -type f -name "*idat" | wc -l`" | tee -a idat_count.txt
abbreviation `echo "${instance}" | sed s/jhu-usc.edu_//g`
#Move the folder to the destination
mv ${instance} ${destination}
done

Resources