Command line Build a Build script help

Hello I am having issue with the Build a Build Script for some reason it is not copying anything from the source directory to the build directory like the exercise suggested.

Link to exercise:
https://www.codecademy.com/courses/learn-the-command-line/projects/bash-scripting-p

Even after using the code suggested in the exercise it was having issues:

#!/bin/bash

echo "What is your name?"
read name
echo "Hello $name"

firstline=$(head -n 1 source/changelog.md)
read -a splitfirstline <<< $firstline
version=${splitfirstline[1]}
echo "You're building version" $version
echo "Enter 1 for yes to continue and 0 for no to exit"
read versioncontinue
if [ $versioncontinue -eq 1 ]
then
  echo "OK"
  for filename in source/*
  do
    echo $filename
  done
  if [ "$filename" == "source/secretinfo.md" ]
  then
    echo "Not copying" $filename
  else
    echo "Copying" $filename
    cp $filename build/.
  fi
else
  echo "Please come back when you are ready"
fi  

cd build/

cd ..

echo "Build version $version contains:"
ls
1 Like

Hey, welcome to the forums!

The issue is with your for loop… which you’re only using to print out the filenames to the terminal.

By the time you get to your if statement, to make sure we don’t copy our secretinfo.md file, your loop has finished and $filename is fixed at it’s final value of source/secretinfo.md.

So, your script is only trying to move one file - and it’s the one you’re explicitly telling it to ignore.

Can you see how you need to change your script to fix this? :slight_smile:

Okay so are the instructions for the build a build script wrong? I am following it exactly as they stated. Is the issue that my for loop should be after the if statement with the $filename?

Thank you your clue was good enough. I didn’t have my if statement in the right for loop.

#!/bin/bash

echo "What is your name?"
read name
echo "Hello $name"

firstline=$(head -n 1 source/changelog.md)
read -a splitfirstline <<< $firstline
version=${splitfirstline[1]}
echo "You're building version" $version
echo "Enter 1 for yes to continue and 0 for no to exit"
read versioncontinue
if [ $versioncontinue -eq 1 ]
then
  echo "OK"
    for filename in source/*
    do
      echo $filename
      if [ "$filename" == "source/secretinfo.md" ]
      then
        echo "Not copying" $filename
      else
        echo "Copying" $filename
        cp $filename build/.
      fi
    done

else
  echo "Please come back when you are ready"
fi  

cd build/

cd ..

echo "Build version $version contains:"
ls
1 Like