I have recently written some code in Bash where I'm starting to program some things and I think I'm overcomplicating the script I wrote.
I'd like to make it simpler so that in future I would avoid these mistakes.
I've posted this on Stack Overflow and one user did reply that I can "simplify" multi-echoing:
just use 1 echo command, open with a double quote on the first line and close the quote after the last one
Here's the code:
#!/bin/bash
# Look for path when the skript is
SCRIPT_PATH=$(readlink -f "$0")
SEARCH=$(dirname "$SCRIPT_PATH")"/"
REPLACE="../../"
echo
echo \ \ START - Replacing Absolute path to Relative
echo \ \ =================================================================
echo \ \ Info: Automatic replacement of the \>absolute\< path
echo \ \ to \>relative\< for the Pedestrian Tool positioning result files
echo \ \ =================================================================
echo
echo \ \ "Searching for >> $SEARCH"
echo \ \ "Replacing by >> $REPLACE"
echo
echo \ \ "Found in files:"
echo \ \ =================
echo
# grep - filter files where inside is scripts path name and write them down
grep -rl "${SEARCH}" --include \*.pc ./
# replace them with sed ../../
grep -rl "${SEARCH}" --include \*.pc ./ | xargs sed -i "s#${SEARCH}#${REPLACE}#g"
echo
echo \ \ END - Replacing finished...
echo \ \ =================================================================
echo \ \ =================================================================
echo
echo
echo \ \ START - Renaming Folders by Y-Coordinate of Impactor
echo \ \ =================================================================
echo \ \ Info: Script will look into RESULTS Folder, than find directories
echo \ \ then find files in these directories and from
echo \ \ file named POSITIONING extcarc Y-Coordinate of the Impactor
echo \ \ This value is then used to rename by the: MODEL_Y\$Y-Coord
echo \ \ =================================================================
echo
# $SEARCH=path where the script is + /RESULTS/ where the results are
adresar=$SEARCH"RESULTS/"
directories=$(find $adresar*/ -type d)
echo
echo "Folders in dir: $adresar:"
echo
for d in $directories
do
echo \ "Operations in folder: $d"
files=$(find $d -type f )
for f in $files; do
fileExtension=${f##*.}
fullPathFileName=${f%.*}
fileName=$(basename "$fullPathFileName")
# if Positioning_<#>.pc file
# find line that has Target POint Coordinates string
# example: # Target Point Coordinates : -934.541, -100.000, 247.510
# and extract Y-coordinate without float, that is: "-100"
# can be 600 (without minus)
if [[ $fileExtension == pc && $fileName == Positioning_* ]]; then
line=$(grep "Target Point Coordinates" "$f")
# -o (only matching) -E (extended-regexp)
# [^.](without dot)[0-9]+(variable number of numbers)
STRING=$(echo $line | grep -o -E '[^.][0-9]+')
N=3
yCoord=$(echo $STRING | awk -v N=$N '{print $N}')
fi
done
folderName=$(basename "$d" | tr 'a-z' 'A-Z')
if [[ $folderName != REPORT ]]; then
echo \ \ "Dir $d renamed by Y-Coordinates of impactor"
newFolderName=MODEL_Y$yCoord
cd $d; cd ..
mv $(basename "$d") $newFolderName
fi
echo
done
echo
echo \ \ END - Renaming complete...
echo \ \ =================================================================
echo \ \ =================================================================
echo
echo
echo \ \ START - Renaming .DSY, .msg, .THP files by folder name
echo \ \ =================================================================
echo \ \ Info: Renames .DSY, .msg, .THP files in folder RESULTS
echo \ \ by the name of parrent folder \(that is named by impactor\'s
echo \ \ Y-Coordinate \(\>\> MODEL_Y300\)
echo \ \ =================================================================
echo
directories=$(find $adresar*/ -type d)
for d in $directories
do
echo \ "Working in folder: $d"
files=$(find $d -type f )
baseName=$(basename "$d")
for f in $files; do
cd $d
fileExtension=${f##*.}
fullPathFileName=${f%.*}
fileName=$(basename "$fullPathFileName")
Name=${baseName}.${fileExtension}
if [[ $fileExtension == 'pc' && $fileName != Positioning_* ]]; then
Name=${baseName}.${fileExtension}
mv "$f" "$Name"
echo \ \ "Was renamed to: $Name"
elif [ $fileExtension == 'DSY' ]; then
Name=${baseName}.${fileExtension}
mv "$f" "$Name"
echo \ \ "Was renamed to: $Name"
elif [ $fileExtension == 'THP' ]; then
Name=${baseName}.${fileExtension}
mv "$f" "$Name"
echo \ \ "Was renamed to: $Name"
elif [ $fileExtension == 'msg' ]; then
Name=${baseName}.${fileExtension}
mv "$f" "$Name"
echo \ \ "Was renamed to: $Name"
else
echo \ \ "X Wasn't renamed."
fi
echo \ "====="
done
echo
done
echo \ \ END - Renaming complete...
echo \ \ =================================================================
echo \ \ =================================================================
echo -e "\n"