DCEM's bash script howto: Unterschied zwischen den Versionen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche
(using here-documents)
Zeile 25: Zeile 25:
 
To create or append to a config file i like to use here-documents.
 
To create or append to a config file i like to use here-documents.
  
create files with <code style="white-space: nowrap">></code>, expand files with ">>" after cat
 
<code>cat > /destination/file.ext << EOF
 
cat > /destination/file.ext << 'EOF' #
 
  
'EOF' is quoted => the lines in the here-document are not expanded
+
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 ‘`’.  
 
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 ‘`’.  
 +
<code>
 +
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
 +
</code>
 +
<code style="white-space: nowrap">$(hostname)</code> will be expanded
  
</code>
+
==== expansions: off - EOF needs to be quoted ====
 +
'EOF' is quoted => the lines in the here-document are not expanded
 +
<code>
 +
cat >> /etc/nginx/conf.d/seafile.conf << EOF # 'EOF' is quoted => expansions: off
 +
    location / {
 +
        proxy_set_header  Host $host;</code>
 +
    [...]
 +
    }
 +
EOF
 +
</code>
 +
<code style="white-space: nowrap">$host</code> will not be expanded

Version vom 8. Januar 2020, 13:20 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.

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 # 'EOF' is quoted => expansions: off

   location / {
       proxy_set_header   Host $host;
   [...]
   }

EOF </code> $host will not be expanded