user200913
user200913

Reputation: 13

Bash Script Variable

#!/bin/bash

RESULT=$(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)

for i in $(RESULT);
do
echo "$i"
FILENAME="$(dirname $RESULT)"
done

I have a problem with the line FILENAME="$(dirname $RESULT)". Running the script in debugging mode(bash -x script-name), the ouput is:

test.sh: line 9: RESULT: command not found

For some reason, it can't take the result of the variable RESULT and save the output of dir command to the new variable FILENAME. I can't understand why this happens.

After lots of tries, I found the solution to save full path of finame and finame to two different variables. Now, I want for each finame, find non-case sensitive of each filename. For example, looking for file image.png, it doesn't matter if the file is image.PNG

I am running the script

while read -r name; do
  echo "$name"
  FILENAME="$(dirname $name)"
  BASENAME="$(basename $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)

and then enter the command:

find . $FILENAME -iname $BASENAME

but it says command FILENAME and BASENAME not found.

Upvotes: 1

Views: 1080

Answers (2)

Dylan
Dylan

Reputation: 16

for i in $(RESULT) isn't right.You can use $RESULT or ${RESULT}

Upvotes: 0

devnull
devnull

Reputation: 123458

The syntax:

$(RESULT)

denotes command substitution. Saying so would attempt to run the command RESULT.

In order to substitute the result of the variable RESULT, say:

${RESULT}

instead.


Moreover, if the command returns more than one line of output this approach wouldn't work.

Instead say:

while read -r name; do
  echo "$name"
  FILENAME="$(dirname $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)

The <(command) syntax is referred to as Process Substitution.

Upvotes: 2

Related Questions