Skip to content

Instantly share code, notes, and snippets.

View mikybars's full-sized avatar
🥛

Miguel Ibars mikybars

🥛
  • Madrid
  • 06:05 (UTC +02:00)
View GitHub Profile
@mikybars
mikybars / remotetar.sh
Last active February 8, 2019 14:59
Nice one-liner when you want to extract a file from a tarball hosted somewhere but you don't want to deal with temporary files. File is copied to the target directory with path stripped.
$ URL="https://github.com/sharkdp/bat/releases/download/v0.9.0/bat-v0.9.0-x86_64-unknown-linux-musl.tar.gz"
$ TARPATH="$(basename $URL .tar.gz)/bat"
$ TO=~/bin
$ curl -fsSL $URL | tar xzf - -C $TO --transform="s,$(dirname $TARPATH)/,," $TARPATH
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
@mikybars
mikybars / ex.sh
Last active February 15, 2019 23:55
We have two options here: * `sed` is a powerful **stream** editor although it also features a handy in-place/`-i` flag that may modify the source file. Unfortunately this is a non-standard FreeBSD extension and therefore results in unportable code.
# Replace the shell executing the script
$ ex - '+g/^#!/s,bash,zsh,' -cwq install.sh
# Comment all non-commentary lines
$ ex - '+g/^[[:space:]]*[^#]/s/^/#/' -cwq test.sh
public static boolean isMyChannelId(Long employeeId, String channelId) {
assert(employeeId != null);
Matcher pattern = Pattern
.compile("user_(?<user1>[0-9]+)_user_(?<user2>[0-9]+)")
.matcher(channelId);
if (pattern.matches()) {
long user1 = Long.parseLong(pattern.group("user1"));
long user2 = Long.parseLong(pattern.group("user2"));
@mikybars
mikybars / colors.sh
Last active February 8, 2019 14:24
Colors and other style attributes on the terminal work by sending ANSI escape sequences that are encoded in a special way. To abstract from those codes, that may vary from terminal to terminal, we can use the utility [tput](http://linuxcommand.org/lc
$ RED=$(tput setaf 1)
$ GREEN=$(tput setaf 2)
$ RESET=$(tput sgr0)
$ echo "This is ${RED}red${RESET} and this is ${GREEN}green${RESET}"
$ echo $TERM
xterm-256color
$ infocmp $TERM
# Reconstructed via infocmp from file: /Users/miguel/.terminfo/78/xterm-256color
echo -en "Directory ~/bin already exists, overwrite it? [Y/n]: "
read -n 1 action; echo
case "$action" in
'') ;&
[Yy]) rm -rf ~/bin;;
*) echo "Skipping directory ~/bin";;
esac
$ docker image inspect -f 'CMD {{.Config.Cmd}}, ENTRYPOINT {{.Config.Entrypoint}}' postgres:9.4
CMD [postgres], ENTRYPOINT [docker-entrypoint.sh]
@mikybars
mikybars / ex.sh
Last active February 18, 2019 23:31
sed & ex examples showcasing use cases of regex syntax for group capturing & backreferencing
# Lowercase all html tags
$ ex - '+%s,<\(.*\)>,<\L\1>,' -cwq index.html
@mikybars
mikybars / docker_stdin.sh
Last active October 10, 2019 11:10
Feed input to a Docker container
$ docker container run -i db psql postgres -U postgres <<EOF
CREATE TABLE employee (id INTEGER PRIMARY KEY, name VARCHAR);
INSERT INTO employee VALUES (1, 'Miguel');
INSERT INTO employee VALUES (2, 'Javier');
EOF
addons:
ssh_known_hosts: <deploy-host>
before_deploy:
- openssl aes-256-cbc -K $encrypted_<...>_key -iv $encrypted_<...>_iv -in deploy_rsa.enc -out /tmp/deploy_rsa -d
- eval "$(ssh-agent -s)"
- chmod 600 /tmp/deploy_rsa
- ssh-add /tmp/deploy_rsa