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 02-14-2012, 09:16 AM   #1
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Rep: Reputation: Disabled
Cool Checking the result from a command executed.


Hi,

How can I test the result of a command? In DOS, one can use errorlevel to check the return code and based in this, take further steps. I want to move a file from one directory to another with:

Code:
mv /usr/test/myfile /usr/test/archive/myfile
Then, before I move on to the next step, make sure that the file was indeed moved. If it was not moved, I want to raise some alert. If it was moved, then simply carry on with the rest of the code.

How can I do this?

Thanks,
J
 
Old 02-14-2012, 09:35 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
You've marked this as solved... can you provide any feedback incase others find it useful?
 
Old 02-14-2012, 09:46 AM   #3
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
I may have been a bit quick on the draw on this one, but I though I had is solved. What I did was:

Code:
if [ -f filename ]
then
echo "file was moved"
else
echo "warning"
fi
The problem is, after I moved it, the filename is obviously no longer the same. It consists out of the the new path and filename that was moved. For instance, moving /usr/test/myfile to /usr/test/archive/myfile, the latter is the new full path. What I need is the filename only to do the search...

J
 
Old 02-14-2012, 09:58 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
I didn't actually read your question, just that it was solved with no reply...


the equivalent to that awful ERRORLEVEL nonsense is the $? shell variable:


Code:
# ls -l 1234
-rw-r--r-- 1 root webusers 0 Feb 14 14:56 1234
# echo $?
0
# ls -l 5678
ls: 5678: No such file or directory
# echo $?
2
and if you want to get the filename from a path, use the basename command:

Code:
# basename /usr/bin/ls
ls

Last edited by acid_kewpie; 02-14-2012 at 09:59 AM.
 
Old 02-14-2012, 10:01 AM   #5
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Aha, are you saying that invoking the echo $? immediately after a command will give me the return code of that command?
 
Old 02-14-2012, 10:10 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
yup
 
Old 02-14-2012, 10:16 AM   #7
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
That's exactly what I was looking for! Thanks bud, its working.
 
  


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
Can I execute a shell command and put the result in command field? fran4tw Linux - General 9 08-08-2011 09:27 PM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 03:39 PM
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 01:09 PM
time when a command was executed. anandkj Linux - Newbie 3 12-21-2007 02:09 AM
how to see which command has been executed ? Volcano Linux - Newbie 4 12-11-2006 09:48 AM


All times are GMT -5. The time now is 07:21 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