User Tools

Site Tools


unix_survival

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
unix_survival [2020/04/08 21:08]
joshd
unix_survival [2020/04/08 23:16] (current)
joshd
Line 1: Line 1:
 **UNIX Command Reference** **UNIX Command Reference**
  
-The command prompt in UNIX is called the //shell// Here is a list of common UNIX commands which will work across many different versions of UNIX with discrepancies in Example/Notes.+The command prompt in UNIX is called the //shell//, which typically uses a dollar sign ($) as its prompt.  Here is a list of common UNIX commands which will work across many different versions of UNIX with discrepancies in Example/Notes.
  
 ||Command||Description||Examples||Notes|| ||Command||Description||Examples||Notes||
Line 102: Line 102:
 </code> </code>
  
 +Here's a more complex program.  It makes use of //for// loops to print a small multiplication table.
  
 +<code>
 +$ ed table.c
 +?table.c
 +a
 +int main(argc, argv)
 +    int argc;
 +    char** argv;
 +{
 +    int i,j;
 +    
 +    /* Use nested 'for' loops to print a 4x4 multiplication table. */
 +    for(i=1;i<5;i++)
 +    {
 +        for(j=1;j<5;j++)
 +        {
 +            /* '%d' means 'Substitute a numerical value here' */
 +            printf("%d x %d = %d\t", i, j, i * j);
 +        }
 +        
 +        /* '\n' is the "newline" character -- this moves the cursor to the next line
 +           for the next row of the table */
 +        printf("\n");
 +    }
 +
 +    return 0;   
 +}
 +.
 +w
 +516
 +q
 +$
 +</code>
 +
 +Compiling this as in the above example and running it yields:
 +
 +
 +<code>
 +$ cc table.c
 +$ ./a.out
 +1 x 1 = 1       1 x 2 = 2       1 x 3 = 3       1 x 4 = 4
 +2 x 1 = 2       2 x 2 = 4       2 x 3 = 6       2 x 4 = 8
 +3 x 1 = 3       3 x 2 = 6       3 x 3 = 9       3 x 4 = 12
 +4 x 1 = 4       4 x 2 = 8       4 x 3 = 12      4 x 4 = 16
 +$
 +</code>
 +
 +** Shell Programming **
 +
 +The shell itself is programmable via /shell scripts/ A shell script can invoke any of the standard UNIX tools as well as other shell scripts, and can make use of control constructs, including loops and comparisons.  As with C, a complete tutorial is beyond the scope of this page but we'll provide a few examples to get you started.  The below examples are for // /bin/sh//, the //Bourne shell//, as this is available on all online UNIX systems.  Other common shells are //ksh, csh, tcsh, and bash//.
 +
 +As with the C examples above, we'll use //ed// to create our source files.  (Short shell scripting can also be done interactively at the command line.)
 +
 +<code>
 +$ ed hello.sh
 +#!/bin/sh
 +
 +echo "Hello, world!"
 +.
 +w
 +33
 +q
 +$
 +</code>
 +
 +The first line (//#!/bin/sh//) is sometimes referred to as the "shebang" line (because UNIX programmers are weird) and tells the operating system what shell to invoke to execute the script file -- in this case the Bourne shell, in /bin/sh.
 +
 +Now we've created the script, and there it is on disk:
 +
 +<code>
 +$ ls -l hello.sh
 +-rw-rw-rw-   1 user       user             33 Apr  9 05:56 hello.sh
 +</code>
 +
 +But if we try to run it, we get:
 +
 +<code>
 +$ ./hello.sh
 +sh: ./hello.sh: Execute permission denied.
 +</code>
 +
 +(The exact message may vary depending on what UNIX you're on.)  This is telling us that the script we created doesn't have the //execute bit// set (only the //read// and //write// bits indicated by //rw//) -- so it cannot be run.  The //chmod// command can be used to set the execute bit on our new shell script:
 +
 +<code>
 +$ chmod u+x hello.sh
 +$ ls -l hello.sh
 +-rwxrw-rw-   1 root       sys             33 Apr  9 05:56 hello.sh
 +$
 +</code>
 +
 +As you can see the hello.sh file now has the //execute bit// set (see the "x" in the file listing above).  Now it can be executed:
 +
 +<code>
 +$ ./hello.sh
 +Hello, world!
 +$
 +</code>
  
unix_survival.1586380097.txt.gz · Last modified: 2020/04/08 21:08 by joshd