InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Deciding Whether a Command Succeeds in bash
Bash

Deciding Whether a Command Succeeds in bash

InfinityCoder February 9, 2017

You need to run some commands, but you only want to run certain commands if certain other ones succeed.

For example, you’d like to change directories (using the cd command) into a temporary directory and remove all the files.

However, you don’t want to remove any files if the cd fails (e.g., if permissions don’t allow you into the directory, or if you spell the directory name wrong).

We can use the exit status ($?) of the cd command in combination with an if statement to do the rm only if the cd was successful.

1
2
cd mytmp
if (( $? )); then rm * ; fi

Obviously, you wouldn’t need to do this if you were typing the commands by hand.
You would see any error messages from the cd command, and thus you wouldn’t type the rm command.

But scripting is another matter, and this test is very well worth doing to make sure that you don’t accidentally erase all the files in the directory where you are running.
Let’s say you ran that script from the wrong directory, one that didn’t have a subdirectory named mytmp.

When it runs, the cd would fail, so the current directory remains unchanged.

Without the if check (the cd having failed) the script would just continue on to the next statement.

Running the rm * would remove all the files in your current directory. Ouch. The if is worth it.
So how does $? get its value? It is the exit code of the command. For C Language programmers, you’ll recognize this as the value of the argument supplied to the exit( ) function; e.g., exit(4); would return a 4.

For the shell, zero is considered success and a non-zero value means failure.
If you’re writing bash scripts, you’ll want to be sure that your bash scripts explicitly set return values, so that $? is set properly from your script.

If you don’t, the value set will be the value of the last command run, which you may not want as your result.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Showing All Hidden (dot) Files in the Current Directory in bash
You want to see only hidden (dot) files in a …

Showing All Hidden (dot) Files in the Current Directory in bash

Saving Output When Redirect Doesn’t Seem to Work in bash
You tried using > but some (or all) of the …

Saving Output When Redirect Doesn’t Seem to Work in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap