Problem with Bash Scripting -> Build a Build Script Project

Hi guys, I got completely lost in this project: https://www.codecademy.com/paths/web-development/tracks/learn-the-command-line/modules/bash-scripting/projects/bash-scripting-p
I didn’t manage to copy the files to another directory, I don’t know where the issue is. Could you please guide me? Some hints don’t tell you where you should put some lines of code, and I was just improvising.

#!/bin/bash
echo "Welcome to the happy little world of darius maximus"
firstline=$(head -n 1 source/changelog.md) 
read -a splitfirstline <<< $firstline
version=${splitfirstline[1]}
echo "You are building version"
$version
echo "Do you want to continue? (enter "1" for yes, "0" for no)"
read versioncontinue
if [ $versioncontinue -eq 1 ]
then 
	echo "OK"
  cd build/
# We'll add more code here later
cd ..
  for filename in source/*
  do 
 		if ["$filename" == "source/secretinfo.md"]
  	then 
    		echo "Not copying" $filename
    else
    		echo "Copying" $filename
        cp $filename build/.
    fi
  echo $filename  
  done
else
	echo "Please come back when you are ready."
fi

echo "Build version $version contains:"
ls

and this is what I get after running it in Bash:

Copying source/foo1.html
source/foo1.html
./script.sh: line 18: [source/foo2.html: No su
ch file or directory
Copying source/foo2.html
source/foo2.html
./script.sh: line 18: [source/foo3.html: No su
ch file or directory
Copying source/foo3.html
source/foo3.html
./script.sh: line 18: [source/secretinfo.md: N
o such file or directory
Copying source/secretinfo.md
source/secretinfo.md
Build version 11.2.3 contains:
build  script.sh  source
$

I will be grateful for your help and replies! :slight_smile:

1 Like

Commands and arguments are separated by space, you’ve omitted the space and are therefore executing a different command than you intended. The command you meant to execute is [

You can run only that command to see how it behaves:

nate@scine → [ hello == hai ]
nate@scine 1 → [ hello == hello ]
nate@scine → 

The way I have my prompt set up, it hides the return value when it’s 0 (success) and otherwise shows it before →
If you don’t display the return value in your prompt you can check it by printing out $?

When the comparison failed (false), [ returned a value of 1 to indicate failure.
When I change one of the strings to match the other, the return value is instead 0.

2 Likes

Oh, hi, thx a lot for your reply! :wink: :slight_smile: guess it works after adding space in the if statement :slight_smile:
But there is one more thing that I don’t get:
Now my code looks like this, but Bash tells me that on line 7 the command is not found, do you have any idea why?:

#!/bin/bash
echo "Welcome"
firstline=$(head -n 1 source/changelog.md) 
read -a splitfirstline <<< $firstline
version=${splitfirstline[1]}
echo "You are building version"
$version
echo "Do you want to continue? (enter "1" for yes, "0" for no)"
read versioncontinue
if [ $versioncontinue -eq 1 ]
then 
	echo "OK"
  cd build/
# We'll add more code here later
cd ..
  for filename in source/*
  do 
 	if [ "$filename" == "source/secretinfo.md" ]
  	then 
    		echo "Not copying" $filename
    else
    		echo "Copying" $filename
        cp $filename build/.
    fi
  echo $filename  
  done
else
	echo "Please come back when you are ready."
fi
echo "Build version $version contains:"
ls

And I get this in my response:

$ ./script.sh
Welcome
You are building version
./script.sh: line 7: 11.2.3: **command not found**
Do you want to continue? (enter 1 for yes, 0 f
or no)
1
OK
Copying source/bar.js
source/bar.js
Copying source/buzz.css
source/buzz.css
Copying source/changelog.md
source/changelog.md
Copying source/foo1.html
source/foo1.html
Copying source/foo2.html
source/foo2.html
Copying source/foo3.html
source/foo3.html
source/foo2.html
Copying source/foo3.html
source/foo3.html
Not copying source/secretinfo.md
source/secretinfo.md
Build version 11.2.3 contains:
build  script.sh  source
$ cd build
$ ls
bar.js    changelog.md  foo2.html
buzz.css  foo1.html     foo3.html
1 Like

Well uhm. Have you read line 7?
Your error message says a whole lot too.

1 Like

Yeah, there is something wrong with $version, but I have no idea what. I followed every step from hints from the task. I don’t know what I should change.

wrong? What is it supposed to do? How would it need to be used to do that? Why is it there in the first place? If you remove it, what would be missing? What do you currently tell bash to do with it (read the error message)?

Seems to me like you already have the answers, if only you expand the word “wrong”

1 Like

Oh! Is it ‘echo’ command before version missing? thx, now it works fine. Nevertheless, now, another question came to my mind - where is the version number taken from? How does bash know that I am building version 11.2.3? I know there is the ‘read’ command, but I thought ‘read’ is used when the user writes smth…

1 Like

Sounds like you answered your own question, except that users aren’t the only ones that can write to a program’s stdin

1 Like

all right, thx for your guidance, I appreciate it a lot! Take care! :slight_smile:

1 Like

#!/bin/bash

echo “Hello my friend!”
firstline=$(head -n 1 source/changelog.md)
read -a splitfirstline <<< $firstline

version=${splitfirstline[1]}
echo “You are building version” $version

echo ‘Do you want to continue? (enter “1” for yes, “0” for no)’
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/
echo “Build version $version contains:”
ls
cd …

#!/bin/bash
echo “welcome mofo !!”
firstline=$( head -n 1 source/changelog.md )
echo $firstline
read -a splitfirstline <<< $firstline
echo "splitfirstline" version={spiltfirstline[1]}
echo “you are building version” $version
echo “continue? (1=yes, 0=no)”
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
cd build
echo "build version $version contains : "
ls

cd ..

else
echo “please come back when you are ready”
fi

in my code the version is showing only ## and not 11.2.3 like it should