I prepare a contest and I training for it.
The exercise is the following : I must write a shell script to find all the elements with a ".sh" extension, print them on the terminal with only the name of the files (without extension ".sh" and without the "./").
I know that I can write this "simple" script (I'm a very beginner to shell) :
#!/bin/sh
find . -type f -name "*.sh" | rev | cut -d'/' -f 1 | rev | cut -d '.' -f 1
But the fact is I'm not sure to uderstand what are all the stuff in this script, especially the "rev" command, which reverse some stuff, but I don't uderstand what exactly, even with a man rev. If you can explain it to me it's cool, I want to understand it to reuse it with knoledge, not only with "stupid" memory. :)
Also, I want to do the same job than this script, but with other "funtion" if the term is okay.
So I wrote this :
#!/bin/sh
filename= find . -type f -name "*.sh"
echo "$filename" | cut -d '/' -f 1 | basename "$filename" .sh
But unfortunatly this does'nt work. Can you explain me why and how can I fix the problem ?
I've the feeling that the find command is'nt write with the good approach.
This is the output I have when I run the script :
./print_groups.sh
./find_sh.sh
./dd.sh
./ee.sh
Ha ha.
Thank you very much to take time for a beginner. :)
EDIT :
With the help of Charles Duffy, I could wrote this stuff :
#!/bin/sh
filename=$(find . -type f -name "*.sh" -printf '%P\n')
name=$(basename -s .sh $filename)
echo "$name"
The output is :
print_groups
find_sh
dd
ee
SUPER-EDIT :
You can also use the sed command :
#!/bin/sh
filename=$(find . -type f -name "*.sh" -printf '%P\n' | sed -e 's/[.][^.]*$//')
echo "$filename"