User Tools

Site Tools


shell_or_bash_scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
shell_or_bash_scripts [2016/04/28 08:07] skipidarshell_or_bash_scripts [2022/10/14 06:01] (current) – [Double-Dash in Shell commands] skipidar
Line 1: Line 1:
-===== Shell or Bash Scripts =====+====== Shell or Bash Scripts ======
  
-http://www.tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html+|Shebang|Header, which tells where the interpreter is located <code>#!/bin/sh</code>http://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang | 
 +|Sourcing| <WRAP>launch script in a new shell using point with an empty space between sript path Souring:  
 +<code>. path/to/script.sh</code>  
 +vs Execute in current shell: 
 +<code>./path/to/script.sh</code> </WRAP>| http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip |
  
 +===== Guides===== 
  
-== declare Function==+|https://www.shellcheck.net/|Check the syntax of the shell live| 
 +|http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/| | 
 +|https://linuxconfig.org/bash-scripting-tutorial| | 
 +|http://www.tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#chap_11| | 
 +|http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html| | 
 + 
 +===== Executing or Sourcing the script===== 
 + 
 +Sourcing.  
 +Execute in the **CURRENT** shell 
 +<code>. path/to/script.sh</code>  
 + 
 +Executing. 
 +Execute in a **NEW** shell - e.g. cd doesn't modify current shell's path 
 +<code>./path/to/script.sh</code> 
 + 
 + 
 +===== Syntax ===== 
 + 
 +==== declare Function====
 Returning result is done via **echo** Returning result is done via **echo**
  
Line 39: Line 63:
 </code> </code>
  
-== declare Map ==+==== declare Map ====
 Big A!!! Big A!!!
 <code> <code>
Line 50: Line 74:
 </code> </code>
  
-== iterate array keys ==+==== iterate array keys ====
 Access via **${!array[@]}** Access via **${!array[@]}**
  
Line 61: Line 85:
 </code> </code>
  
-== dereference an array / map value ==+==== dereference an array / map value ====
 <code> <code>
 ${ARRAY[2]} ${ARRAY[2]}
Line 68: Line 92:
  
 == If Else == == If Else ==
 +  * Empty spaces after the parenthesis are critical: **[ $a==$b ]**
 +  * 
 +
 <code> <code>
-# check if it is the oot device +# check if it is the root device 
-if [ $device_name=$root_device_name ];+if [ $device_name==$root_device_name ]
 then then
  echo "$device_name is root"  echo "$device_name is root"
 +else
 + echo "NOT root"
 fi fi
 </code> </code>
  
-== Command Substitution ==+==== Command Substitution ====
  
 Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this: Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:
Line 89: Line 118:
 `command` `command`
 </code> </code>
 +
 +
 +==== Input from command line ====
 +If a command expects **a file** as input - one can pass input from STDOUT instead,
 +without storing the output to a file first by using following syntax
 +
 +**oc create -f** expects a file here.
 +
 +<code>
 +oc process -f build/my-build-template.yaml  GITSERVER=$GITSERVER | oc create -f - 
 +oc process -f build/my-build-template.yaml  GITSERVER=$GITSERVER | oc create -f /dev/stdin 
 +</code>
 +
 +
 +==== Double-Dash -- in Shell commands ====
 +A double-dash in a shell command signals the end of options and disables further option processing.
 +https://www.baeldung.com/linux/double-dash-in-shell-commands
 +
 +
 +<sxh shell>
 +# If we use the same approach, we'll receive an error:
 +# That's because grep treats "--hello" as a multi-character command option.
 +grep "--hello" data.txt
 +
 +#works
 +grep -- --hello data.txt
 +</sxh>
shell_or_bash_scripts.1461830826.txt.gz · Last modified: (external edit)