LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10: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, 12:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

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

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

while read -r line
do
$line || exit 1
done< /root/ansi-pb/sample.txt
 
Old 03-02-2017, 03: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, 03:49 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
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, 07:05 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

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

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

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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, 04:25 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
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, 05: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



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 01:22 AM
[SOLVED] Bash script commands execute even though test fails. CollieJim Linux - General 4 05-31-2014 06:31 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 01:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM

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

All times are GMT -5. The time now is 06:52 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
Open Source Consulting | Domain Registration