0% found this document useful (0 votes)
15 views

2.7 Control Flow - If

The document discusses various shell scripting control flow structures including if/then/else conditional statements and while/until loops. It explains that shift renames the positional parameters and loses the first one, and an until loop will repeat commands until a specified test condition is met, such as a file existing. If/then/else statements can be used to conditionally execute blocks of commands based on the exit status of a test.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

2.7 Control Flow - If

The document discusses various shell scripting control flow structures including if/then/else conditional statements and while/until loops. It explains that shift renames the positional parameters and loses the first one, and an until loop will repeat commands until a specified test condition is met, such as a file existing. If/then/else statements can be used to conditionally execute blocks of commands based on the exit status of a test.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

shift done

is equivalent to
for i do ... done

shift is a shell command that renames the positional parameters $2, $3, ... as $1, $2, ... and loses $1.

Another kind of use for the while/until loop is to wait until some external event occurs and then run some commands. In an until loop the termination condition is reversed. For example,
until test -f file do sleep 300; done commands

will loop until file exists. Each time round the loop it waits for 5 minutes before trying again. (Presumably another process will eventually create the file.)

2.7 Control flow - if


Also available is a general conditional branch of the form,
if command-list then command-list else command-list fi

that tests the value returned by the last simple command following if.

The if command may be used in conjunction with the test command to test for the existence of a file as in
if test -f file then process file else do something else fi

An example of the use of if, case and for constructions is given in section 2.10.

A multiple test if command of the form


if ... then else ... if ... then ... else if ... ... fi fi

fi

may be written using an extension of the if notation as,


if ...

then elif then elif ... fi

... ... ... ...

The following example is the touch command which changes the `last modified' time for a list of files. The command may be used in conjunction with make (1) to force recompilation of a list of files.
flag= for i do case $i in -c) flag=N ;;

You might also like