When can a “cd” command fail in a shell script and what can I do to remedy it?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have a shell script that failed to finish last week; it was a failed "cd" command and it exits if it fails.

The script is a bash shell script for configuring new Debian installs. Here is the full script: debianConfigAswome.sh. The script is run as root so it has full access to the file-system.

Can you please list all the reasons a script would not be able to successfully execute a cd command and what to do to avoid the error?

profilepic.png
manpreet 2 years ago

 

There are various reasons why a cd could fail. The target might not exist, the target might not be a directory, you might not have permission to access the target directory, cd might not be found (although this is extremely unlikely since it's a shell builtin), the chdir() operation might fail due to a broken file system etc.

In this particular case, however, it looks like a bug in the script. The script you link to has two calls to cd:

cd "$WORK_DIR"/nanorc || { echo "cd failed"; exit 127; }

and

cd $HOME || { echo "cd $HOME failed"; exit 155; }

I am assuming the one that failed is the first one, since cd $HOME should normally work. This is the relevant (slightly simplified) section of your script:

if [ ! -d "$WORK_DIR"/nanorc  ]
then
    echo "Setting up Nanorc file for all users....please, wait!"
    git clone https://$OAUTH_TOKEN:x-auth-basic@github.com/gnihtemoSgnihtemos/nanorc || { echo "git failed"; exit 127; }
    chmod 755 "$WORK_DIR"/nanorc || { echo "chmod nanorc failed"; exit 127; }
     cd "$WORK_DIR"/nanorc || { echo "cd failed"; exit 127; }
fi

So, if "$WORK_DIR"/nanorc is not a directory, you run a git command which creates the nanorcdirectory. The first possible issue is that the nanorc will be created in the curent directory which might not be $WORK_DIR. At this point in your script, you haven't actually moved to $WORK_DIR, so it should only work if you run the script from within $WORK_DIR.

So the simple solution is to add a cd $WORK_DIR before the git command:

if [ ! -d "$WORK_DIR"/nanorc  ]
then
    cd "$WORK_DIR" | { echo "cd $WORK_DIR failed"; exit 127; }
    echo "Setting up Nanorc file for all users....please, wait!"
    git clone https://$OAUTH_TOKEN:x-auth-basic@github.com/gnihtemoSgnihtemos/nanorc || { echo "git failed"; exit 127; }
    chmod 755 "$WORK_DIR"/nanorc || { echo "chmod nanorc failed"; exit 127; }
     cd "$WORK_DIR"/nanorc || { echo "cd failed"; exit 127; }
fi

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.