Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizGeneral Tech Bugs & Fixes 2 years ago
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.
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 nanorc
directory. 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
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
manpreet
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?