Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble understanding a simple shell script

I have not used shell scripting in ages. I have searched online for help but have not found adequate guides..

Here is the code I am trying to understand:

#!/bin/sh
# -*-sh-*-
if gcc $1 &> cmp_out; then
    if ! ./a.out &> run_out; then
        exit 0;                 # Success.
    fi
fi
exit 1;                         # Failure.

what is cmp_out, and run_out? Are these std_out?

I know that &> is a redirection instruction. But i do not get what that means in this scripts syntax in the slightest bit.

I am looking for a high level overview of what this script is doing. If you need more information about why i am using this script let me know. I will include more about the why.

In short it is an included example of delta debugging (//www.st.cs.uni-saarland.de/dd/)

like image 796
Recurrsion Avatar asked Nov 12 '12 22:11

Recurrsion


People also ask

Why shell scripting is hard?

Shell scripting is hard because it is not easy to get started. There are many different ways to write scripts, as well as many different versions of the same language and there's a huge gap between experts and beginners.

Is shell difficult to learn?

Shell scripting is not a single language but, because it uses some natural language commands, it's easy to learn, even without a programming background. However, each shell scripting dialect is considered a language, and if you plan more complex activities, shells take a lot of practice.

How long does it take to learn shell scripting?

You can learn this language in this course called Linux Shell Scripting through many projects, which will take you around two weeks to a month to complete.


1 Answers

This script takes one argument, the name of a source file. It compiles that source file with gcc and if that compilation succeeds it executes the result. (By default, gcc writes the executable as a file named a.out.)

If the compilation fails, you're in luck, because the script saves the output (both standard output and standard error) into a file called cmp_out. And if the executed result produces any output, it saves that in run_out. The script itself writes nothing to stderr or stdout. Even if gcc isn't found, that error will end up in cmp_out.

The strangest thing about the script is that it inverts the exit status of the execution, using the exclamation point after the if. If executing a.out fails, the script exits with 0, success. If a.out succeeds, the script exits with 1, failure, which is also what it does if the compilation fails. Maybe the exclamation point is an error? The semicolon after the integers are unnecessary.

like image 144
Rob Davis Avatar answered Nov 14 '22 19:11

Rob Davis