Difference between revisions of "BASH"
Jump to navigation
Jump to search
Rafahsolis (talk | contribs) |
Rafahsolis (talk | contribs) |
||
| Line 303: | Line 303: | ||
</source> | </source> | ||
| + | === Links === | ||
| + | [http://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide] | ||
Revision as of 14:41, 22 September 2016
BASH Scripting
Hello World
#!/bin/bash
STR="Hello World"
echo $STR
Variables
Local Variables
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
Boolean
#!/bin/bash
the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
echo 'Be careful not to fall off!'
fi
Strings
Loops
While
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
For
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
Example: git pull from all repositories in a pwd
for i in $( ls ); do cd $i; git pull; cd ..; done
C-like for
#!/bin/bash
for i in `seq 1 10`;
do
echo $i
done
Until
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Conditional
if
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expresion evaluated as true
else
echo expresion evaluated as false
fi
case
#!/bin/bash
case $yn in
[YySs]* ) adduser $user; break;;
[Nn]* ) echo no se creo usuario $user; break;;
* ) echo "Elije y/n";;
esac
User Input
#!/bin/bash
while true; do
read -p "Do you wish to install this program?" yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Password prompt
read -s -p "Password: " PASSWORD
Arrays
ARRAY=(one two three)
echo ${ARRAY[*]}
one two three
echo $ARRAY[*]
one[*]
echo ${ARRAY[2]}
three
ARRAY[3]=four
echo ${ARRAY[*]}
one two three four
Functions
function quit {
exit
}
function e {
echo $1
}
e Hello
e World
quit
echo foo
Redirections
stdout 2 file
ls -l > ls-l.txt
stderr 2 file
grep da * 2> grep-errors.txt
stdout 2 stderr
grep da * 1>&2
stderr 2 stdout
grep * 2>&1
stderr and stdout 2 file
rm -f $(find / -name core) &> /dev/null
extras
Check if a package is installed
#!/bin/sh
debInst() {
dpkg-query -Wf'${db:Status-abbrev}' "$1" 2>/dev/null | grep -q '^i'
}
debInst terminator
Check if a file exists
if [ -f $FILE1 ]
then
if [ -f $FILE2 ]
then
echo OK
# MAIN PROGRAM HERE
fi
fi
Check if directory exists
#!/bin/bash
if [ -d /opt/ ]; then
echo "Sí, sí existe."
else
echo "No, no existe"
fi
#-d -» Comprobar si existe determinado directorio
#-f -» Comprobar si existe determinado archivo
#-w -» Comprobar si determinado archivo tiene permisos de escritura
#-x -» Comprobar si determinado archivo tiene permisos de ejecución.
#if not: if [ ! -d /opt/ ]; then
Check user
#!/bin/bash
user=$(whoami)
if [ "$user" == "root" ]; then
isroot=true
echo "Eres Root! :)"
else
isroot=false
echo "No eres root... :'("
exit
fi
Execute localScript on remote server
ssh [user]@[server] 'bash -s' < [local_script]
Remove directorys
#!/bin/sh
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
echo $1 | grep -E -q '^[0-9]+$' || die "Numeric argument required, $1 provided"
while read dir
do
[ -d "$dir" ] || die "Directory $dir does not exist"
rm -rf "$dir"
done <<EOF
~/myfolder1/$1/anotherfolder
~/myfolder2/$1/yetanotherfolder
~/myfolder3/$1/thisisafolder
EOF
Check if variable is set
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
Ubuntu proxy setup
#! /bin/bash
HTTP_PROXY_HOST=proxy.example.com
HTTP_PROXY_PORT=3128
HTTPS_PROXY_HOST=proxy.example.com
HTTPS_PROXY_PORT=3128
gsettings set org.gnome.system.proxy mode manual
gsettings set org.gnome.system.proxy.http host "$HTTP_PROXY_HOST"
gsettings set org.gnome.system.proxy.http port "$HTTP_PROXY_PORT"
gsettings set org.gnome.system.proxy.https host "$HTTPS_PROXY_HOST"
gsettings set org.gnome.system.proxy.https port "$HTTPS_PROXY_PORT"
sudo sed -i.bak '/http[s]::proxy/Id' /etc/apt/apt.conf
sudo tee -a /etc/apt/apt.conf <<EOF
Acquire::http::proxy "http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/";
Acquire::https::proxy "http://$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/";
EOF
sudo sed -i.bak '/http[s]_proxy/Id' /etc/environment
sudo tee -a /etc/environment <<EOF
http_proxy="http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT/"
https_proxy="http://$HTTPS_PROXY_HOST:$HTTPS_PROXY_PORT/"
EOF
Git check for updates and prompt to update
#!/bin/bash
PROJECT_DIR="$HOME/cpc"
PROJECT_PYTHON_INTERPRETER="$HOME/Virtualenvs/cpc/bin/python"
TRACK_BRANCH="MacBookPablo"
TRACK_REPO="origin"
cd $PROJECT_DIR
remote_commit=$(git branch -av | sed s/*/' '/g | grep $TRACK_BRANCH | grep $TRACK_REPO | awk '{print $2}')
echo "Remote commit: $remote_commit"
local_commit=$(git branch -av | sed s/*/' '/g | grep $TRACK_BRANCH | grep -v remotes/ | awk '{print $2}')
echo "Local commit: $local_commit"
django_migrate() {
$PROJECT_PYTHON_INTERPRETER manage.py migrate
}
update() {
echo "Updating code"
git pull $TRACK_REPO, $TRACK_BRANCH
django_migrate
}
if [ "$remote_commit" = "$local_commit" ]; then
echo "Runing latest version"
else
while true; do
read -p "Update available. Do you wish to update? " yn
case $yn in
[YySs]*) update; break;;
[Nn]* ) echo "Skipping update..." ; break;;
* ) echo "Choose y/n";;
esac
done
fi