DCEM's bash script howto: Unterschied zwischen den Versionen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „I'm not vell versed in using bash script and here I store the information that I think is useful. [https://google.github.io/styleguide/shell.xml Google Style…“)
 
(minor corrections (by SublimeText.Mediawiker))
 
(12 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
I'm not vell versed in using bash script and here I store the information that I think is useful.
 
I'm not vell versed in using bash script and here I store the information that I think is useful.
 +
 +
== Code Style ==
 +
I use bashate as lint tool:
 +
 +
[https://github.com/openstack/bashate bashate]
  
 
[https://google.github.io/styleguide/shell.xml Google Style Guides: Shell]
 
[https://google.github.io/styleguide/shell.xml Google Style Guides: Shell]
  
Indentations
+
=== Indentations ===
I like to be able to copy and paste parts of a shell script to the shell.
+
I like to be able to copy and paste parts of a shell script directly to the shell.
Inden
+
 
 +
Indentations with tabs will cause problems here, this is why I prefer spaces.
 +
 
 +
== Strategy ==
 +
I prefer to use command line editors like sed to edit config (or similar) files.
 +
 
 +
This way these edits are scriptable.
 +
 
 +
If there are variables these will be defined as enviromental variables at the beginning of a script.
 +
 
 +
This way it is easy to see what customisations are intended/needed.
 +
 
 +
=== Fail fast ===
 +
I think it is a good idea to have a script stop as soon as any error appers.
 +
 
 +
[https://codeinthehole.com/tips/bash-error-reporting/ Bash error reporting - by David Winterbottom] promotes a nice concept which I modiefied for better readability.
 +
 
 +
<nowiki>
 +
#!/bin/bash
 +
set -o nounset  # treat unset variables as an error when substituting
 +
set -o pipefail # the return value of a pipeline is the status of
 +
                #    the last command to exit with a non-zero status,
 +
                #    or zero if no command exited with a non-zero status
 +
set -o errexit  # exit immediately if a command exits with a non-zero status
 +
 
 +
function print_error {
 +
    read line file <<<$(caller)
 +
    echo "An error occurred in line $line of file $file:" >&2
 +
    sed "${line}q;d" "$file" >&2
 +
}
 +
trap print_error ERR</nowiki>
 +
 
 +
=== using here-documents ===
 +
To create or append to a config file i like to use here-documents.
 +
 
 +
 
 +
Using a here-document with <code style="white-space: nowrap">-</code> would allow you to remove tabs to have a nice indentation in the script, but I'm not using tags for indentation.
 +
 
 +
 
 +
create files with
 +
<code style="white-space: nowrap">></code>,
 +
expand files with
 +
<code style="white-space: nowrap">>></code>,
 +
after cat
 +
 
 +
==== expansions: on - EOF needs to be unquoted ====
 +
EOF is unquoted => all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.
 +
 
 +
<nowiki>
 +
cat > /etc/nginx/snippets/letsencrypt.conf << EOF
 +
    ssl_certificate /etc/letsencrypt/live/$(hostname)/fullchain.pem;
 +
    ssl_certificate_key /etc/letsencrypt/live/$(hostname)/privkey.pem;
 +
EOF</nowiki>
 +
<code style="white-space: nowrap">$(hostname)</code> will be expanded
 +
 
 +
==== expansions: off - EOF needs to be quoted ====
 +
'EOF' is quoted => the lines in the here-document are not expanded
 +
<nowiki>
 +
cat >> /etc/nginx/conf.d/seafile.conf << 'EOF'
 +
    location / {
 +
        proxy_set_header  Host $host;
 +
    [...]
 +
    }
 +
EOF</nowiki>
 +
<code style="white-space: nowrap">$host</code> will not be expanded
 +
 
 +
== Links ==
 +
[https://www.grymoire.com/Unix/Sed.html The Grymoire - Sed Tutorial]

Aktuelle Version vom 26. März 2020, 11:23 Uhr

I'm not vell versed in using bash script and here I store the information that I think is useful.

Code Style

I use bashate as lint tool:

bashate

Google Style Guides: Shell

Indentations

I like to be able to copy and paste parts of a shell script directly to the shell.

Indentations with tabs will cause problems here, this is why I prefer spaces.

Strategy

I prefer to use command line editors like sed to edit config (or similar) files.

This way these edits are scriptable.

If there are variables these will be defined as enviromental variables at the beginning of a script.

This way it is easy to see what customisations are intended/needed.

Fail fast

I think it is a good idea to have a script stop as soon as any error appers.

Bash error reporting - by David Winterbottom promotes a nice concept which I modiefied for better readability.

#!/bin/bash
set -o nounset  # treat unset variables as an error when substituting
set -o pipefail # the return value of a pipeline is the status of
                #    the last command to exit with a non-zero status,
                #    or zero if no command exited with a non-zero status
set -o errexit  # exit immediately if a command exits with a non-zero status

function print_error {
    read line file <<<$(caller)
    echo "An error occurred in line $line of file $file:" >&2
    sed "${line}q;d" "$file" >&2
}
trap print_error ERR

using here-documents

To create or append to a config file i like to use here-documents.


Using a here-document with - would allow you to remove tabs to have a nice indentation in the script, but I'm not using tags for indentation.


create files with >, expand files with >>, after cat

expansions: on - EOF needs to be unquoted

EOF is unquoted => all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.

cat > /etc/nginx/snippets/letsencrypt.conf << EOF
    ssl_certificate /etc/letsencrypt/live/$(hostname)/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/$(hostname)/privkey.pem;
EOF

$(hostname) will be expanded

expansions: off - EOF needs to be quoted

'EOF' is quoted => the lines in the here-document are not expanded

cat >> /etc/nginx/conf.d/seafile.conf << 'EOF'
    location / {
        proxy_set_header   Host $host;
    [...]
    }
EOF

$host will not be expanded

Links

The Grymoire - Sed Tutorial