LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-25-2009, 11:27 PM   #1
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Rep: Reputation: Disabled
printing my own message as each command in a bash script has completed successfully


I would like to print my own messages as each command finishes with success. Something like "foo has been moved", "~/foo_foo has been removed", foo_foo_foo has been copied"
an example:
Code:
#!/bin/bash

mv foo &&
rm ~/foo_foo&&
cp foo_foo_foo

exit
 
Old 01-25-2009, 11:45 PM   #2
laitcg
LQ Newbie
 
Registered: Oct 2008
Location: Westside, Oahu, Hawaii
Distribution: Slackware64-current
Posts: 13

Rep: Reputation: 3
echo "Hello World!"

Might I recommend the Advanced Bash Scripting Guide: http://tldp.org/LDP/abs/html/
 
Old 01-26-2009, 12:13 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A slightly better way that ensures that the action was actually completed first:

Code:
if [ "$?" = "0" ]; then
   echo "success"
else
   echo "fail"
fi
This tests the exit code of the last executed command, and prints an appropriate message depending on if it's zero (success), or not (fail).

Edit: Here's a streamlined version you can tack onto the end of a command for single-line use.

Code:
mv foo && echo "success" || echo "fail"

Last edited by David the H.; 01-26-2009 at 12:23 AM.
 
Old 01-26-2009, 12:37 AM   #4
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
A slightly better way that ensures that the action was actually completed first:

Code:
if [ "$?" = "0" ]; then
   echo "success"
else
   echo "fail"
fi
This tests the exit code of the last executed command, and prints an appropriate message depending on if it's zero (success), or not (fail).

Edit: Here's a streamlined version you can tack onto the end of a command for single-line use.

Code:
mv foo && echo "success" || echo "fail"
Thanks David. What about if I wanted to display a message until the file finished moving and then display if it was successful or not?
 
Old 01-26-2009, 12:45 AM   #5
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
with this
Code:
#! /bin/bash
# cp.sh

while cp archives.tar archives2.tar
 do echo "copying"
   if [ "$?" = "0" ]; then
   echo "success"
else
   echo "fail"
fi
exit
I am getting an error:

./cp.sh: line 11: syntax error: unexpected end of file
 
Old 01-26-2009, 01:21 AM   #6
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
now I'm only getting the success message :

Code:
#! /bin/bash

cp archives.tar archives2.tar
while [ "$?" != "0" ];
   do
       echo "copying"
   done
if [ "$?" = "0" ]; then
   echo "success"
else
   echo "fail"
fi
 
Old 01-26-2009, 01:31 AM   #7
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
tried this but "copying" does not display as the file is copying. Any suggestions?
Code:
#! /bin/bash

cp archives.tar archives2.tar
while [ "$?" != "0" ];
   do
       echo "copying"
   done
if [ "$?" = "0" ]; then
   echo "success"
else
   echo "fail"
fi
 
Old 01-26-2009, 02:36 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I don't think any of those will work. You don't get an exit code until after the process is complete, so you can't test for it until then. Also, most bash actions are run sequentially, so it's not a simple task to get two operations going at the same time (such as testing the status while the program is running). Finally, $? outputs the exit status of the last command run in that shell, so it will be testing whatever you did before the loop.

In most cases, all you really need to do is echo a line before the command stating that you're starting the job, then output the exit status afterwards. That's what most people do.

Code:
echo "copying file foo"

cp foo foo_foo && echo "file foo copied" || "failed to copy file foo"
 
Old 01-26-2009, 02:58 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just an aside note: the exit status can be treated as a numerical value and the correct way to check it is to use the integer comparison operator:
Code:
if [ $? -eq 0 ]
then
   echo success
fi
Another way to use the if/then test is for testing commands (not expressions) so that you can do something like this:
Code:
if cp archives.tar archives2.tar
then
  echo success
else
  echo failed
fi
in this case the exit code of the command is still evaluated, but you embed the command and the test in a single construct.
 
Old 01-26-2009, 11:02 AM   #10
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
thanks guys
 
  


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 as command whited Programming 15 12-10-2007 06:14 AM
Bash Script- command on exit PDock Programming 6 12-23-2005 07:03 PM
If bash command can send message to a host ? naihe2010 Programming 4 10-29-2005 03:28 PM
Simple Bash Script Help Command kemplej Linux - Software 1 03-11-2004 03:52 AM
linux command error message bash: /usr/bin/find: No such file or directory sundaram123 Linux - General 8 04-02-2002 07:18 AM

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

All times are GMT -5. The time now is 06:40 PM.

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