Reputation: 119
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.
Upvotes: 1
Views: 9436
Reputation: 23
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 31
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
Upvotes: 3
Reputation: 1
#!/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
Upvotes: 0
Reputation: 11
#!/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
Upvotes: 0
Reputation: 53
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
Upvotes: 5
Reputation: 77
#!/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
Upvotes: 2
Reputation: 741
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
Jane
because it doesn't match jane
jane example.doc
best movies.txt
it makes less sense to show how to do it right because the concept of list.txt is bad choice
Upvotes: 0