Verifying if the shell command executed or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Verifying if the shell command executed or not?
# 1  
Old 05-12-2008
Verifying if the shell command executed or not?

Hi,

I am working on a shell script that would verify if the mount command has executed or not. So , i have been doing this.


mount /dev/cdrom /mnt/cdrom

echo "$?"

if [ "$?" = "0" ]; then

echo " Mount succesful"

else

echo " Mount unsuccessful"

fi


I have the problem with the if stmt. It is displaying " Mount successful even when it hasn't"


for example

echo "$?" would give 32, instead of going else loop, it still enters the if block of the code.


Can somebody help me with this.?


--Sundeep
# 2  
Old 05-12-2008
Code:
mount /dev/cdrom /mnt/cdrom

echo "$?"          # echo previous command exit code (mount)   .... remove this line!       

if [ "$?" = "0" ]; # if previous command exit code (echo) equal 0 ....

You get the point..
# 3  
Old 05-12-2008
Thanks dude,


That was so stupid of me.

--Sunny
# 4  
Old 05-13-2008
As a stylistic aside, that's really better written as

Code:
if mount /dev/cdrom /mnt/cdrom; then
  echo " Mount succesful"
else
  echo " Mount unsuccessful ($?)"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to check a command executed sucessfully or not

Hi All, I am trying to write a shell script to check if a command executed successfully or not in rhel 7 and finding the installed tomcat version. I am using below script. var4=$(find / -name "catalina.jar" ! -size 0 |egrep -v... (6 Replies)
Discussion started by: sravani25
6 Replies

2. Shell Programming and Scripting

awk command not getting executed in shell script

I am able to execute awk command from shell prompt. but the same command is not getting executed when written and run in a bash script the command from bash cmd prompt. awk '/world/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile file: # hello world my... (4 Replies)
Discussion started by: ashima jain
4 Replies

3. UNIX for Dummies Questions & Answers

How to send keyboard inputs toa UNIX command executed from a shell script?

I have a unix command that prompts for 'y'. How do I run this from my shell script? (4 Replies)
Discussion started by: Sree10
4 Replies

4. Shell Programming and Scripting

Verifying if a file exist (script shell)

Hello, This is my code: nb_lignes=`wc -l $1 | cut -d " " -f1` for i in $(seq $(($nb_lignes - 1)) ) do machine=`head $1 -n $i | tail -1` machine1=`head $1 -n $nb_lignes | tail -1` ssh root@$machine -x " scp /home/file.txt root@$machine1:/home && rm -r /home/file.txt" done fi ... (2 Replies)
Discussion started by: chercheur857
2 Replies

5. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

6. Shell Programming and Scripting

Verifying oracle connection from shell script

Hi, Oracle 9.2 Solaris 10 From the shell script ........How can we verify whether oracle connection is successful or not ? Shell script will prompt the user to enter the oracle schema username, password and tns name. So, *how to verify whether oracle connection has been established or... (14 Replies)
Discussion started by: milink
14 Replies

7. Shell Programming and Scripting

Verifying killall command usage

killall -KILL rdiff-backup Is it a valid command coz i couldn't find a -KILL option for killall in the man page. (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

perl - why is the shell script executed before the print command?

i'm writing some simple scripts to help me learn perl. why does the print command get called after the shell script is executed? the purpose of the shell script is to simply echo to the screen "script run". which is does, but before the print command, you can clearly see the shell script is... (3 Replies)
Discussion started by: mjays
3 Replies

9. Shell Programming and Scripting

problem executed shell command from PL/SQL

i wrote plsql procedure that executed shell command using java class my problem is that in some reason the shell command ( liks Is -l , mv ... ) are not recordnize can someone help me with that 10x Alodvg (2 Replies)
Discussion started by: alodvg
2 Replies

10. HP-UX

Verifying remote command status

hello Friends, I wanted to verify status of remote command executed on remote machine in hpux. remsh <hostname> -l root cd /sampldir i wanted to check weather the command executed on remote machine correctly or not by $?. Can you tell me how to execute multiple command on remote machine. ... (0 Replies)
Discussion started by: ravikiran
0 Replies
Login or Register to Ask a Question