| Line 1: |
Line 1: |
| | '''BASH Scripting''' | | '''BASH Scripting''' |
| − | == Hello World == | + | ==Hello World== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 7: |
Line 7: |
| | </source> | | </source> |
| | | | |
| − | == Variables == | + | ==Variables== |
| − | === Local Variables === | + | ===Local Variables=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 20: |
Line 20: |
| | echo $HELLO | | echo $HELLO |
| | </source> | | </source> |
| − | === Boolean === | + | ===Boolean=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 30: |
Line 30: |
| | </source> | | </source> |
| | | | |
| − | == Print == | + | ==Print== |
| − | === Print to stderr == | + | ===Print to stderr=== |
| | f_pinta_error () { | | f_pinta_error () { |
| | v_TIMESTAMP=$(date +${v_FORMATO_TIMESTAMP}) | | v_TIMESTAMP=$(date +${v_FORMATO_TIMESTAMP}) |
| | >&2 echo "${v_TIMESTAMP} $1" | | >&2 echo "${v_TIMESTAMP} $1" |
| | } | | } |
| − | == Strings ==
| |
| | | | |
| − | == Loops == | + | ==Strings== |
| − | === While === | + | |
| | + | ==Loops== |
| | + | ===While=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 49: |
Line 50: |
| | </source> | | </source> |
| | | | |
| − | === For === | + | ===For=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 62: |
Line 63: |
| | </source> | | </source> |
| | | | |
| − | === C-like for === | + | ===C-like for=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 70: |
Line 71: |
| | done | | done |
| | </source> | | </source> |
| − | === Loop files === | + | ===Loop files=== |
| | while read p; do echo $p|underscore pretty ; done < /tenkaichi/boo/done/krilin_2017-12-21T12:45:29.544997.jl.boo | | while read p; do echo $p|underscore pretty ; done < /tenkaichi/boo/done/krilin_2017-12-21T12:45:29.544997.jl.boo |
| | | | |
| − | === Loop files in directory === | + | ===Loop files in directory=== |
| − | <source lang="bash">#!/bin/bash
| + | With paths: |
| − | for filename in /Data/*.txt; do | + | <source lang="bash">for filename in /usr/bin/*; do echo $filename; done</source> |
| − | for ((i=0; i<=3; i++)); do
| + | Without path: |
| − | ./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
| + | <source lang="bash">for i in $(ls /usr/bin);do echo $i; done;</source> |
| − | done
| |
| − | done</source> | |
| − | | |
| − | In one line
| |
| − | <source lang="bash">for filename in /media/rafa/ebbb55fb-ef6e-4d19-8474-71f61e77d066/*; do echo $filename; done</source>
| |
| | | | |
| − | === Until === | + | ===Until=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 94: |
Line 90: |
| | </source> | | </source> |
| | | | |
| − | == Conditional == | + | ==Conditional== |
| − | === if === | + | ===if=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 107: |
Line 103: |
| | </source> | | </source> |
| | | | |
| − | === case === | + | ===case=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 142: |
Line 138: |
| | </source> | | </source> |
| | | | |
| − | == User Input == | + | ==User Input== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 154: |
Line 150: |
| | done | | done |
| | </source> | | </source> |
| − | === Password prompt === | + | ===Password prompt=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | read -s -p "Password: " PASSWORD | | read -s -p "Password: " PASSWORD |
| | </source> | | </source> |
| | | | |
| − | == Arrays == | + | ==Arrays== |
| | <source lang="bash"> | | <source lang="bash"> |
| | ARRAY=(one two three) | | ARRAY=(one two three) |
| Line 178: |
Line 174: |
| | </source> | | </source> |
| | | | |
| − | == Functions == | + | ==Functions== |
| | <source lang="bash"> | | <source lang="bash"> |
| | function quit { | | function quit { |
| Line 196: |
Line 192: |
| | fun1 || fun2 will only run fun2 if fun1 returns a 0 value<br /> | | fun1 || fun2 will only run fun2 if fun1 returns a 0 value<br /> |
| | | | |
| − | == Redirections == | + | ==Redirections== |
| − | === stdout 2 file === | + | ===stdout 2 file=== |
| | ls -l > ls-l.txt | | ls -l > ls-l.txt |
| − | === stderr 2 file === | + | ===stderr 2 file=== |
| | grep da * 2> grep-errors.txt | | grep da * 2> grep-errors.txt |
| − | === stdout 2 stderr === | + | ===stdout 2 stderr=== |
| | grep da * 1>&2 | | grep da * 1>&2 |
| − | === stderr 2 stdout === | + | ===stderr 2 stdout=== |
| | grep * 2>&1 | | grep * 2>&1 |
| − | === stderr and stdout 2 file === | + | ===stderr and stdout 2 file=== |
| | rm -f $(find / -name core) &> /dev/null | | rm -f $(find / -name core) &> /dev/null |
| − | == extras == | + | ==Extras== |
| − | === Ping IP range === | + | |
| − | <source lang="bash">#!/bin/bash
| + | ===Killswitch When ssh user logs in=== |
| | + | <syntaxhighlight lang="bash"> |
| | + | #!/bin/bash |
| | + | ME=10.0.253.131 |
| | + | |
| | + | while true; do |
| | + | OTHER=$( who | grep -v tmux | grep -v ${ME} ) |
| | + | if [ -z "${OTHER}" ]; then |
| | + | echo "No one" |
| | + | else |
| | + | tmux kill-session -t test |
| | + | fi |
| | + | sleep 5 |
| | + | done |
| | + | </syntaxhighlight><br /> |
| | + | ===Set files and folder permissions=== |
| | + | <syntaxhighlight lang="bash"> |
| | + | #!/bin/sh |
| | + | # syntax: setperm.s destdir |
| | + | # |
| | + | |
| | + | destdir=/var/www/rrahome |
| | + | |
| | + | dirmode=0770 |
| | + | filemode=0660 |
| | + | |
| | + | find $destdir -type f -exec chmod ${filemode} {} \; |
| | + | find $destdir -type d -exec chmod ${dirmode} {} \; |
| | + | </syntaxhighlight> |
| | + | |
| | + | ===Backup Hard Drive=== |
| | + | <source lang="bash">#!/bin/bash |
| | + | USAGE="./$(basename ${0}) UUID output_filename" |
| | + | BACKUP_ROOT=/mnt/backup |
| | + | |
| | + | if [ -z "${1}" ]; then |
| | + | echo ${USAGE} |
| | + | exit |
| | + | fi |
| | + | if [ -z "${2}" ]; then |
| | + | echo ${USAGE} |
| | + | exit |
| | + | fi |
| | + | |
| | + | HD=$(blkid | grep $1 | awk -F : '{print $1}') |
| | + | ISO=${BACKUP_ROOT}/$(hostname)/$(date '+%Y/%m')/$2.iso |
| | + | |
| | + | dd if=${HD} of=${ISO} bs=1M</source> |
| | + | |
| | + | ===Extract date from string=== |
| | + | <syntaxhighlight lang="bash"> |
| | + | #!/bin/bash |
| | + | DATE=$( echo ${FILE} | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' ) |
| | + | </syntaxhighlight> |
| | + | |
| | + | ===Ping IP range=== |
| | + | <source lang="bash">#!/bin/bash |
| | # cat ping_result.txt | grep ttl | awk '{print $4}' | sed s/:/''/g| sort | uniq | | # cat ping_result.txt | grep ttl | awk '{print $4}' | sed s/:/''/g| sort | uniq |
| | BASE=192.168.0. | | BASE=192.168.0. |
| Line 215: |
Line 267: |
| | ping -c 3 ${BASE}$i >> ping_result.txt | | ping -c 3 ${BASE}$i >> ping_result.txt |
| | done</source> | | done</source> |
| − | === Check if a package is installed === | + | ===Check if a package is installed=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/sh | | #!/bin/sh |
| Line 224: |
Line 276: |
| | debInst terminator | | debInst terminator |
| | </source> | | </source> |
| − | === Check if a file exists === | + | ===Check if a file exists=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | if [ -f $FILE1 ] | | if [ -f $FILE1 ] |
| Line 236: |
Line 288: |
| | </source> | | </source> |
| | | | |
| − | === Check if directory exists === | + | ===Check if directory exists=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 253: |
Line 305: |
| | </source> | | </source> |
| | | | |
| − | === Check user === | + | ===Check user=== |
| − | <source lang="bash"> | + | <syntaxhighlight lang="bash"> |
| | + | #!/bin/bash |
| | + | if [ "$EUID" -ne 0 ] |
| | + | then echo "Please run as root" |
| | + | exit |
| | + | fi |
| | + | </syntaxhighlight><source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| | user=$(whoami) | | user=$(whoami) |
| Line 266: |
Line 324: |
| | fi | | fi |
| | </source> | | </source> |
| − | === Execute localScript on remote server === | + | ===Execute localScript on remote server=== |
| | <nowiki>ssh [user]@[server] 'bash -s' < [local_script]</nowiki> | | <nowiki>ssh [user]@[server] 'bash -s' < [local_script]</nowiki> |
| | | | |
| − | === Remove directorys === | + | ===Remove directorys=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/sh | | #!/bin/sh |
| Line 289: |
Line 347: |
| | ~/myfolder3/$1/thisisafolder | | ~/myfolder3/$1/thisisafolder |
| | EOF | | EOF |
| − | </source> | + | </source><br /> |
| − | === Check if variable is set === | + | ===Check if variable is set=== |
| | if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi | | if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi |
| − | === Check command success === | + | ===Check command success=== |
| | | | |
| − | <source lang="bash">#!/bin/bash
| + | <source lang="bash">#!/bin/bash |
| | ########################################### | | ########################################### |
| | # Pull latest code & # | | # Pull latest code & # |
| Line 334: |
Line 392: |
| | </source> | | </source> |
| | | | |
| − | === Ubuntu proxy setup === | + | ===Ubuntu proxy setup=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #! /bin/bash | | #! /bin/bash |
| Line 361: |
Line 419: |
| | </source> | | </source> |
| | | | |
| − | === Git check version === | + | ===Git check version=== |
| − | <source lang="bash">#!/bin/bash
| + | <source lang="bash">#!/bin/bash |
| | | | |
| | function set_upstream() { | | function set_upstream() { |
| Line 401: |
Line 459: |
| | } | | } |
| | check_latest_version</source> | | check_latest_version</source> |
| − | === Git check for updates and prompt to update === | + | ===Git check for updates and prompt to update=== |
| | <source lang="bash"> | | <source lang="bash"> |
| | #!/bin/bash | | #!/bin/bash |
| Line 443: |
Line 501: |
| | </source> | | </source> |
| | | | |
| − | === Logging === | + | ===Logging=== |
| | | | |
| | | | |
| | For logging, you can wrap sections of your script in curly braces and redirect the stdout to a log file: | | For logging, you can wrap sections of your script in curly braces and redirect the stdout to a log file: |
| − | <source lang=bash> | + | <source lang="bash"> |
| | { | | { |
| | script_command_1 | | script_command_1 |
| Line 455: |
Line 513: |
| | </source> | | </source> |
| | | | |
| − | === Links === | + | ===Links=== |
| | [http://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide] | | [http://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide] |