User Tools

Site Tools


os_cp:scripting

Differences

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

Link to this comparison view

Next revision
Previous revision
os_cp:scripting [2023/05/31 00:20] – utworzono jkonczakos_cp:scripting [2026/04/08 20:53] (current) – [Parameters and variables in shell] jkonczak
Line 54: Line 54:
 Notice that ''./file'' (and ''sh file'') interprets the file in a new shell and Notice that ''./file'' (and ''sh file'') interprets the file in a new shell and
 ''. ./file'' (and ''. file'' if PATH contains a ''.'') executes the commands ''. ./file'' (and ''. file'' if PATH contains a ''.'') executes the commands
-form the file in the current shell.+from the file in the current shell.
  
 ====== Processing an input line by the shell ====== ====== Processing an input line by the shell ======
Line 66: Line 66:
 |5. sets up redirections|''echo'', ''a'', ''b'',    ''c d 2+3=5''|…| |5. sets up redirections|''echo'', ''a'', ''b'',    ''c d 2+3=5''|…|
 |6. executes the command||| |6. executes the command|||
-(cf. [[https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_01|POSIX documentation]])+(cf. [[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_01|POSIX documentation]])
  
 ===== Splitting line into commands ===== ===== Splitting line into commands =====
Line 128: Line 128:
 ===== Shell expansions ===== ===== Shell expansions =====
  
-The shell //[[https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_06|expands]]//+The shell //[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06|expands]]//
 the following tokens: the following tokens:
 +<html><div style="margin-top:-1.2em"></div></html>
   - the tilde in ''~'', ''~/…'' (and ''…:~'' / ''…:~/…'' in variable assignment) is expanded to home directory of the current user, \\ the tilde followed by a username (in contexts as above), e.g., ''~user'', is expanded to home directory of the user,   - the tilde in ''~'', ''~/…'' (and ''…:~'' / ''…:~/…'' in variable assignment) is expanded to home directory of the current user, \\ the tilde followed by a username (in contexts as above), e.g., ''~user'', is expanded to home directory of the user,
   - ''$//NAME//'' and ''${…}'' are substituted by a variable/parameter value    - ''$//NAME//'' and ''${…}'' are substituted by a variable/parameter value 
Line 140: Line 141:
 \\ \\
 Inside single quotes no expansions are performed. Inside single quotes no expansions are performed.
 +\\
 +<small>The [[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_02_04|most recent release]]
 +of the POSIX standard also requires expanding **''%%$'…'%%''** by transforming
 +backslash-escaped sequences into characters they represent((This sanctions the
 +feature that has been there for a long time in most shells.)).</small>
  
 **Importantly, the results of expansions 2-4 are split again into tokens whenever **Importantly, the results of expansions 2-4 are split again into tokens whenever
Line 163: Line 169:
 \\ \\
 <small>The C language functions ''[[https://en.cppreference.com/w/c/program/getenv|getenv]]'' <small>The C language functions ''[[https://en.cppreference.com/w/c/program/getenv|getenv]]''
-and ''[[https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html|setenv]]''+and ''[[https://pubs.opengroup.org/onlinepubs/9799919799/functions/setenv.html|setenv]]''
 provide access to the environment variables.</small> provide access to the environment variables.</small>
  
Line 169: Line 175:
  
 For the shell, the name For the shell, the name
-[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05|parameter]]+[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_05|parameter]]
 denotes either script/function arguments or some special variables, while the name denotes either script/function arguments or some special variables, while the name
-[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_03|variable]]+[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_05_03|variable]]
 denotes an ordinary shell variable (that is closely related to an environment variable). denotes an ordinary shell variable (that is closely related to an environment variable).
 \\ \\
Line 198: Line 204:
 <small> <small>
 Moreover the following expansions are performed: Moreover the following expansions are performed:
 +<html><div style="margin-top:-1.2em"></div></html>
   * ''${#X}'' → the length of the value (in characters)   * ''${#X}'' → the length of the value (in characters)
   * ''${X:-expr}'' → value of ''X'' if ''X'' is set and nonempty, ''expr'' otherwise   * ''${X:-expr}'' → value of ''X'' if ''X'' is set and nonempty, ''expr'' otherwise
Line 207: Line 214:
   * ''${X#pattern}'' → ''X'''s value with the shortest match of the ''pattern'' removed from the end \\ ''${X##pattern}'' → ''X'''s value with the longest match of the ''pattern'' removed from the end   * ''${X#pattern}'' → ''X'''s value with the shortest match of the ''pattern'' removed from the end \\ ''${X##pattern}'' → ''X'''s value with the longest match of the ''pattern'' removed from the end
 </small> </small>
 +
 +++++ Examples | {{section>os_cp:scripting:examples#simple_vars&inline&noheader}} ++++
  
 ~~Exercise.#~~ ~~Exercise.#~~
Line 303: Line 312:
  
 |''PATH''|Colon-separated list of directories where binaries are looked up \\ Notice: it's the ''exec…p'' system call that looks up an executable files with a matching name in these paths | |''PATH''|Colon-separated list of directories where binaries are looked up \\ Notice: it's the ''exec…p'' system call that looks up an executable files with a matching name in these paths |
-|''HOME''|Katalog domowy bieżącego użytkownika|+|''HOME''|Path to the home directory of the current user|
 |''PS1'' \\ ''PS2''|The main [[https://en.wikipedia.org/wiki/Command-line_interface#Command_prompt|prompt]] \\ The prompt for next line of multi-line commands| |''PS1'' \\ ''PS2''|The main [[https://en.wikipedia.org/wiki/Command-line_interface#Command_prompt|prompt]] \\ The prompt for next line of multi-line commands|
 |''PWD''|Current working directory| |''PWD''|Current working directory|
Line 332: Line 341:
 \\ \\
 The shell understands assignments and operators similar to those in C (cf. The shell understands assignments and operators similar to those in C (cf.
-[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_01_02_01|table of operators]]).+[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap01.html#tag_18_01_02_01|table of operators]]).
 \\ \\
 Inside ''%%$((…))%%'' one can leave out the ''$'' preceding variable names, e.g., ''%%X=$((X+2))%%''. Inside ''%%$((…))%%'' one can leave out the ''$'' preceding variable names, e.g., ''%%X=$((X+2))%%''.
Line 372: Line 381:
 ===== Aliases ===== ===== Aliases =====
  
-[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03_01|Aliases]]+[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_19_03_01|Aliases]]
 are intended to provide user-defined alternative names for a command. are intended to provide user-defined alternative names for a command.
 \\ \\
Line 404: Line 413:
 ====== The read command ====== ====== The read command ======
  
-The ''**[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html|read]]**''+The ''**[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/read.html|read]]**''
 command reads a line from the standard input. command reads a line from the standard input.
 \\ \\
Line 507: Line 516:
 ==== if ==== ==== if ====
  
-The ''[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_07|if]]'' statement has the following syntax:+The ''[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_07|if]]'' statement has the following syntax:
 <html><pre style="margin:0"> <html><pre style="margin:0">
 <b>if</b> <i>command1</i> <b>if</b> <i>command1</i>
Line 544: Line 553:
 ==== case ==== ==== case ====
  
-The ''[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_05|case]]'' statement (in many other languages called the switch statement) has the following syntax:+The ''[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_05|case]]'' statement (in many other languages called the switch statement) has the following syntax:
 <html><pre style="margin:0"> <html><pre style="margin:0">
 <b>case</b> <i>value</i> <b>in</b> <b>case</b> <i>value</i> <b>in</b>
Line 578: Line 587:
 ==== for ==== ==== for ====
  
-The ''[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_03|for]]'' loop iterates over a set of values. It has the following syntax:+The ''[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_03|for]]'' loop iterates over a set of values. It has the following syntax:
 <html><pre style="margin:0"> <html><pre style="margin:0">
 <b>for</b> <i>VAR</i> <b>in</b> <i>value1</i> <i>value2</i> <i>…</i>   <b>for</b> <i>VAR</i> <b>in</b> <i>value1</i> <i>value2</i> <i>…</i>  
Line 615: Line 624:
 ==== while and until ==== ==== while and until ====
  
-<html><p style="margin:0">The syntax of the <a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_09" class="urlextern" title="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_09" rel="nofollow">while</a> loop is as follows:</p><pre style="margin:0">+<html><p style="margin:0">The syntax of the <a href="https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_09" class="urlextern" title="https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_09" rel="nofollow">while</a> loop is as follows:</p><pre style="margin:0">
 <b>while</b> <i>command1</i>   <b>while</b> <i>command1</i>  
 <b>do</b> <b>do</b>
Line 621: Line 630:
 <b>done</b> <b>done</b>
 </pre></html> </pre></html>
-and the syntax of the ''[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_11|until]]'' loop is as follows:+and the syntax of the ''[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_04_11|until]]'' loop is as follows:
 <html><pre style="margin:0"> <html><pre style="margin:0">
 <b>until</b> <i>command1</i>   <b>until</b> <i>command1</i>  
Line 646: Line 655:
 ====== Functions in shell ====== ====== Functions in shell ======
  
-To define a [[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05|function]]+To define a [[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_09_05|function]]
 in the shell, one has to use the following syntax: ''//name//() //body//'', in the shell, one has to use the following syntax: ''//name//() //body//'',
 where the //body// shall be a compound command such as a list of statements  where the //body// shall be a compound command such as a list of statements 
Line 694: Line 703:
 ====== Signals in shell ====== ====== Signals in shell ======
  
-The command ''[[https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28|trap]] "//statements//" //CONDITION//''+The command ''[[https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_29|trap]] "//statements//" //CONDITION//''
 is used to set up either handing a signal (when the //CONDITION// is a name of a is used to set up either handing a signal (when the //CONDITION// is a name of a
 signal) or set the statements to be executed upon terminating the shell (when the signal) or set the statements to be executed upon terminating the shell (when the
os_cp/scripting.1685492408.txt.gz · Last modified: (external edit)