How to Escape Character when Running SSH Remote Command

Tags: April 8, 2020 4:18 AM
0 comments

Solution of Escaping Character on SSH

The solution is DO NOT escape it. Use cat HEREDOC to build the string of commands and then pass it to SSH.

$ export VARIABLE="FOO BAR"
$ cat <<EOF | ssh user@myserver bash
echo "This is complex command"
echo "Another command that take a $VARIABLE."
sudo mkdir /tmp/foo && echo "SUCCESS"
EOF

If you have a lot of $ dollar signs in your script and do not want local shell to interpret it then use single quote HEREDOC.

$ export VARIABLE_NAME="FOO BAR"
$ cat <<'EOF' | ssh user@myserver bash
export VARIABLE="SSH VAR"
echo "This is complex command"
echo "Another command that take a $VARIABLE name."
sudo mkdir /tmp/foo && echo "SUCCESS"
EOF

On command above the variable $VARIABLE value is "SSH VAR" since it is took the value from remote server not from local machine.

Share on Facebook Twitter

Variable Variables in Shell

Tags: March 10, 2020 8:12 PM
0 comments

Subtitute Variable inside Variable in Bash

Example below is using Bash for variable variables subtitution.

$ hello="Hello World"
$ foobar="hello"
$ echo "${!foobar}"
Hello World

Subtitute Variable inside Variable using eval

This is for other shell which do not recognize "${!}" syntax. It utilise eval so use it with caution.

$ hello="Hello World"
$ foobar="hello"
$ eval echo "\$${foobar}"
Hello World

References for Variable Variables in Shell

Share on Facebook Twitter

Terraform: Force Destroy Resource when prevent_destroy is true

Tags: March 3, 2020 6:55 AM
1 comments

How to Force Destroy Resource in Terraform

Terraform resource that having lifecycle prevent_destroy = true can not be destroyed. You need to manually edit the file inplace and change the value prevent_destroy to false manually each time you want to destroy the resource. Instead of having to edit manually and make git status dirty we can automate this using simple shell script.

Automate Force Destroy Resource in Terraform

The idea is simple.

  1. Search all *.tf files and look the value of prevent_default = true to prevent_default = false
  2. Run terraform destroy command
  3. Revert the changes back to prevent_default = true
Here is the implementation in Bash.
#!/bin/bash

$ find . -name '*.tf' -type f \
-exec perl -i -pe 's@prevent_destroy = true@prevent_destroy = false@g' {} \;

# Run terraform destroy
[ "$IS_PLAN" = "yes" ] && terraform plan -destroy || terraform destroy $@

# Revert the changes
$ find . -name '*.tf' -type f \
 -exec perl -i -pe 's@prevent_destroy = false@prevent_destroy = true@g' {} \;
Save the file with name e.g: terraform-force-destroy.sh. To issue terraform plan -destroy command use the following.
$ IS_PLAN=yes bash terraform-force-destroy.sh
To force destroy the resource use the following command.
$ bash terraform-force-destroy.sh -auto-approve
You can give normal terraform's arguments just like the original terraform destroy.

References for Terraform Force Destroy

Share on Facebook Twitter

Expose module in global scope using Browserify or Webpack

Tags: October 25, 2019 6:05 AM
0 comments

Goal

You want to expose a module as in global scope so it can be called in HTML file. For example we will create a small function for reversing a string. We will expose it as StrReverse.

// File main.js
module.exports = function(str) {
  return str.split('').reverse().join('');
}

Browserify

$ browserify --standalone StrReverse main.js --outfile bundle.js

The key is --standalone parameter.

Webpack

$ webpack-cli --mode=none --output-library StrReverse main.js --output bundle.js

The key is --output-library parameter.

Test in HTML

Create a HTML file and include bundle.js via <script> tag.

<!DOCTYPE html>
<html>
<body>
<script src="bundle.js"></script>
var reversed = StrReverse("Hello World");
document.write(reversed);
</body>
</html>

Share on Facebook Twitter

Compile Swoole Extension on MacOS using Homebrew

Tags: July 29, 2019 10:59 PM
0 comments

What is Swoole

Swoole is Production-Grade Async programming Framework for PHP. It helps you write high-performance asynchronous non-blocking I/O. Similar with Go or NodeJS.

How to Compile using Homebrew

I am using MacOS High Sierra and PHP 7.2.20.

$ export LD_LIBRARY_PATH=$( brew --prefix openssl )/lib
$ export CPATH=$( brew --prefix openssl)/include
$ export PKG_CONFIG_PATH=$( brew --prefix openssl )/lib/pkgconfig
$ pecl install swoole
...
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
enable sockets supports? [no] : yes
enable openssl support? [no] : yes
enable http2 support? [no] : no
enable mysqlnd support? [no] : no
...
... some long message about compiling
...
Build process completed successfully
Installing '/usr/local/Cellar/php@7.2/7.2.20/include/php/ext/swoole/config.h'
Installing '/usr/local/Cellar/php@7.2/7.2.20/pecl/20170718/swoole.so'
install ok: channel://pecl.php.net/swoole-4.4.2
Extension swoole enabled in php.ini

Share on Facebook Twitter

MySQL DISTINCT with Case Sensitive

Tags: March 14, 2019 9:55 AM
0 comments

Goals

We want MySQL distinct to use case sensitive grouping because by default MySQL use case insensitive.

Solution of MySQL DISTINCT case sensitive

We can use binary operator to convert the character set.

SELECT DISTINCT CAST(expr as BINARY)
As an alternative we can just use BINARY.
SELECT BINARY expr

References

Share on Facebook Twitter

Disable Word Wrap on MySQL Shell

Tags: March 8, 2019 8:54 PM
0 comments

Goals

Turn off or disable word wrap on MySQL shell

Solution of Disable Word Wrap on MySQL Shell

We can use external pager such as less to do the job. Pager in MySQL shell actually is a pipe to another program.

mysql> pager less -SFX
PAGER set to 'less -SFX'

That's it. Simple and easy. Now when you have very long output horizontally it will not wrap.

Reference

Share on Facebook Twitter