0

I have simplified the problem by removing domain-specific details to be easier to be reproduced.

I have a file called items that has the following content:

itemA
itemB

And two other files called itemA.settings and itemB.settings that have the following content:

itemA.settings content

export title="Title itemA"

itemB.settings content:

export title="Title itemB"

I have created a script(configure-all-items.sh) that reads the content from items file and loops through each element like below:

#!/bin/bash

$itemListDIR="../path"

itemList=$(cat $itemListDIR/items);

for(item in itemList)
do
    echo "Configuring item: $item"
    prepare-item.sh $item
done

prepare-item.sh uses the title to create a file for each item with the title as filename. The content of the prepare-item.sh is like below:

#!/bin/bash
$item="$1"
source "$item.settings"
echo "Item has title: $title" > title.txt

When executing the configure-all-items.sh script two files are created:

Title itemA.txt that the content:

Item has title:

Title itemB.txt that has the content:

Item has title: Title itemB

Also, I get this error when itemA.settings is being sourced

No such file or directoryline xx: ./../../../itemA

Rando Shtishi
  • 1,222
  • 1
  • 21
  • 31
  • 3
    Check your script for CRLF line breaks, fix with `dos2unix` – Barmar Sep 30 '22 at 15:53
  • `for(item in itemList)` is invalid syntax. This can't be your real code. – Charles Duffy Sep 30 '22 at 15:58
  • ...also, `$item=$1` isn't a valid assignment -- it doesn't change `$item` at all; it would need to be `item=$1`, without the leading `$`. – Charles Duffy Sep 30 '22 at 15:58
  • (and `for item in $itemList`, while more correct, is _still_ bad practice; it would do things like replace a `*` in the list with a list of filenames in the current directory -- proper practice would be something like `readarray -t itemList <"$itemListDir/items"` and then `for item in "${itemList[@]}"; do`) – Charles Duffy Sep 30 '22 at 15:59
  • 1
    (but yes, stray carriage returns caused by your file being saved as a DOS/Windows text file instead of as a UNIX one are the obvious immediate problem, in addition to everything else) – Charles Duffy Sep 30 '22 at 16:01
  • Thank you all for your help. The bash script had the correct style ending the problem was I forgot to check the style ending in the items file and that was causing this problem. After fixing the style ending in the items file, everything is working as it should. – Rando Shtishi Oct 03 '22 at 08:43

0 Answers0