Ceso Adventures

Blogs, Security CTFs & Tutorials


Bash

xargs

ls | xargs -I {} mv {} 20080815-{}

String substitution:

STR=/path/to/foo.c

echo ${STR%/*}      #=> "/path/to"
echo ${STR%.c}      #=> "/path/to/foo"
echo ${STR%.c}.o    #=> "/path/to/foo.o"
echo ${STR##*.}     #=> "c" (extension)

BASE=${SRC##*/}     #=> "foo.c" (basepath)
DIR=${SRC%$BASE}    #=> "/path/to"
echo ${STR%/*}      #=> "/path/to"

Varible Substitution

echo ${STR/hi/hello}          # Replace first match
echo ${STR//hi/hello}         # Replace all matches
echo ${STR/#hi/hello}         # ^hi
echo ${STR/%hi/hello}         # hi$

echo "${STR:0:3}"             # .substr(0, 3) -- position, length
echo "${STR:-3:3}"            # Negative position = from the right

echo ${#line}                 # Length of $line

[ -z "$CC" ] && CC=gcc        # CC ||= "gcc"  assignment
${CC:=gcc}                    # $CC || "gcc"

#Substring for first 5 characters:
${variable:0:5}

Regex

if [[ "A" =~ "." ]]

Numeric comparisons

if (( $a < $b ))

Numeric calculation

$((a + 200))                # $ is optional

Variable defaults

${FOO:-word}                # Returns word
${FOO:+word}                # Returns empty, or word if set
${FOO:=word}                # Sets parameter to word, returns word
${FOO:?message}             # Echoes message and exits

${FOO=word}                 # : is optional in all of the above

Random numbers

$((RANDOM%=200))            # Random number 0..200
set -o noclobber            # Avoid overlay files (echo "hi" > foo)
set -o errexit              # Used to exit upon error, avoiding cascading errors
set -o pipefail             # Unveils hidden failures
set -o nounset              # Exposes unset variables

References

http://wiki.bash-hackers.org/doku.php


Colors

echo $(tput setaf 1) This is just a test

bold() {
  echo -ne '\033[1;37m'$1'\033[0m'
}

red() {
  echo -ne '\033[1;31m'$1'\033[0m'
}

green() {
  echo -ne '\033[1;32m'$1'\033[0m'
}

blue () {
  echo -ne '\033[1;34m'$1'\033[0m'
}

yellow() {
  echo -ne '\033[1;33m'$1'\033[0m'
  #   tput sgr0
}

pink() {
  echo -ne '\033[1;35m'$1'\033[0m'
  #   tput sgr0
}



fgblk="$(tput setaf 0)" # Black - Regular
fgred="$(tput setaf 1)" # Red
fggrn="$(tput setaf 2)" # Green
fgylw="$(tput setaf 3)" # Yellow
fgblu="$(tput setaf 4)" # Blue
fgpur="$(tput setaf 5)" # Purple
fgcyn="$(tput setaf 6)" # Cyan
fgwht="$(tput setaf 7)" # White

bfgblk="$(tput setaf 8)" # Black - Bright
bfgred="$(tput setaf 9)" # Red
bfggrn="$(tput setaf 10)" # Green
bfgylw="$(tput setaf 11)" # Yellow
bfgblu="$(tput setaf 12)" # Blue
bfgpur="$(tput setaf 13)" # Purple
bfgcyn="$(tput setaf 14)" # Cyan
bfgwht="$(tput setaf 15)" # White

bgblk="$(tput setab 0)" # Black - Background
bgred="$(tput setab 1)" # Red
bggrn="$(tput setab 2)" # Green
bgylw="$(tput setab 3)" # Yellow
bgblu="$(tput setab 4)" # Blue
bgpur="$(tput setab 5)" # Purple
bgcyn="$(tput setab 6)" # Cyan
bgwht="$(tput setab 7)" # White

bbgblk="$(tput setab 8)" # Black - Background - Bright
bbgred="$(tput setab 9)" # Red
bbggrn="$(tput setab 10)" # Green
bbgylw="$(tput setab 11)" # Yellow
bbgblu="$(tput setab 12)" # Blue
bbgpur="$(tput setab 13)" # Purple
bbgcyn="$(tput setab 14)" # Cyan
bbgwht="$(tput setab 15)" # White

normal="$(tput sgr0)" # text reset
mkbold="$(tput bold)" # make bold
undrln="$(tput smul)" # underline
noundr="$(tput rmul)" # remove underline
mkblnk="$(tput blink)" # make blink
revers="$(tput rev)" # reverse