LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 03-01-2017, 11:57 PM   #1
hruday
Member
 
Registered: Jun 2015
Posts: 88

Rep: Reputation: Disabled
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?
 
Old 03-02-2017, 01:42 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
so add
Code:
#!/bin/bash
set -e
at the beginning of that sample.txt, make it executable and run.
 
Old 03-02-2017, 02:35 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,391
Blog Entries: 9

Rep: Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654
Code:
#!/bin/bash

while read -r line
do
$line || exit 1
done< /root/ansi-pb/sample.txt
 
Old 03-02-2017, 04:12 AM   #4
hruday
Member
 
Registered: Jun 2015
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
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.
 
Old 03-02-2017, 04:49 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
that is something called state-machine, you need to save the state at the time of failure and restart from there. Not really trivial....
 
Old 03-02-2017, 08:05 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,422
Blog Entries: 13

Rep: Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740Reputation: 3740
Quote:
Originally Posted by hruday View Post
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 View Post
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.
 
Old 03-04-2017, 03:28 AM   #7
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
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.
 
Old 03-06-2017, 04:56 AM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,000
Blog Entries: 1

Rep: Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846
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.
 
Old 03-06-2017, 05:19 AM   #9
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,000
Blog Entries: 1

Rep: Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846Reputation: 846
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.
 
Old 03-06-2017, 05:25 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
Quote:
Originally Posted by Shadow_7 View Post

Code:
if ( ! $LINE ) then; exit 1; fi
that is post #2
 
1 members found this post helpful.
Old 03-06-2017, 06:45 AM   #11
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script - Supressing Command Output and Writing to Text File Nexusfactor Programming 4 07-03-2015 02:22 AM
[SOLVED] Bash script commands execute even though test fails. CollieJim Linux - General 4 05-31-2014 07:31 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 07:53 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 02:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration