I'm trying to make a script that will find all symlinks in a directory that the script is currently in, and it will echo "This 'link' refers to 'path' and it's owner is 'owner'.
I've come up with this solution so far, but it doesn't work for some reason.
Here's my code:
#!/bin/bash
for each in .
do
echo "Link '$1' refers to" $(realpath $1) "and its owner is: " $(ls -ld $(realpath $1) | awk '{print $3}')
end for
And this it the error that it gives me:
jakub.kacerek@perun:~$ ./najdi_symlink.sh
./najdi_symlink.sh: line 7: syntax error: unexpected end of file
jakub.kacerek@perun:~$
I'm 100% sure that the error is in the for loop, but I don't know how to solve it.
CodePudding user response:
Well after a quick thought I found out the solution for it, it may not be the smartest one but it gets the job done.
The solution is:
#!/bin/bash
for each in $(find . -maxdepth 1 -type l -ls | awk '{print $11}'); do
echo "Link '$each' refers to" $(realpath $each) "and its owner is: " $(ls -ld $(realpath $each) | awk '{print $3}')
done
CodePudding user response:
Once you figured out you can use find
, why do you keep using a for
-loop?
find . -type l -exec echo {} : $(realpath {}) \;
This is not complete but it shows what you can do:
- basic usage of
find
find
, combined with{}
(find
result)- You can launch commands (like
realpath
) on thefind
result.
Have fun :-)
CodePudding user response:
I liked the assignment. I've figured it out.
I'm trying to make a script that will find all symlinks in a directory that the script is currently in, and it will echo "This 'link' refers to 'path' and it's owner is 'owner'.
You might need to change the strings, but the actual code works.
#!/bin/bash
for item in * ; do
if [ ! -L $item ]
then
echo "filename: '$item' is NOT a symlink"
else
printf "filename: '$item' is a symlink " && echo $item | awk '{printf("%s ->", $1); system("readlink -f " $1)}' && printf 'owner is: ' ; stat -c '%U' $item
fi
done
Example output:
[vagrant@vm-local-1 ~]$ ./a.sh
filename: 'a.sh' is NOT a symlink
filename: 'hi.jar' is NOT a symlink
filename: 'ho' is a symlink ho ->/tmp/abc/ho
owner is: vagrant
filename: 'one' is NOT a symlink
filename: 'test.jar' is NOT a symlink
filename: 'test.tar.gz' is NOT a symlink
filename: 'two' is a symlink two ->/home/vagrant/one
owner is: vagrant
CodePudding user response:
Not an expert but dont u end a for loop with done? Not end for