DCEM's bash script howto: Unterschied zwischen den Versionen

Aus Hackerspace Ffm
Wechseln zu: Navigation, Suche
Zeile 3: Zeile 3:
 
== Code Style ==
 
== Code Style ==
 
I use bashate as lint tool:
 
I use bashate as lint tool:
 +
 
[https://github.com/openstack/bashate bashate]
 
[https://github.com/openstack/bashate bashate]
  
Zeile 9: Zeile 10:
 
=== Indentations ===
 
=== Indentations ===
 
I like to be able to copy and paste parts of a shell script directly to the shell.
 
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.
+
 
 +
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.
 +
 
 +
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
 +
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>

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

create files with >, expand files with ">>" after cat cat > /destination/file.ext << EOF cat > /destination/file.ext << 'EOF' #

'EOF' is quoted => the lines in the here-document are not expanded 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 ‘`’.