0

Attempting to locate all lines in a list of files containing jane, then test if the result exists as a real file, and append their names to a Document. Here is my code:

#!/bin/bash

> oldFiles.txt

janeSearch=$( grep " jane " ../data/list.txt | cut -d ' ' -f 1,3 )    

for x in $janeSearch;
        if test -e ~/$x: then
                echo $x >> oldFiles.txt
        fi

Can someone explain why I get the following error?

syntax error near unexpected token `if'
6
  • 3
    Use ; instead of : Commented Dec 1, 2021 at 14:42
  • 4
    Also, you probably miss a do before the if. Commented Dec 1, 2021 at 14:42
  • Well, excuse my bad syntax, I am new to bash scripting! Commented Dec 1, 2021 at 14:43
  • 3
    Also a done after the fi to pair with the missing do. Commented Dec 1, 2021 at 14:44
  • shellcheck.net is a terrific resource for checking syntax errors in shell code. Add it to your development process. Commented Dec 1, 2021 at 19:13

1 Answer 1

1

Suggesting to try the following

#!/bin/bash

> oldFiles.txt

janeSearch=$( grep " jane " ../data/list.txt | cut -d ' ' -f 1,3 )    

for x in $janeSearch;
        if (test -e ~/$x); then
                echo $x >> oldFiles.txt
        fi
done
Sign up to request clarification or add additional context in comments.

Comments

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.