LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-26-2009, 12:27 AM   #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-26-2009, 12:45 AM   #2
laitcg
LQ Newbie
 
Registered: Oct 2008
Location: Westside, Oahu, Hawaii
Distribution: Slackware64-current
Posts: 11

Rep: Reputation: 3
echo "Hello World!"

Might I recommend the Advanced Bash Scripting Guide: http://tldp.org/LDP/abs/html/
 
Old 01-26-2009, 01:13 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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 01:23 AM.
 
Old 01-26-2009, 01: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, 01: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, 02: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, 02: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, 03:36 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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, 03:58 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 12:02 PM   #10
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
thanks guys
 
  


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


All times are GMT -5. The time now is 08:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration