Linux Programming: Difference between revisions

From 太極
Jump to navigation Jump to search
Line 115: Line 115:
shift  ==> moves all the parameter variables down by one.
shift  ==> moves all the parameter variables down by one.
unset  ==> remove variables or functions from the environment.
unset  ==> remove variables or functions from the environment.
</pre>


== Command Execution ==
== Command Execution ==
<pre>
$(command)
`command`
</pre>
Note all new scripts should use the $(...) form, which was introduced to avoid some rather complex rules.
Example
<pre>
#!/bin/sh
echo The current directory is $PWD
echo The current users are $(who)
exit 0
</pre>
The concept of putting the result of a command into a script variable is very powerful, as it makes it easy to use existing commands in scripts and capture their output.
'''Arithmetic Expansion'''
<pre>
$((...))
</pre>
is a better alternative to the '''expr''' command.
'''Parameter Expansion'''
<pre>
${parameter}
</pre>
== Here documents ==
== Debugging Scripts ==
== Debugging Scripts ==



Revision as of 10:43, 21 April 2014

Shell Programming

Variables

food=Banana
echo $food
food="Apple"
echo $food
$HOME
$PATH
$0 -- name of the shell script
$# -- number of parameters passed
$$ process ID of the shell script, often used inside a script for generating unique temp filenames
$1, $2, .... -- parameters given to the script
$* -- list of all the parameters, in a single variable
$@ -- subtle variation on $*

Conditions

We can use the test command to check if a file exists. The command is test -f <filename>.

[] is just the same as writing test, and would always leave a space after the test word.

if test -f fred.c; then ...; fi

if [ -f fred.c ]
then
...
fi

if [ -f fred.c ]; then
...
fi

Arithmetic comparison

expr1 -eq expr2  ==> check equal
expr1 -ne expr2  ==> check not equal
expr1 -gt expr2  ==> expr1 > expr2
expr1 -ge expr2  ==> expr1 >= expr2
expr1 -lt expr2  ==> expr1 < expr2
expr1 -le expr2  ==> expr1 <= expr2
! expr  ==> opposite of expr

File conditionals

-d file  ==> True if the file is a directory
-e file  ==> True if the file exists
-f file  ==> True if the file is a regular file
-r file  ==> True if the file is readable
-s file  ==> True if the file has non-zero size
-w file  ==> True if the file is writable
-x file  ==> True if the file is executable

Control Structures

if

if condition
then
  statements
elif [ condition ]; then
  statements
else 
  statements
fi

for

for variable in values
do 
  statements
done

while

while condition do
  statements
done

until

until condition
do 
  statements
done

AND list

statement1 && statement2 && statement3 && ...

OR list

statement1 || statement2 || statement3 || ...

Functions

Commands

break  ==> escaping from an enclosing for, while or until loop
:      ==> null command
continue ==> make the enclosing for, while or until loo continue at the next iteration
.      ==> executes the command in the current shell
eval   ==> evaluate arguments
exec   ==> replacing the current shell with a different program
export ==> make the variable named as its parameter available in subshells
expr   ==> evaluate its arguments as an expression
printf ==> similar to echo
set    ==> sets the parameter variables for the shell. Useful for using fields in commands that output spaced-separated values
shift  ==> moves all the parameter variables down by one.
unset  ==> remove variables or functions from the environment.

Command Execution

$(command)
`command`

Note all new scripts should use the $(...) form, which was introduced to avoid some rather complex rules.

Example

#!/bin/sh
echo The current directory is $PWD
echo The current users are $(who)
exit 0

The concept of putting the result of a command into a script variable is very powerful, as it makes it easy to use existing commands in scripts and capture their output.

Arithmetic Expansion

$((...))

is a better alternative to the expr command.

Parameter Expansion

${parameter}

Here documents

Debugging Scripts

Working with Files

UNIX environment

Logging

Resources and Limits

Curses

A technique between command line and full GUI.

Example: vi.

Data Management

Development Tools

make and Makefiles

Writing a Manual Page

Distributing Software

The patch Program

Debugging

gdb

Processes and Signals

POSIX threads

Inter-process Communication: Pipes

Sockets