Bash Script Knowledge Base: Unterschied zwischen den Versionen

Aus MattWiki
Die Seite wurde neu angelegt: „This article holds some notes about scriping in bash.             STR="Hello World!"             echo $STR    == General == A bash script starts in the first line with: #!/bin/bash == Variables == Define variable MY_STR: HW="Hello World!" Read content of the variable and put it to the console: echo $HW == Check if Bash Script Received Parameters == Check if any arguments were passed to the script: if [ $# -eq 0 ] then echo "No…“
 
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
This article holds some notes about scriping in bash.
This article holds some notes about scriping in bash.
            STR="Hello World!"
            echo $STR   
== General ==
== General ==
A bash script starts in the first line with:
A bash script starts in the first line with:

Version vom 12. Mai 2025, 21:39 Uhr

This article holds some notes about scriping in bash.

General

A bash script starts in the first line with:

#!/bin/bash

Variables

Define variable MY_STR:

HW="Hello World!"

Read content of the variable and put it to the console:

echo $HW

Check if Bash Script Received Parameters

Check if any arguments were passed to the script:

if [ $# -eq 0 ] then
  echo "No arguments received"
fi

$# contains how many arguments were passed to the script.

Check if first argument was empty:

if [ -z "$1" ] then
  echo "Argument 1 empty"
fi

The -z switch will test, if the argument passed is empty. If it is empty, then the expression resolves to true and the body of the if statement is executed.