Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Some common ways of getting input into a program are the following.
Standard input (keyboard, etc.)
External file (explicit read, not the standard input)
Command line (limited but useful)
Note: A common use of the command line is to pass the name of an external file to be used for more input.
This page covers some simple aspects of Bash scripting and how Bash scripts interact with C programs via the command line.
A typical C program to add two integers, compute the sum, and output the sum, is as follows.
Here is the C code.
Here is the output of the C code.
What is new in the above program is the following.
The stdlib.h library is included.
The function atoi in the stdlib.h library is used to convert a string (of digits) into an integer (with the expected value).
The name atoi stands for ASCII (American Standard Code for Information Interchange) to integer (in 1972 ASCII was the primary character set).
2. Command line arguments
Arguments can be passed to a program from the command line.
In C, one can modify the main function as follows.
argc is the integer count of the passed arguments.
argv is the string array of passed arguments.
./hello.exe one two three
"./hello.exe" is argument 0.
"one" is argument 1.
"two" is argument 2.
"three" is argument 3.
3. Self reference
The list starts at 1 where the argument at position 0 is the name of the program.
This, a program can check the argument passed at position 0 and answer the question, "Who am I?".
Note: If one renames a program, and that program is run, the argument passed at position 0 shows how the program was invoked.
4. Program
The following program adds the supplied arguments and returns that as the return code.
Here is the C code.
5. Output
Here is the output of the C code.
Note that (when run from Windows) Cygwin will use the Windows path as seen by Cygwin.
6. Run the program
Suppose the following command is used to run the program from Cygwin.
The following command can be used to see the return code from running the program.
echo $?
Note: The dollar sign "$" is the escape meta-character for printing environment variables in Bash.
9. Return code output
The output is as follows.
8
This is the sum of the arguments from the command line.
Note: If any other command is run, the return code is changed, so one wants to get that return code before running anything else from the command line.
10. Bash scripts
A Bash script is a way to automate commands typed at the command line (and make conditional decisions, loop, create functions and procedures, etc.).
A Bash script can be used to invoke a program and check the return code.
In the above program, the return code is the sum of the two arguments.
11. Shell programs
A shell program, such as PowerShell in Windows, is a program that provides a command line to the user and provides a way to automate those commands via scripts.
Linux shell (Bash, etc.)
PowerShell
DOS Batch (Console window)
12. Bash name
The original UNIX shell program was not the best.
In the 1980's, a person named Bourne write a much better shell program.
13. Bourne shell
In the 1970's President Jimmy Carter was known for, among other things, being a "Born again Christian" - someone who has had a dramatic transformation in their life.
Someone made a play on words and called Bourne's dramatic improvement of the UNIX shell program a "Bourne Again Shell". The short name became the acronym Bash.
14. Bash script
Here is a simple Bash script called add2.bsh to call the above program with arguments and then print the result code.
#!/bin/bash
myArg1=3
myArg2=5
echo "myArg1 is ${myArg1}."
echo "myArg2 is ${myArg2}."
echo "Begin run"
/cygdrive/d/W/PROGRAM1.C/bash11-02 $myArg1 $myArg2
myResult1=$?
echo "End run"
echo "The sum of ${myArg1} and ${myArg2} is ${myResult1}."
Note: The first line with the hash-bang "#!" and path is to allow Linux to auto-determine that this text is code text and that the program /bin/bash knows how to run this code text.
15. Command line
Here is the command line command to call the Bash script add2.bsh which is in folder ~/BSH.
source ~/BSH/add1.bsh
16. Command line output
Here is the command line output.
myArg1 is 3.
myArg2 is 5.
Begin run
Arguments: 3
0. "/cygdrive/d/W/PROGRAM1.C/bash11-02"
1. "3"
2. "5"
3 + 5 is 8
End run
The sum of 3 and 5 is 8.
17. Transitive abstraction
Abstractions like those above can be cascaded to more and more levels, with programs calling programs calling programs, etc., in a transitive manner.
The outermost level is, of course, the user or programmer, as one traces each level of computation to the next outer level (outside the box, so to speak).