Skip to content

Instantly share code, notes, and snippets.

@biiont
biiont / iterate_shell_array.sh
Last active February 7, 2023 18:00 — forked from anonymous/iterate_shell_array.sh
Iterate over an array using POSIX Shell
#!/bin/sh
# Iterate over an array using POSIX Shell
# Initial data
ARR="a:b:c:d"
# Iteration. Do whatever is needed instead of 'echo "$VAL"'.
CDR="${ARR}:"; while [ -n "$CDR" ]; do CAR=${CDR%%:*}; echo "$CAR"; CDR=${CDR#*:}; done; unset CAR CDR
# IMPORTANT!!! Add semicolon to the end of an array (IT="${ARR}:") to make stop condition working.
@biiont
biiont / ping_with_mtu.sh
Created October 1, 2015 08:20
Determining MTU With Ping
# This script allows to determine MaxTranssmitUnit size for current network.
#
# Argument "-M" selects Path MTU Discovery strategy: - "do" means prohibit
# fragmentation, even local one; - "want" do PMTU discovery, fragment locally
# when packet size is large; - "dont" do not set DF flag.
#
# Argument "-s" specifies payload size. Value 1472 produces frames of 1500 bytes.
# And value 1473 is for frames 1501 byte which is more than usual MTU.
#
# Arguments "-c 3 -A" are used to send 3 packets as fast as possible (adaptive ping).
@biiont
biiont / absolute_paths_in_shell.sh
Created October 1, 2015 08:22
Canonical, Absolute and Relative Paths in POSIX Shell
# Prepare
mkdir -p "${HOME}/path without/symlinks"; ln -s "${HOME}/path without" "${HOME}/path with"
TESTPATH="${HOME}/..///${USER}/path with/symlinks///"; echo "${TESTPATH}"
echo "Absolute path: '$(realpath -m ${TESTPATH})'"
echo "Canonical path: '$(realpath -s -m ${TESTPATH})'"
echo "Relative to '/usr/bin': '$(realpath -s -m --relative-to="/usr/bin" ${TESTPATH})'"
echo "Canonical relative to '/usr/bin': '$(realpath -m --relative-to="/usr/bin" ${TESTPATH})'"
echo "Relative with base '/usr/bin': '$(realpath -s -m --relative-base="/usr/bin" ${TESTPATH})'"
@biiont
biiont / gist:7928f1490b20051a5008c3b85bc65123
Created September 5, 2016 13:12
ISO TO_DATE TO_TIMESTAMP format
select to_timestamp('1970-01-01T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.FFF"Z"') as ts from dual;
@biiont
biiont / reboot-test.sh
Created February 25, 2016 14:44
Continiosly reboot target system by ssh until ssh returns connection error then beep.
CNT=1; ERR=0; while test $ERR -eq 0; do printf "Attempt: %3d at %s\n" $CNT "$(date)"; ssh -o 'VisualHostKey=no' root@192.168.21.72 /sbin/reboot; ERR=$?; test $ERR -eq 0 && sleep $((2 * 60)); CNT=$CNT+1; done; test $ERR -ne 0 && printf "Failed with code %d at %s" $ERR "$(date)" && paplay /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga
#!/bin/bash
# Since we're dealing with dd, abort if any errors occur
set -e
SOURCE_FILE=${1:-/dev/zero}
TEST_FILE=${2:-dd_obs_testfile}
[ -e "$TEST_FILE" ]; TEST_FILE_EXISTS=$?
TEST_FILE_SIZE=${3:-$((128*1024*1024))} # 128Mb
@biiont
biiont / profile
Created January 26, 2016 10:34
Universal way to find out scripts BASEDIR. Can be used as base for sourcable 'profile' to alter current environment for particular use (e.g. cross-compiler environment).
if [ "${0#/*profile*}" != "${0}" ]
then
BASEDIR=$(dirname "$(readlink -f "$0")")
else
if [ $# -gt 0 -a "${1#/*profile*}" != "${1}" ]
then
BASEDIR=$(dirname "$(readlink -f "$1")")
else
echo 'NOTE: When sourcing this profile on some shells (e.g. Busybox) you need to profile file name twice.'
echo 'Usage: . path/to/profile path/to/profile'
@biiont
biiont / yacy_alternative_search.js
Created January 17, 2016 12:18
Add search links to alternative engines. To be able to continue to search other search engines if Yacy does not give you the answer.
// Go to "Administration > Portal Configuration"
// Replace donation box at "'About' Column" with:
<div style="padding:8px;"><a id="googlesearchlink" href="https://google.com/">Google</a></div>
<div style="padding:8px;"><a id="yandexsearchlink" href="https://yandex.ru/">Yandex</a></div>
<div style="padding:8px;"><a id="wikisearchlink" href="https://wikipedia.org/">Wikipedia</a></div>
<script>
function updateSearchLink(elid, hrefPref, captionPref, srch) {
var searchlink = document.getElementById(elid);
@biiont
biiont / tomatousb-button_toggle_vpnclient.sh
Created September 28, 2015 22:04
Script to toggle vpnclient by pressing WPS button on Tomato/Shibby based router (put this to "Custom script" field at "Administration>Buttons/LED" page).
[ $(pidof vpnclient1; echo $?) -ne 0 ] && service vpnclient1 start || service vpnclient1 stop
#[ $1 -le 2 ] && telnetd #...
@biiont
biiont / multipdf.sh
Created November 10, 2015 19:55
Create multipage scanned pdf files
#!/bin/sh
OUT="result" convert source1.tiff source1.tiff ... -depth 75 -units pixelsperinch -compress zip $OUT.pdf && pdf2ps $OUT.pdf && ps2pdf $OUT.ps && rm -f $OUT.ps