Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Disable Homebrew Auto Update

Tags: November 24, 2020 8:10 AM
0 comments

Homebrew will do self update when we install a package. Sometimes this is not what we want. We are okay using an old version of Homebrew and some old packages.

To prevent Homebrew doing automatic update just set an environment variable named `HOMEBREW_NO_AUTO_UPDATE`.

$ HOMEBREW_NO_AUTO_UPDATE=1 brew install [PACKAGE]
<link crossorigin='anonymous' href='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shCoreDefault.min.css' integrity='sha256-+6BtzNuRjIOAeRxs56BdQS0n2/5w5HLMEoJsXdMP5TI=' rel='stylesheet'/>

References

Share on Facebook Twitter

How to Kill Background Child Process in Bash

Tags: July 13, 2020 12:52 PM
0 comments

Terminate Background Child Process in Bash

In Bash by child process that sent into background is still alive when main program is terminated. Take a look an example below.

#!/bin/bash

# Run a Python web server
python -m http.server src/ &

# Run SASS watcher and compiler...
sass --watch src/scss:src/css &

wait
When we run script above and terminate using CTRL+C Python and SASS are still running.

Solution to terminate Background Child Process in Bash

The solution to above problem is using shell built-in trap command.

#!/bin/bash

# Kill all child process (Python and SASS) when exit
trap "kill 0" EXIT

# Run a Python web server
python -m http.server src/ &

# Run SASS watcher and compiler...
sass --watch src/scss:src/css &

wait

Now when the script is exited all the child process even which has been sent as background process will also terminated.

References

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

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

Curl Dump Response Headers to STDOUT and Ignore Response Body

Tags: May 7, 2018 6:48 AM
0 comments

Goals

Return only HTTP response header when opening a web page using curl. This is useful when we are interested in processing response headers only.

Command

We will utilize /dev/stdout and /dev/null to achieve what we want.

$ curl https://notes.rioastamal.net -D /dev/stdout -o /dev/null --silent
HTTP/2 200
date: Sun, 06 May 2018 23:42:37 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=d6347d57f364b276150034b241b19cdb01525650157; expires=Mon, 06-May-19 23:42:37 GMT; path=/; domain=.rioastamal.net; HttpOnly
expires: Sun, 06 May 2018 23:42:37 GMT
cache-control: private, max-age=0
last-modified: Sun, 06 May 2018 23:42:05 GMT
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 416f4deb6f51a320-HKG

Reference

Share on Facebook Twitter

Generate Random String in Shell Using /dev/urandom

Tags: May 3, 2018 8:18 AM
0 comments

Goals

Generate random string in Shell and using /dev/urandom as the source. This random string typically useful to be used as encryption key.

Implementation

We will use combination of tr and head to generate 32 random characters. Command below will only output alphanumeric and some characters symbol only.

$ </dev/urandom tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>?@[\]^_`{|}~' | head -c 32 && echo
}s9s2c8W7aZlI:yg<{bg&-<7YnyJEk.u

On Mac OS X system you may need to define LC_ALL=C environment variable as shown below.

$ LC_ALL=C </dev/urandom tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>?@[\]^_`{|}~' | head -c 32 && echo
f(s_TPj*.H3Z/s[*:zLe[=9&0$FF"*8[

References

Share on Facebook Twitter

Redis: How to Increase File Descriptor Limits

Tags: November 25, 2016 8:06 PM
1 comments

Problem

When you run redis server it complains can not set maximum open files because it has reached the OS max file descriptor limits. Here is the sample output.

$ ./bin/redis-server
28436:C 25 Nov 20:10:03.978 # Warning: no config file specified, using the default config. In order to specify a config file use ./bin/redis-server /path/to/redis.conf
28436:M 25 Nov 20:10:03.979 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
28436:M 25 Nov 20:10:03.979 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
28436:M 25 Nov 20:10:03.979 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
[...CUT...]

When you try to increase the maximum file descriptor using ulimit as root by issuing sudo it returns an error.

$ sudo ulimit -n 65000
sudo: ulimit: command not found

Wow, WTF is that? ulimit is a shell built so giving sudo an instruction to run a command called ulimit will not work. It will the same as statement below.

Share on Facebook Twitter

Quickest Way: Using STDIN and Pipe to Copy SSH Public Key to Server

Tags: July 27, 2016 8:13 PM
0 comments

Goal

Copy SSH public key to another machine without using external tools such as ssh-copy-id - Only pure shell built-in or at least standard commands.

Solution

The solution is using shell STDIN and PIPE it to ssh.

$ cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys -'
The quote for the ssh arguments is important because without it the redirection will goes to your local machine instead of remote machine. The "-" at the last of cat command on the remote indicate it reads the input from STDIN.

Reference

Share on Facebook Twitter

Reset All Tables Except Migration Table on MySQL

Tags: April 15, 2016 9:29 PM
0 comments

Overview

When developing an application there is case when you need to clear all the data in your database e.g: testing the seeding or such. But you want to exclude some tables let say migration table which used by the application framework to migrate the schema.

Problem

You want to clean up data on all the tables, except the migration table because you don't want to re-run the schema migration.

Solution

  1. Get list of tables
  2. Exclude the migration table
  3. Append the prefix 'DELETE FROM ' to each line
  4. Append the suffix ';' to each line
  5. Pipe the result to MySQL
$ echo "SHOW TABLES;" | mysql -N DB_NAME | grep -v orb_migration | sed -e 's/^/DELETE FROM /' -e 's/$/;/' | mysql DB_NAME
In case above the table which excluded is orb_migration.

Share on Facebook Twitter

Bash Trim Whitespace

Tags: May 2, 2014 10:45 AM
1 comments

To remove leading and trailing whitespace from a subset of strings in a shell we can use sed.

$ echo '   #FOO#   ' | sed -e 's/\s*$//' -e 's/^\s*//'
#FOO#

Function 'trim'

To reuse it in another place, it is good idea to wrap it as a function.
# Function to trim leading and trailing spaces
trim() {
  # Accept input from argument or STDIN
  # So you can do both:
  # $ echo '  #FOO#   ' | trim
  # or
  # $ trim '   #FOO#   '
  local STRING=$( [ ! -z "$1" ] && echo $1 || cat ; )
  
  echo "$STRING" | sed -e 's/^\s*//' -e 's/\s*$//'
}
Now it can be used to trim a string both from argument or standard input.
$ echo '   #FOO#   ' | trim
#FOO#

$ trim '   #FOO#   '
#FOO#

Reference

Share on Facebook Twitter

Shared Library Extractor

Tags: January 5, 2013 9:38 AM
0 comments

Untuk mempermudah penyalinan file-file library yang akan digunakan pada lingkungan chroot, penulis menggunakan shell script berikut.

#!/bin/bash
#
################################################################################
# Shell Script untuk melakukan otomasi penyalinan file-file yang diperlukan    #
# oleh sebuah binary/program.                                                  #
#                                                                              #
# @author Rio Astamal <me@rioastamal.net>                                      #
################################################################################
#
ACTION=$1
JAIL_FILE=$2
JAIL_ROOT=$3
LIST_FILES=

# cek apakah file ada
if [ ! -e $JAIL_FILE ]; then
 echo "Error: file '${JAIL_FILE}' tidak ditemukan."
 exit 2
fi

# cek apakah direktori target jail ada
if [ ! -d $JAIL_ROOT ]; then
 echo "Error: direktori jail '${JAIL_ROOT}' tidak ditemukan."
fi

show_help() {
 echo "Penggunaan: $0 [OPTIONS] [JAIL_FILE] [JAIL_ROOT]"
 echo ""
 echo "Dimana OPTIONS:"
 echo "  file - Mencetak daftar library."
 echo "  dir - Mencetak nama direktori dari daftar library."
 echo "  run - Melakukan penyalinan ke direktori JAIL_ROOT."
 echo ""
 echo "Contoh:"
 echo "$0 dir /usr/bin/nginx /opt/jail"
 echo ""
}

# Fungsi untuk mencetak daftar file library yang dibutuhkan oleh file binary
# yang ingin di-jail.
show_files() {
 LIST_FILES=`ldd $JAIL_FILE | awk '{print $3}' | grep ^/`
 LD_LINUX=`ldd $JAIL_FILE | grep ld-linux | awk '{print $1}'`
 
 for f in $LIST_FILES
 do
  echo $f
 done
 echo $LD_LINUX
}

# Fungsi untuk mencetak nama direktori dari setiap file library
show_directories() {
 LIST_FILES=`ldd $JAIL_FILE | awk '{print $3}' | grep ^/`
 LD_LINUX=`ldd $JAIL_FILE | grep ld-linux | awk '{print $1}'`
 
 for f in $LIST_FILES
 do
  dirname $f
 done
 dirname $LD_LINUX
}

if [ "$ACTION" == "file" ]; then
 show_files
elif [ "$ACTION" == "dir" ]; then
 show_directories
elif [ "$ACTION" == "run" ]; then
 # buat direktori tujuan dulu sebelum melakukan copy file
 for folder in `show_directories`
 do
  mkdir -p "${JAIL_ROOT}${folder}"
 done
 
 # copy setiap library
 for f in `show_files`
 do
  TARGET="${JAIL_ROOT}${f}"
  echo -n "Copying $f to ${TARGET}..."
  cp $f "${JAIL_ROOT}${f}" 2>/dev/null
  
  if [ $? -eq 0 ]; then
   echo "done."
  else
   echo "failed."
  fi
 done
fi

Simpan dengan nama ldd-extractor.sh lalu beri atribut +x.

Contoh Penggunaan

Melihat daftar shared library dari program "ncat".

# ldd-extractor file /usr/bin/ncat 
/lib/i686/cmov/libssl.so.0.9.8
/lib/i686/cmov/libcrypto.so.0.9.8
/usr/lib/libpcap.so.0.8
/lib/tls/i686/cmov/libdl.so.2
/lib/tls/i686/cmov/libc.so.6
/lib/libz.so.1
/lib/ld-linux.so.2

Melakukan penyalinan ke jail direktori yang telah ditentukan.

# ldd-extractor run /usr/bin/ncat /opt/jail
Copying /lib/i686/cmov/libssl.so.0.9.8 to /opt/jail/lib/i686/cmov/libssl.so.0.9.8...done.
Copying /lib/i686/cmov/libcrypto.so.0.9.8 to /opt/jail/lib/i686/cmov/libcrypto.so.0.9.8...done.
Copying /usr/lib/libpcap.so.0.8 to /opt/jail/usr/lib/libpcap.so.0.8...done.
Copying /lib/tls/i686/cmov/libdl.so.2 to /opt/jail/lib/tls/i686/cmov/libdl.so.2...done.
Copying /lib/tls/i686/cmov/libc.so.6 to /opt/jail/lib/tls/i686/cmov/libc.so.6...done.
Copying /lib/libz.so.1 to /opt/jail/lib/libz.so.1...done.
Copying /lib/ld-linux.so.2 to /opt/jail/lib/ld-linux.so.2...done.
Referensi:

Share on Facebook Twitter

How to Count String Length using Shell

Tags: April 2, 2012 7:14 AM
0 comments

Bash Variable: The POSIX Way

$ MYSTRING="Speed of Light"
$ echo ${#MYSTRING}
14

Using wc

$ echo -n "Speed of Light"
14

Using expr

This is not POSIX complaint.

$ expr length "Speed of Light"
14

Share on Facebook Twitter

Mencari String pada File melalui Shell

Tags: March 11, 2012 10:45 AM
0 comments

Perintah yang dapat digunakan.

find [DIRECTORY] -exec grep -H -n [STRING] {} \;

Contoh

Contoh berikut akan mencari letak dimana file dari fungsi wp_login berada. Pencarian dilakukan pada direktori distribusi wordpress yang telah diextract.

Share on Facebook Twitter

Melihat Daftar Paket yang Dapat Diupgrade pada Debian/Ubuntu

Tags: March 10, 2012 6:37 PM
0 comments

Perintah ini digunakan untuk melihat daftar paket yang dapat diupgrade pada distro Debian/Ubuntu. Kuncinya adalah pemberian opsi -s yang artinya hanya merupakan proses simulasi.

# apt-get update
# apt-get upgrade -s
Atau full OS (distribution) upgrade.
# apt-get dist-upgrade -s

Share on Facebook Twitter