bash script to read commands from text file and abort if any command fails
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
#!/bin/bash
while read -r line
do
$line || exit 1
done< /root/ansi-pb/sample.txt
Thank u,
I just want to improvise the script, so if I correct my command and run script again it should start executing from the command it got failed instead of executing from first command. Will it be possible in bash scripting.
I just want to improvise the script, so if I correct my command and run script again it should start executing from the command it got failed instead of executing from first command. Will it be possible in bash scripting.
Quote:
Originally Posted by pan64
that is something called state-machine, you need to save the state at the time of failure and restart from there. Not really trivial....
This is quite correct. You need to save the information, have your script stop and prompt, allow yourself to attempt to correct the TXT file and then use the prompt in your script to continue. Bear in mind that depending how you "remember" where you were, things may also change. For instance, say you remembered line number of the text file, and then you go and edit the text file and delete a ton of lines, thus changing it, you'll then change the circumstances by which you remembered where you were. As pan64 points out, this is not trivial.
#!/bin/bash
while read LINE
do
if ( $LINE ) then
echo -n $LINE" ... "
echo "[ OK ]"
else
echo -n $LINE" ... "
echo "[ FAILED ]"
exit 1
fi
done
echo "directory and files created"
exit 0
Since a restart would be manual, you could have a counter and use "tail -n $REMAINING_COUNT $FILE" as the input. But always test first as there could be some inclusive / exclusive quirks depending on version of bash / tail / ... and the default environment of a given distro. Or manually edit the input to compensate for completed tasks for peace of mind. On a copy ofc, and remove it when done. But I cut my teeth on mainframes with single threaded batch processing. Step 1, create a list. Step 2, do the list. Step 3, verify. Step 4, if fail then cry.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.