Command Line. Build a Build Script

Hello there!
in the Command Line project Build a Build Script
I’ve run onto misunderstanding over how the command ‘cp’ on ‘filename’ does actually work.

While iterating filenames in directory ‘source’ the command ‘echo “Not copying” $filename’ (line 17)’ then just informed user, by only printing in terminal, that the file ‘source/’ conditionally (line 14) won’t be copied to the ‘build’ directory.

The command ‘echo’, as i grasp it, is actually using just for printing purpose in terminal, so how the command ‘cp $filename’ (line 21) does work and why actually exclude the file ‘source/’ from a list of iterated files in this regard, since there any other actions on that loop are predefined?


Same puzzle! Hope someone can pop over to this post and help answer!

In the exercise you refer to, the aim is to create a bash script that selectively copies files from one folder (named source) to another folder (named build). In the specific case of the exercise, the idea is to exclude one special file (named from being copied.

To that end, we

  1. loop over all files in the source folder using
    for filename in source/* do

  2. we check for each file whether it is using
    if [ $filename == "source/" ]

  3. we decide what to do when the file is after

  4. we decide what to do when the file is not after

After points 3 and 4 you can put any code you want. You can use echo to inform the user if the file that is currently handled is You can also decide to put in the cp command. The echo and cp commands are in principle independent – the one command does not know about the other. So, what you do in the exercise is: you use echo to inform the bash script user about what is going to happen (namely, a file will be copied or not) and after that you take the corresponding copy action (or not). Does that clarify the situation?

Note that in a real-life situation, you may, of course, decide to ignore more files than just one when copying from a source to a build folder. The exercise just shows how you could in principle go about making a build script.

P.S. It would help if you indented your bash script a bit more consistently, so you can see which ifs, elses, fis, etc. belong together.

1 Like