Labs: Q2 "Conditional Logic"

Question is: Create a shell script in the home directory called check_dir.sh. The script should print the line Directory exists if the directory /home/bob/caleston exists. If not, it should print Directory not found

I tried the solution below, which is similar (matches) to the solution provided. But somehow this is not working. I’m unable to figure out the mistake (if any) that I’m doing:

if [ -d $HOME/caleston ]
then 
        echo "Directory caleston exists under $HOME"
else
        echo "Directory caleston does not exist under $HOME"
        fi

When I execute the script, this is the output I’m getting:

bob@caleston-lp10:~$ ./check_dir.sh
Directory caleston does not exist under /home/bob
bob@caleston-lp10:~$

But when “check” is used, its failing with the message below:

The script is not working if the directory does not exist

Please help.

Nothing wrong with the script you have pasted above…

bob@ubuntu:~$ cat check_dir.sh 
if [ -d $HOME/caleston ]
then 
        echo "Directory caleston exists under $HOME"
else
        echo "Directory caleston does not exist under $HOME"
        fi
bob@ubuntu:~$ ls -l
total 4
-rwxrwxr-x 1 bob bob 161 Apr 14 18:33 check_dir.sh
bob@ubuntu:~$ ./check_dir.sh 
Directory caleston does not exist under /home/bob
bob@ubuntu:~$ mkdir caleston
bob@ubuntu:~$ ./check_dir.sh 
Directory caleston exists under /home/bob
bob@ubuntu:~$ 

Thank you @Alistair_KodeKloud for checking and confirming. I am facing similar issue for few other lab questions as well. Guess, the “Check” functionality of the lab questions is checking for solutions implemented in a specific way. If I can make sure that the solution I’m trying is working as expected, is it safe to assume that I’m doing it right. Please suggest.

If you have some specific issues, please post the issue and the link to the lab for each one.

Q1: Develop a new script at /home/bob/launch-rockets.sh to call the create-and-launch-rocket script to launch 5 rockets for the following missions using a for loop.

Link to the same:

Solution I tried:

bob@caleston-lp10:~$ cat launch-rockets.sh
for mission in lunar-mission mars-mission jupiter-mission saturn-mission mercury-mission
do 
        ./create-and-launch-rocket $mission
done
bob@caleston-lp10:~$ 

bob@caleston-lp10:~$ cat launch-rockets.sh
for mission in lunar-mission mars-mission jupiter-mission saturn-mission mercury-mission
do 
        ./create-and-launch-rocket $mission
done
bob@caleston-lp10:~$

Out of the 2 checks:

Script runs as expected →

Script uses for loop → is successful

It doesn’t run as expected!

bob@caleston-lp10:~$ bash ./launch-rockets.sh 
./launch-rockets.sh: line 3: ./create-and-launch-rocket: Permission denied
./launch-rockets.sh: line 3: ./create-and-launch-rocket: Permission denied
./launch-rockets.sh: line 3: ./create-and-launch-rocket: Permission denied
./launch-rockets.sh: line 3: ./create-and-launch-rocket: Permission denied
./launch-rockets.sh: line 3: ./create-and-launch-rocket: Permission denied
bob@caleston-lp10:~$ ./launch-rockets.sh
-su: ./launch-rockets.sh: Permission denied

If you look at the permissions of these scripts, they do not have the x (execute) permission so they cannot be run directly.

bob@caleston-lp10:~$ ls -l
total 12
-rw-r--r-- 1 bob bob  383 Apr 15 18:45 create-and-launch-rocket
-rw-rw-r-- 1 bob bob  142 Apr 15 18:46 launch-rockets.sh

There are 2 ways to fix this.

  1. Give the bash scripts execute permission

    chmod +x *.sh
    

    and run it as

    ./launch-rockets.sh
    
  2. Do it as suggested in the solution tab, and invoke bash directly to run the launch script

    for mission in lunar-mission mars-mission jupiter-mission saturn-mission mercury-mission
    do 
        bash ./create-and-launch-rocket $mission
    done
    

    and run the whole thing as

    bash ./launch-rockets.sh
    ```

Literal “Directory not found” is not the same as lietral “Directory caleston does not exist under $HOME” - I think you should adhere to the required output formatting.

BTW, the task description was adjusted:

Create a shell script in the home directory called check_dir.sh.
The script should print the line
Directory exists
if the directory /home/bob/caleston exists.
If not, it should print
Directory not found

but the general issue with this task is still not fixed.
My script works fine, the task validation test does not.

bob@caleston-lp10:~$ mkdir -p caleston
bob@caleston-lp10:~$ ./check_dir.sh 
Directory exists
bob@caleston-lp10:~$ rmdir caleston/
bob@caleston-lp10:~$ ./check_dir.sh 
Directory not found
bob@caleston-lp10:~$

@Alistair_KodeKloud - I did run the script after giving executing permission.

@john_doe - If I understand you correctly, manual execution of the script is working as expected, but when executed with the “Check” functionality in the lab it is failing.

I’ll try again from my end and update.

Thank you.

@john_doe

Looks like when “$HOME” is being used instead of “/home/bob” the check functionality is failing. Attached screenshots for reference.

I am not sure how the “Check” functionality of the lab is implemented. But, when I do a “echo $HOME” on the command prompt, its expanded to /home/bob:

check_dir_1-test

Functionally your script is correct.
If the grading script is checking the text of your script and not the outcome then that will be where the issue is. I shall ask the lab team to look at it…

1 Like

This has been already fixed.