Bash reminder

I’m currently doing so much Bash scripting that I always end up googling the stuff I should know by now so it’s time for me to write it all down once and for all.

Looping through a text file

Sometimes you build up a list that you want to process, let’s say you want to wget a few URLs.

for read line; do
  wget "${line}"
done < ./urls.txt

It’s obviously a good idea to validate inputs but for the sake of this post it’s not necessary.

Redirecting STDERR

Redirecting to a file and just redirecting to STDOUT can be done two ways. I always forget the syntax so it’s always good to be reminded of it.

curl "${URL}" 2>&1

# STDERR and STDOUT to file
curl "${URL}" &> ./out.log

EOF syntax

In case you want to forward  a multi-line string to a variable you can invoke cat with the EOF syntax:

cat <<EOF
Hello World!
Why So Serious?

The syntax can actually be a bit tricky since you define on the first line the keyword that will end the multi-line string, in this case EOF.

Mounting a RAM disk

RAM disks are quick and ideal if you need a fast scratch disk, they can be mounted to any folders just like any other kind of device:

mkdir /mnt/ram
mount -t tmpfs tmpfs /mnt/ram -o size=4096M

You can now read and write on your RAM disk through /mnt/ram.

SSH tunnel

When you want to access services that are not available outside of a network you can open an SSH tunnel, which is like a VPN but not totally.

ssh -L 13306:

Here we bind the local port 13306 to the remote port 3306 through our localhost, we can then access a remote MySQL server through the SSH folder since there’s not good reason to have a MySQL server open to access on the internet.
Let’s manage ou MySQL server now:

mysql -h -P 13306

Keep in mind that even though you can use this SSH command to proxy your network traffic, you will most certainly not achieve better performances than a standard VPN or OpenVPN setup.

Published by


Junk food tastes good.