1

Problem Statement Find files using bash script In this section, you are going to write a script named findJane.sh within the scripts directory.

This script should catch all "jane" lines and store them in another text file called oldFiles.txt. You will complete the script using the command we practiced in earlier sections. Don't worry, we'll guide you throughout the whole process.

Navigate to /scripts directory and create a new file named findJane.sh.

Mycode


#!/bin/bash


>oldFiles.txt

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

for i in $files:do

  do if test -e ~/data/"$i"; then 
     echo "$i" >> OldFiles.txt; 
  else 
     echo "File doesn't exist"; fi
done

output now file does not exist file does not exist file does not exist it should not print nothing and cat oldFiles.txt should return all those files with name 'jane

Where i am coding wrong

Guide Create the text file oldFiles.txt and make sure it's empty. This oldFiles.txt file should save files with username "jane".

Now, search for all lines that contain the name "jane" and save the file names into a variable. Let's call this variable files, we will refer to it with that name later in the lab.

Since none of the files present in the file list.txt are available in the file system, check if file names present in files variable are actually present in the file system. To do this, we'll use the test command that we practiced in the previous section.

Now, iterate over the files variable and add a test expression within the loop. If the item within the files variable passes the test, add/append it to the file oldFiles.txt.

8
  • 2
    For starters, you need to enclose the command that sets files, so files=$(...). Then, use the-l or --files-with-matches option to just get filenames. Even with that, if a filename has a space in it, the for loop will split the filename. (Person who put that space in the filename should be charged with conduct unbecoming of a human being.) Commented Jun 1, 2020 at 14:40
  • @Jack just a newbee please elaborate,your sarcasm is quite strong :).Looking for a clear answer. Commented Jun 1, 2020 at 15:04
  • Try fixing the files=$(grep...) line first. Commented Jun 1, 2020 at 15:33
  • you're on the right track. Fix the syntax errors uncomvered at shellcheck.net . Be sure to use #!/bin/bash as the first line when you post there (you're missing a # char). If fixing the syntax errors doesn't solve your problem, then edit your Q with updated code so people aren't wasting time commenting on easy fixes. Include exact text of any error messages generated. Good luck. Commented Jun 1, 2020 at 15:36
  • 1
    As I said, you don't need all that | cut ... stuff. Just use the -l option on grep. Commented Jun 1, 2020 at 17:21

8 Answers 8

5

I have the same problem, just don't call the files by $in grep

#!/bin/bash

files= grep ' jane ' ../data/list.txt | cut -d ' ' -f 3

for file in files; do

   if  test -e ~/data/$file; then
     echo  $file >> oldFiles.txt;
   else
     echo "File dosen't exsist"; fi
done

And make the new fie .oldFiles.txt in the command line

like user@linux: ~/program$ ./findJane.sh > oldFiles.txt

The code shall work

Sign up to request clarification or add additional context in comments.

Comments

3

If you're trying to do a Qwicklabs assignment, try this

#!/bin/bash

> oldFiles.txt

files=$(grep " jane " /home/<Student-id>/data/list.txt | cut -d ' ' -f 3)

for i in $files;do

if test -e ~/$i;then

echo $i>>oldFiles.txt;

else echo "not working"; fi

done

1 Comment

Your question is not clear. What are you trying to achieve? What errors/problems are you encountering? Please read this for help on asking a good question: How to Ask
2
#!/bin/bash

>oldFiles.txt

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

for i in $files; do

  if test -e ~/$i; then
    echo $i >> OldFiles.txt; 
  else 
    echo "File doesn't exist"; fi
done

Comments

0

you have just some typos. this is "working" code for your example

#!/bin/bash

>oldFiles.txt

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

for i in $files; do

  if test -e ~/data/"$i"; then
    echo "$i" >> OldFiles.txt; 
  else 
    echo "File doesn't exist"; fi
done

however, this works only if list.txt doesn't contain any spaces except as delimiters between (at least) three columns

  • it won't work for Jane because it doesn't match jane
  • it won't work when there are more spaces like jane example.doc
  • it won't work for file names containing spaces like best movies.txt

it makes less sense to show how to do it right because the concept of list.txt is bad choice

Comments

0
#!/bin/bash

>oldFiles.txt

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

for file in $files; do 
  if [ -e $HOME$file ] ; then 
    echo $HOME$file >> oldFiles.txt; 
  fi 
done

Comments

0
#!/bin/bash
> oldFiles.txt
files=$(grep ' jane ' ../data/list.txt | cut -d ' ' -f 3)
#echo $files
for file in $files; do
        if  test -e ~/$file; then
                echo "File path added !!"
                echo  $file >> oldFiles.txt;
   fi
done

1 Comment

Please correct your markdown and give some details.
0

You need to check the path and then append it to the oldFile

#1/bin/bash
>oldFiles.txt
files=$(grep "jane " ../data/list.txt | cut -d' ' -f3)
for f in $files; do
 if [ -e $HOME$f ];then
    echo $HOME$f >> oldFiles.txt;
 fi
 done

Comments

0

I made a few fixes to your code and now works 😉

#!/bin/bash

> oldFiles.txt

files=$(grep " jane " ../data/list.txt | cut -d " " -f 3)
for file in $files; do
if test -e ~/$file; then echo $file  >> oldFiles.txt; else echo "File doesn't exist"; fi done

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.