User Tools

Site Tools


development:scripts:shell

Shell


Helpful Aliases

alias ..='cd ..'
alias ...='cd ../..'
alias +='pushd .'
alias -- -='popd'
 
alias ll='ls -lF'
alias la='ls -AF'
alias l='ls -CF'

Optional source another script

if [ -f ~/.bashrc-rho ]; then
    . ~/.bashrc-rho
fi

How to "source" an environment for a command

$SHELL -c "( source env-of-cmd.sh ; $* )"

How to "daemonize" a script

if [ "x$1" != "x--" ]; then
    echo "restarting as daemon..."
    $0 -- &
    exit 0
fi
 
# stuff to do

Kill a child after timeout

    TIMEOUT=5
    echo -n "executing app... "
    app &
    PID_APP=$!
    ( sleep $TIMEOUT && kill -9 $PID_APP ) 2>/dev/null &
    PID_WATCHER=$!
 
    if wait $PID_APP 2>/dev/null; then
        echo "done."
         kill -9 $PID_WATCHER
         wait $PID_WATCHER 2>/dev/null
    else
        echo "error."
    fi

Read/Write number binary

# write to file (example: 1st byte in file)
    echo -ne "$(printf '\\x%x' $number)" | dd of=<file> count=1 bs=1c 2>/dev/null
 
# read from file
    number="$(dd if=<file>" count=1 bs=1c 2>/dev/null | od -i | tr -s ' ' | cut -s -d ' ' -f 2)"
    if [ $(echo "$number" | grep -c -E "^[0-9]+$") -ne 0 ] ; then
    # success
    fi

Generate a "C" source from binary file

echo "/* HEADER */"
echo
:
:
echo "static int binarray = {"
od -v -t x1 -A n $FILE | sed -e 's/\([0-9a-f][0-9a-f]\)/0x\1,/g'
echo "};"

Compare two directory listings (regarding names and size)

ls -lL <path/to/dir1> | sed -e 's/ [ ]*/ /g' | cut -d ' ' -f '5,9-' | sort -k 2 > dir1.lst
ls -lL <path/to/dir2> | sed -e 's/ [ ]*/ /g' | cut -d ' ' -f '5,9-' | sort -k 2 > dir2.lst
diff dir1.lst dir2.lst
development/scripts/shell.txt · Last modified: 2021/04/10 09:39 by Ralf Hoppe