This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| scripting:bash [2018/09/24 20:44] – jotasandoku | scripting:bash [2023/11/02 14:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | **BASH** | + | BASH CONDITIONS: |
| - | This is an example of a very simple script: | + | # run ssh agent in /etc/bashrc |
| - | https://docs.google.com/document/d/1uNUGXkHmb9jvHA4Y12nTXEDSOjkLzzVoAcHkSVwxNmw/edit? | + | if [ ! -S ~/.ssh/ |
| + | eval `ssh-agent` | ||
| + | ln -sf " | ||
| + | fi | ||
| + | ssh-add -l > /dev/null || ssh-add | ||
| + | #run ssh agent | ||
| + | ssh-add ~/.ssh/* >/dev/null 2>&1 | ||
| + | __EXAMPLE OF ssh/config FILE__:\\ | ||
| + | $ cat .ssh/ | ||
| + | Host * | ||
| + | AddKeysToAgent yes | ||
| + | # | ||
| + | ServerAliveCountMax 6 | ||
| + | ServerAliveInterval 10 | ||
| + | TCPKeepAlive yes | ||
| + | ForwardAgent yes | ||
| + | Compression yes | ||
| + | GatewayPorts yes | ||
| + | StrictHostKeyChecking no | ||
| + | User jaime.santos.amandi | ||
| + | | ||
| + | ## This is saying for anything for those IP ranges or for domains | ||
| + | ## use socks proxy over that host: bastion-lhr.myorgdatacloud.com | ||
| + | Host 10.32.* 10.33.* *.myorgdatacloud.com *.bigshot.co.uk !bastion-62-* | ||
| + | ProxyCommand ssh bastion-lhr.myorgdatacloud.com -W %h:%p | ||
| - | http:// | ||
| - | Flow control: http:// | ||
| - | http:// | ||
| - | $# represents the number of arguments: | + | ---- |
| - | \\ | + | |
| - | \\ | + | |
| - | **OR & AND OPERATORS FOR PROCESS FLOW "&& | + | |
| - | The right side of && will only be evaluated if the exit status of the left side is zero. || is the opposite: it will evaluate the right side only if the left side exit status is nonzero. You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise. | + | |
| - | (ip route add 10.0.0.0/8 via 10.20.3.5 dev eth0 ; echo ' | + | |
| - | ping -c 1 $router_ip &>/ | + | |
| - | \\ | + | BASH CONDITIONS:\\ |
| - | IF | + | |
| - | Comparisons: | + | |
| - | * eq equal to | + | |
| - | * ne not equal to | + | |
| - | * lt less than | + | |
| - | * le less than or equal to | + | |
| - | * gt greater than | + | |
| - | * ge greater than or equal to | + | |
| - | File Operations: The following flags (preceded by minus), represent :\\ | + | |
| - | * s file exists and is not empty-f file exists and is not a directory | + | |
| - | * d directory exists | + | |
| - | * x file is executable | + | |
| - | * w file is writable | + | |
| - | * r file is readable | + | |
| - | \\ | + | |
| The tests below are test conditions provided by the shell: | The tests below are test conditions provided by the shell: | ||
| Line 58: | Line 59: | ||
| * G file = True if the file exists and is owned by the effective group id. | * G file = True if the file exists and is owned by the effective group id. | ||
| \\ | \\ | ||
| - | * | + | |
| * file1 –nt file2 = True if file1 is newer, by modification date, than file2. | * file1 –nt file2 = True if file1 is newer, by modification date, than file2. | ||
| * file1 ot file2 = True if file1 is older than file2. | * file1 ot file2 = True if file1 is older than file2. | ||
| Line 69: | Line 70: | ||
| * expr1 –a expr2 = True if both expr1 and expr2 are true. | * expr1 –a expr2 = True if both expr1 and expr2 are true. | ||
| * expr1 –o expr2 = True is either expr1 or expr2 is true. | * expr1 –o expr2 = True is either expr1 or expr2 is true. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | This is an example of a very simple script: | ||
| + | https:// | ||
| + | |||
| + | |||
| + | http:// | ||
| + | |||
| + | Flow control: http:// | ||
| + | |||
| + | http:// | ||
| + | |||
| + | $# represents the number of arguments: | ||
| + | \\ | ||
| + | \\ | ||
| + | **OR & AND OPERATORS FOR PROCESS FLOW "&& | ||
| + | The right side of && will only be evaluated if the exit status of the left side is zero. || is the opposite: it will evaluate the right side only if the left side exit status is nonzero. You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise. | ||
| + | (ip route add 10.0.0.0/8 via 10.20.3.5 dev eth0 ; echo ' | ||
| + | ping -c 1 $router_ip &>/ | ||
| + | |||
| + | \\ | ||
| + | IF | ||
| + | Comparisons: | ||
| + | * eq equal to | ||
| + | * ne not equal to | ||
| + | * lt less than | ||
| + | * le less than or equal to | ||
| + | * gt greater than | ||
| + | * ge greater than or equal to | ||
| + | |||
| + | File Operations: The following flags (preceded by minus), represent :\\ | ||
| + | * s file exists and is not empty-f file exists and is not a directory | ||
| + | * d directory exists | ||
| + | * x file is executable | ||
| + | * w file is writable | ||
| + | * r file is readable | ||
| + | |||
| ---- | ---- | ||
| Line 82: | Line 136: | ||
| **IN CLI (SINGLE LINE) - One-liner** | **IN CLI (SINGLE LINE) - One-liner** | ||
| + | |||
| + | for s in ALL_SERVERS ; do ssh $s '/ | ||
| + | |||
| + | Apply to multiple hosts with no need for a script: | ||
| + | for i in admin-worker00.dc.mycompany1.co.uk \ | ||
| + | centosGUI.dc.mycompany1.co.uk \ | ||
| + | dbtest.dc.mycompany1.co.uk \ | ||
| + | redis-admin.dc.mycompany1.co.uk \ | ||
| + | security-scanner01.dc.mycompany1.co.uk \ | ||
| + | test.dc.mycompany1.co.uk \ | ||
| + | test01.dc.mycompany1.co.uk ; \ | ||
| + | do ssh root@" | ||
| + | | ||
| + | |||
| conns=`ps aux|grep java|egrep ' | conns=`ps aux|grep java|egrep ' | ||
| Line 92: | Line 160: | ||
| **EXECUTE SSH COMMAND IN A SET OF DEVICES FOR LOOP**:\\ | **EXECUTE SSH COMMAND IN A SET OF DEVICES FOR LOOP**:\\ | ||
| for host in $(seq --format=' | for host in $(seq --format=' | ||
| + | |||
| + | |||
| ---- | ---- | ||
| + | |||
| **FOR**\\ | **FOR**\\ | ||
| Line 104: | Line 175: | ||
| To automatically **generate configurations** (configuration generator) from a single line in the CLI. See this example: | To automatically **generate configurations** (configuration generator) from a single line in the CLI. See this example: | ||
| for i in {1..6}; do echo -e "conf t \n int fa0/0 \n ipv6 address 2001: | for i in {1..6}; do echo -e "conf t \n int fa0/0 \n ipv6 address 2001: | ||
| + | for f in *.HEIC; do echo " | ||
| #!/bin/bash | #!/bin/bash | ||
| Line 110: | Line 182: | ||
| echo -e " | echo -e " | ||
| Host $val1 | Host $val1 | ||
| - | | + | |
| User jaime_santos | User jaime_santos | ||
| Port 22"; | Port 22"; | ||
| Line 158: | Line 230: | ||
| ---- | ---- | ||
| + | **BASH IDIOMS:**\\ | ||
| + | Redirection ( stdout , stderr ). All files have an id (descriptor) but also! stdout1 , stderr2 habe ids!. | ||
| + | cat foo.txt 1> output.txt | ||
| + | cat foo.txt 2> output.txt | ||
| + | ls foo > /dev/null 2>& | ||
| + | foo.txt > output.txt 2>& | ||
| + | > /dev/null 2>& | ||
| + | > /dev/null 2>& | ||
| + | |||
| + | |||
| + | cat << EOF > file.txt | ||
| + | [text] | ||
| + | EOF | ||
| + | |||
| + | This is an interesting way of generating a json file with a bash script using EOF (inside an ansible facts file): | ||
| + | #!/bin/bash | ||
| + | user=$(who | cut -f 1 -d " " | tail -1 ) | ||
| + | ipuser=$(who -u | cut -f 2 -d " | ||
| + | | ||
| + | cat << EOF | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | | ||
| - | Output redirection | ||
| - | $ /dev/null | ||
| \\ | \\ | ||
| Increment variable | Increment variable | ||
| Line 168: | Line 263: | ||
| num=`expr $num1 + $num2` | num=`expr $num1 + $num2` | ||
| \\ | \\ | ||
| + | |||
| + | ---- | ||
| **REGULAR EXPRESSIONS** | **REGULAR EXPRESSIONS** | ||
| Line 283: | Line 380: | ||
| **TO EVALUATE COMMANDS REMOTELY (SSH)** | **TO EVALUATE COMMANDS REMOTELY (SSH)** | ||
| - | ssh root@jaguar27.dc.grapeshot.co.uk "/ | + | ssh root@jaguar27.dc.mycompany1.co.uk "/ |
| | | ||