0

Good afternoon, I'm trying to figure out why my variable loop is only using the last line in my do loop to write data my ISLAND loop is finding city names and assigning a static variable (ISLAND-X-XO)

IFS=$'\n'
OUTPUTDIR='/home/IMPACT_COUNT'
for CSR in `ls -1 configs/ | egrep 'router{1-5]'`
do

ISLAND=$(echo $CSR | grep -q "tampa" && echo ISLAND-TAM-MCR-XO) <- does not write
ISLAND=$(echo $CSR | egrep -q "washington-dc|ashburn" && echo ISLAND-WDC-XO) <- writes
COUNT=$(grep "up             up"  /otherconfigs/$CSR | wc -l)
echo $CSR $COUNT >> $OUTPUTDIR/$ISLAND
done

Each one of the "ISLAND" lookups works individually, just not stacked.

Tried [if then] options Expecting multiple lookups in the loop to write to the DIR

2
  • 3
    To increase the code quality: add a suitable shebang (#!/bin/bash) to your script, then paste it at shellcheck.net and try to implement the recommendations made there. Commented Mar 14, 2023 at 22:48
  • 2
    Do what @Cyrus suggested and do not do what was suggested in the 1 answer you have so far - shellcheck would also tell you some (but not all) of the issues with that. If you edit your question to contain a minimal reproducible example with your code after fixing the issues shellcheck tells you about and concise, testable sample input and expected output then we can help you do what you're trying to do the right way. Commented Mar 14, 2023 at 23:05

1 Answer 1

-1

Here:

ISLAND=$(echo $CSR | grep -q "tampa" && echo ISLAND-TAM-MCR-XO) <- does not write
ISLAND=$(echo $CSR | egrep -q "washington-dc|ashburn" && echo ISLAND-WDC-XO) <- writes

You are overwriting the variable in the second line. Try something like this:

if echo $CSR | grep -q "tampa"; then
    ISLAND=ISLAND-TAM-MCR-XO
elif echo $CSR | egrep -q "washington-dc|ashburn"; then
    ISLAND=ISLAND-WDC-XO
fi
Sign up to request clarification or add additional context in comments.

1 Comment

bash has everything built in to find a string in a variable. I suggest to use this syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.