The lab session of Command line arguments & inputs is giving an error

Hi I am sharing with you an issue i am facing in ‘Command line arguments & inputs’ of the Shell scripts for beginners lab… regarding question no 4

I am sharing the link :
https://learn.kodekloud.com/user/courses/shell-scripts-for-beginners/module/2709b373-3a6f-4b31-9aff-fe8a553898fa/lesson/e592522a-7006-4635-a823-40ac75d93225

as per the question any file should be backed up which I was able to do but the check says an error occured & the correct answer check is not done. It is saying I couldn’t complete one task. please check.

So, show us your work :slight_smile: What’s in the modified script file before you do “Submit”?

please put it in a code block

like this!

Also, I did the check if the solution has been accepted… it has been accepted which is strange as running the file backup-file.sh is giving an error in the terminal

# This script creates a backup of a given file by creating a copy as bkp
# For example some-file is backed up as some-file_bkp
set -e

file_name=$1

cp $file_name ${file_name}_bkp

echo "Done"

I am sharing you the contents of the backup-file.sh & i followed the solution way changed the file-name to $1

now while executing the backup-file.sh i am getting

bob@caleston-lp10:~$ ./backup-file.sh 
cp: missing destination file operand after '_bkp'
Try 'cp --help' for more information.

Since you’re not adding an argument to the script, the shell (which is just doing text substition) created this line:

cp _bkp

Since cp needs at least 2 arguments, this causes the error in “cp” that you see.

In a more realistic script, you’d prevent this by adding a check before calling cp:

if [ -z "$1" ]; then 
  echo "$0 must take an argument" >&2
  exit 1
fi

file_name=$1
cp $file_name ${file_name}_bkp

echo "Done"
1 Like