0

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 echoing 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?

closed as off-topic by Jeff Schaller, Stephen Rauch, Anthony Geoghegan, Kusalananda, Rui F Ribeiro Sep 3 '17 at 19:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, Stephen Rauch, Anthony Geoghegan, Kusalananda, Rui F Ribeiro
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It is unclear what the problem is. You can't set the variable within thu while loop, or they don't retain their values after the loop (which is due to the loop running in a subshell)? – Kusalananda Aug 29 '17 at 7:36
  • Run the script with bash -x to enable debugging. You should get feedback from each line of the script then. – Raman Sailopal Aug 29 '17 at 8:32
  • @Kusalananda, for what it's worth, I'm actually trying to use the variables inside the loop; their values will change on each iteration. Perhaps I should try declare them before the loop and simply assign a value in the loop anyway, just to see what happens... – ZaLiTHkA Aug 29 '17 at 15:16
  • @RamanSailopal, I'll do that in a moment, first I want to read through the other question that steeldriver asked about. – ZaLiTHkA Aug 29 '17 at 15:17
0

So, after all that, it turns out the problem was with my regular expressions. Scraping the lines that do contain [something] at the end assigns values to the variables as expected, while the lines that don't have that particular string at the end, leave both variables blank. At least the regex side I can fix though.

My apologies for the silly question, and a big thanks to the commenters for trying to help. I do appreciate your time. :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.