I've been battling with something in a personal bash script for a while now and I'm not having much luck using the suggestions I've found online. I realise this question looks virtually identical to so many others out there already, but even looking through all of those, I'm still not having any joy..
I think the best way to do this is to list what I currently have and work from there.
So, the relevant parts of my script are:
#!/bin/bash
SOURCE_FILE="$1"
cat "$SOURCE_FILE" | while read FOLDER
do
...
FOLDER_RULE=$(echo ${FOLDER} | grep -oP '\[.*\]')
FOLDER_NAME=$(echo ${FOLDER} | grep -oP '.+(?=\[.*\]')
# these output absolutely nothing:
echo FOLDER_RULE is "$FOLDER_RULE"
echo FOLDER_NAME is "$FOLDER_NAME"
...
done
The $SOURCE_FILE
reference points to a simple text file with a list of strings that I pass to the script at runtime. Some dummy data for this file would be:
Folder One [ALL]
Folder Two [LATEST]
Folder Three [FIRST]
Folder Four
I can echo out the contents of $FOLDER
in my script without any issue and these two $(echo ... | grep ...)
lines work perfectly when I run them directly in terminal, but inside my bash file I get nothing.
I've tried structuring the grep
command in different ways, referencing the variables as ${...}
and "$..."
, even simply echo
ing the output of the command I'm trying to catch the output of, but I still get nothing.
Pretty sure it's something simple that I'm missing, but none of the searching I've done has helped me work it out. Please could someone help me understand what I'm doing wrong here?