0

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"
Batche
  • 65
  • 1
  • 8
  • Are you guaranteed the GNU version of find? (If so, it can print only the filenames itself, no need for other tools at all) – Charles Duffy Sep 08 '20 at 12:48
  • As for why your code doesn't work, though... Can just close this question as a duplicate of existing knowledge base entries about the individual mistakes. – Charles Duffy Sep 08 '20 at 12:49
  • ... To just print the filenames, though: `-printf '%P\n'` – Charles Duffy Sep 08 '20 at 12:50
  • Hello, if I write -printf '%P\n' the extension is still here. And I want to have only the name of the file, without the extension ! Thx for your answer anyway ! – Batche Sep 08 '20 at 12:57
  • With your help I could find a solution. Many thanks to you. ;) – Batche Sep 08 '20 at 13:51
  • Gotcha. That being the case, you _do_ need an extra pipeline component. Still no need for the `rev`, though; `sed -e 's/[.][^.]*$//'` and there you are. – Charles Duffy Sep 08 '20 at 13:51
  • Ok thank you ! I will try this one. I managed this problem with the basename command. The fact was I did'nt know how to write correctly a command in a variable... I'm a newby ^ – Batche Sep 08 '20 at 13:52
  • BTW, you had it right earlier with `echo "$value"` instead of `echo $value`. The quotes are important; see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo), and [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Sep 08 '20 at 14:02
  • Yes ! I just noticed that and I was going to edit my answer to correct it ! :) Indeed, with the quote for a value after echo no need to do some weird stuff to have a new line. And this avoids some problem as it is explain in your links. :)) – Batche Sep 08 '20 at 14:24

0 Answers0