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?

  • 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 07: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 08: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... –  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. –  Aug 29 '17 at 15:17
  • @ZaLiTHkA If you want to set and use the variable inside the while loop, then you are not experiencing that issue. If you try to use the variables outside the loop, you will fail. – Kusalananda Aug 29 '17 at 15:21
  • @steeldriver, thanks for the link, but I don't think that applies to my issue: "variables defined in the while loop ... have their own local scope context", while I'm trying to define and use them in the same iteration of the loop. –  Aug 29 '17 at 15:27

1 Answers1

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. :)