Shell Scripting: Difference between revisions
From COMP15212 Wiki
pc>Yuron No edit summary |
W81054ch [PHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs) m (1 revision imported) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 20: | Line 20: | ||
done | done | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This script runs <em>two</em> processes: the first reads two lines of input | This script runs <em>two</em> processes: the first reads two lines of input (the presence of the first controlling the <code>while</code> loop) and passes these, concatenated into a single line with a space separator, via a [[Pipes|pipe]] to the other process. The second loop reads four temporary values into (environment) variables, then echoes these back | ||
(the presence of the first controlling the <code>while</code> loop) and passes | to the <code>stdout</code> [[Streams|stream]] in reverse order. | ||
these, concatenated into a single line with a space separator, via a | |||
[Pipes pipe] to the other process. The second loop reads four | |||
temporary values into (environment) variables, then echoes these back | |||
to the <code>stdout</code> [Streams stream] in reverse order. | |||
The simple way to use this script would be to <em>redirect</em> the standard | The simple way to use this script would be to <em>redirect</em> the standard streams to files, thus: <code>script <input >output</code> | ||
streams to files, thus: <code>script <input >output</code> | |||
An example input file is included in [[Media:scripting.zip|here]] and is duplicated | An example input file is included in [[Media:scripting.zip|here]] and is duplicated below for reference. | ||
below for reference. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
1z 1y | 1z 1y | ||
Line 55: | Line 49: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | ---- | ||
{{PageGraph}} | {{PageGraph}} |
Latest revision as of 10:03, 5 August 2019
Depends on | Shell |
---|
Here is an example of Unix shell scripting with a view to showing how some other facilities can be exploited.
Examples
Example 1
#!/bin/bash
while read line1
do
read line2
echo "$line1 $line2"
done \
| while read a b c d
do
echo "$d $c $b $a"
done
This script runs two processes: the first reads two lines of input (the presence of the first controlling the while
loop) and passes these, concatenated into a single line with a space separator, via a pipe to the other process. The second loop reads four temporary values into (environment) variables, then echoes these back
to the stdout
stream in reverse order.
The simple way to use this script would be to redirect the standard streams to files, thus: script <input >output
An example input file is included in here and is duplicated below for reference.
1z 1y
1x 1w
2z 2y
2x 2w
3z 3y
3x 3w
4z 4y
4x 4w
5z 5y
5x 5w
6z 6y
6x 6w
7z 7y
7x 7w
8z 8y
8x 8w
9z 9y
9x 9w
10z 10y
10x 10w