Skip to content

Instantly share code, notes, and snippets.

@paveltimofeev
Last active October 18, 2017 13:06
Show Gist options
  • Save paveltimofeev/f4426538d9f01285a4d7 to your computer and use it in GitHub Desktop.
Save paveltimofeev/f4426538d9f01285a4d7 to your computer and use it in GitHub Desktop.
Bash hints, tips & tricks

Bash hints, tips & tricks

Exit on Error

set -e
set -o pipefail

# -e (errexit): Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
# In case script is calling subscript inside, the pipefail option can be useful.
# -o pipefail: Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.

Disk space:

du -sh /* 2> /dev/null

HERE dir

HERE=$(dirname `which $0`)
echo "Current directory is $HERE"

Another one HERE

pushd `dirname $0` > /dev/null
HERE=`pwd -P`
popd > /dev/null
echo "Current directory is $HERE"

Terminate all NODEJS PROCESSES

sudo kill `ps aux | grep node | awk '{print $2}'`

wait until host become available

url="my.hostname.com"
echo "Waiting for $url"
while ! curl --max-time 3 http://$url
do
    sleep 1
done

wait until host become available + timeout

function wait_url_return_200()
{
  URI=$1
  TIMEOUT=30
  ATTEMP=0
  while [ $(curl --write-out %{http_code} --silent --output /dev/null $URI) != "200" ]
  do
      ATTEMP=$((ATTEMP + 1))
      echo "   wait $URI become available... $ATTEMP/$TIMEOUT"
      if [ "$ATTEMP" == "$TIMEOUT" ]; then 
        echo "Timeout Error"
        return 1;
      fi
      sleep 1
  done
  return 0;
}

# Usage:
wait_url_return_200 https://g00gle.com
echo "Res $?";

Check HTTP response code

URI=https://www.wikipedia.org/
RES=$(curl --write-out %{http_code} --silent --output /dev/null $URI)

if [ "200" == "${RES}" ];
then
  echo "Successfully $RES"
else
  echo $RES
fi

Wait until HTTP 200 OK

while [ $(curl --write-out %{http_code} --silent --output /dev/null $URI) != "200" ]
do
    echo "wait..."
    sleep 1
done

Check if current user has root

if [ $(id -u) -ne 0 ] ; then
	echo "You must be root to run this install"
	exit 1
fi

Redirect output to log-file (script.log) and to console

bash script.sh 2>&1 | tee script.log

Redirect output to console (tail follow)

tail -f /var/log/syslog
tail -F /var/log/syslog   # for files under rotation

Redirect typed commands into the file

history 1> commands.log
history 5 1> commands.log		# last 5 comands
history | grep docker 1> commands.log	# *docker* commands

Read file and post lines to Elasticsearch

ES=0.0.0.0:9200

curl -XDELETE "http://$ES/pint/"

for e in ./data/*.elastic ; do
  echo "$e"
  curl -s -XPOST http://$ES/_bulk --data-binary "@$e" > /dev/null
done

Allow all java applications use port lower 1024

sudo su
setcap 'cap_net_bind_service=+ep' `readlink -f $(which java)`

Add github.com to known_hosts

touch /home/ubuntu/.ssh/known_hosts
ssh-keyscan github.com >> /home/ubuntu/.ssh/known_hosts

Allow to use sha1 for ssh / Add line to file if it not exists

file=/etc/ssh/sshd_config
val=KexAlgorithms=diffie-hellman-group1-sha1
sudo grep -q -F $val $file || sudo echo $val >> $file # Add line to file if it not exists
sudo systemctl restart sshd

Execute remotely via ssh (unsafe)

lab=10.10.10.10
sshKey=....pem

ssh -i $sshKey -o StrictHostKeyChecking=no ubuntu@$lab <<'ENDSSH'

sudo su
file=/etc/ssh/sshd_config
val=KexAlgorithms=diffie-hellman-group1-sha1
grep -q -F $val $file || echo $val >> $file
systemctl restart sshd

ENDSSH

Parallel run of commands

echo arg1 arg2 arg3 | xargs -n 1 -P 3 bash ./build.sh

Equivalent of next code run in parallel:

bash ./build.sh arg1
bash ./build.sh arg2
bash ./build.sh arg3

Any number of parallel tasks:

TASKS=(arg1 arg2 arg3)
echo ${TASKS[*]} | xargs -n 1 -P ${#TASKS[*]} bash ./build.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment