Help with - Build a Build Script


#1

Hello everyone,

I’m trying do finish this following challenge: ** Copy secretinfo.md but replace “42” with “XX”.*

from the last Command Line project: Build a Build Script

https://www.codecademy.com/paths/web-development/tracks/learn-the-command-line/modules/bash-scripting/projects/bash-scripting-p

As you can see on my code, I was able to leave the file secretinfo.md untouched form its original directory “source/”, however when I use the following commands: sed ‘s/42/XX/’ build/secretinfo.md > build/secretino , to override the file it won’t work. The file appears in blank. Whenever I use " >> " to append the “XX” it works but it won’t override.

Any ideas why ?

#!//bin/bash
echo "========================================"
echo "	Welcome to my first build.  "
echo "		-by Marlon-"
echo "========================================"
firstline=$(head -n 1 source/changelog.md)
read -a splitfirstline <<< $firstline
echo "The version you are building is "${splitfirstline[1]}
echo "========================================"  

echo
echo "Would you like to continue building? [Enter "1" to continue, "0" to make changes to the version]"
read versioncontinue

echo
if [ $versioncontinue -eq 1 ] 
then	
  for filename in source/*
  do    
    if [ "$filename" == "source/secretinfo.md" ]
    then
    	echo "Encrypting" $filename      
      cp $filename build
      
      for singleFile in build/*
      do
      	if [ "$singleFile" == "build/secretinfo.md" ]
      	then
        	sed 's/42/XX/' $singleFile > $singleFile
        fi
      done      
      
    else
    	echo "Copying" $filename
      cp $filename build
    fi
  done
  
  echo
  cd build/
  cd ..
  echo "=============================="  
	echo "Build version ${splitfirstline[1]} contains:"
  ls build
  
else
	echo "Plase come back when you're ready."
fi


#2

It’s “Please” not “Plase”

(I don’t do command line, this is just a note)


#3

Took some research, but I found this: https://unix.stackexchange.com/questions/283407/can-sed-save-its-output-to-a-file

sed 's/42/XX/' $singleFile > $singleFile

Change to:

sed -i 's/42/XX/' $singleFile

Read about the -i option in the first solution. Hopefully this will help. It worked for me.


#4

I’m cracking up! hahahaha :rofl:


#5

Dude!! It worked! Pretty cool. Thank you for your help.

I knew about -i when we use “grep -i ‘FindWord’ filename.txt” when we need to enable the command to be case insensitive. Never thought it could be use for that case! I think it makes sense? Since we want to find a number “42” and substitute for another word.

Thanks again!! :pray::+1::clap: