Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-01-2017, 11:57 PM
|
#1
|
Member
Registered: Jun 2015
Posts: 88
Rep:
|
bash script to read commands from text file and abort if any command fails
I've bash script written to read each line of a file which has commands and execute them one by one.
#!/bin/bash
while read -r line
do
command ${line}
done< /root/ansi-pb/sample.txt
sample.txt has following commands
echo "hello world"
df -h
free -m
mkdir /tmp/`hostname`_bkp
touch /tmp/`hostname`_bkp/file{1..5}
mvn -version
echo "directory and files created"
Now if any command fails say mvn -version then script should stop executing next command.
How to achieve this?
|
|
|
03-02-2017, 01:42 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460
|
so add at the beginning of that sample.txt, make it executable and run.
|
|
|
03-02-2017, 02:35 AM
|
#3
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
Code:
#!/bin/bash
while read -r line
do
$line || exit 1
done< /root/ansi-pb/sample.txt
|
|
|
03-02-2017, 04:12 AM
|
#4
|
Member
Registered: Jun 2015
Posts: 88
Original Poster
Rep:
|
Quote:
Originally Posted by ondoho
Code:
#!/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.
|
|
|
03-02-2017, 04:49 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460
|
that is something called state-machine, you need to save the state at the time of failure and restart from there. Not really trivial....
|
|
|
03-02-2017, 08:05 AM
|
#6
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,938
|
Quote:
Originally Posted by hruday
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.
|
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.
|
|
|
03-04-2017, 03:28 AM
|
#7
|
Member
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
|
Reminds me of 'doing' LFS book [not that I have]
Maybe add an incremental count [if $?==0]; echo $count >myStateFile
Then, as initialization, skip-read that many lines of the input file.
I think Best wishes. Let 'us' know!
Last edited by Jjanel; 03-04-2017 at 03:33 AM.
|
|
|
03-06-2017, 04:56 AM
|
#8
|
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 4,137
|
FILE: temp.txt
Code:
echo "hello world"
df -h
free -m
mkdir /tmp/`hostname`_bkp
touch /tmp/`hostname`_bkp/file{1..5}
mvn -version
FILE: temp.sh
Code:
#!/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
$ temp.sh < temp.txt
alternatively to have less output.
Code:
if ( ! $LINE ) then; exit 1; fi
Last edited by Shadow_7; 03-06-2017 at 05:00 AM.
|
|
|
03-06-2017, 05:19 AM
|
#9
|
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 4,137
|
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.
|
|
|
03-06-2017, 05:25 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,460
|
Quote:
Originally Posted by Shadow_7
Code:
if ( ! $LINE ) then; exit 1; fi
|
that is post #2
|
|
1 members found this post helpful.
|
03-06-2017, 06:45 AM
|
#11
|
Member
Registered: May 2016
Posts: 222
Rep:
|
if ( ! "$LINE" ): And i am not sure why one would want a subshell at all. And use more quotes.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 01:50 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|