Skip to content

Instantly share code, notes, and snippets.

@imme-emosol
Last active April 25, 2016 06:48
Show Gist options
  • Save imme-emosol/3580ee90ca579db14be9 to your computer and use it in GitHub Desktop.
Save imme-emosol/3580ee90ca579db14be9 to your computer and use it in GitHub Desktop.
#!/bin/sh
#!/bin/sh -e
# The -e option for /bin/sh (dash) at the she-bang,
# makes this process immediately exit
# if some condition fails, see `man sh`.
evil_eval_check=$( set | grep '^shells_.*=' | cut -f 1 -d '=' )
if test "x" != "x${evil_eval_check}"
then
echo 'Been injected with a posion.'
exit 0
fi
i=0
i=$(( i + 1 ))
eval "shells_${i}=byobu"
i=$(( i + 1 ))
eval "shells_${i}=bash"
i=$(( i + 1 ))
eval "shells_${i}=bash\ +o\ history"
eval "shells_${i}_desc=bash\ without\ history"
default_choice=2
timeout_milliseconds=15
j=0
printf "\n"'Choose one of the following shells:'"\n"
while true
do
j=$(( j + 1 ))
if test "x" != "x"$( ( eval 'echo "${shells_'${j}'_desc+y}"' ) )
then
desc=\$shells_${j}_desc
else
desc=\$shells_${j}
fi
eval "echo ' - '$j') '$desc'.'"
test "$j" -lt "$i" || break
done
echo ' - ''q'') ''exit without changes''(default).'
echo -n 'Press the identifier of your choice: '
#chosen_shell=$( while ! head -c 1 ; do true ; done )
# http://stackoverflow.com/a/32270158/165330 .
readchr () {
old_stty_cfg=$( stty -g )
stty -echo raw min 0 time ${timeout_milliseconds}
printf '%s' $(dd bs=1 count=1 2>/dev/null)
stty $old_stty_cfg
}
chosen_shell=$( readchr )
if [ "x" = "x$chosen_shell" ]
then
#echo 'setting default choice('$chosen_shell').'
chosen_shell=$default_choice
fi
if test "1" != "$chosen_shell" && test "2" != "$chosen_shell" && test "3" != "$chosen_shell"
then
exit 1
fi
eval "shell_choice=\$shells_${chosen_shell}"
if command -v wmctrl >/dev/null 2>&1 && test "byobu" = "$shell_choice"
then
$( wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz )
fi
if test "bash" = "$shell_choice"
then
$( wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz )
clear
fi
exec $shell_choice
@stefan-jonker
Copy link

stefanjonker at MexBook in ~/Desktop/script
$ bash -vx ./shellchooser.sh

!/bin/sh -e

The -e option for /bin/sh at the she-bang,

makes this process immediately exit

if some condition fails, see man sh.

evil_eval_check=$( set | grep '^shells_.=' | cut -f 1 -d '=' )
set | grep '^shells
.=' | cut -f 1 -d '='
++ set
++ grep '^shells
.*='
++ cut -f 1 -d =

  • evil_eval_check=
    if test "x" != "x${evil_eval_check}"
    then
    echo 'Been injected with a posion.'
    exit 0
    fi
  • test x '!=' x
    i=0
  • i=0
    i=$(( i + 1 ))
  • i=1
    eval "shells_${i}=byobu"
  • eval shells_1=byobu
    shells_1=byobu
    ++ shells_1=byobu
    i=$(( i + 1 ))
  • i=2
    eval "shells_${i}=bash"
  • eval shells_2=bash
    shells_2=bash
    ++ shells_2=bash
    i=$(( i + 1 ))
  • i=3
    eval "shells_${i}=bash\ +o\ history"
  • eval 'shells_3=bash\ +o\ history'
    shells_3=bash\ +o\ history
    ++ shells_3='bash +o history'
    eval "shells_${i}_desc=bash\ without\ history"
  • eval 'shells_3_desc=bash\ without\ history'
    shells_3_desc=bash\ without\ history
    ++ shells_3_desc='bash without history'
    default_choice=2
  • default_choice=2
    timeout_milliseconds=15
  • timeout_milliseconds=15
    j=0
  • j=0
    printf "\n"'Choose one of the following shells:'"\n"
  • printf '\nChoose one of the following shells:\n'

Choose one of the following shells:
while true
do
j=$(( j + 1 ))
if test "x" != "x"$( ( eval 'echo "${shells_'${j}'desc+y}"' ) )
then
desc=$shells
${j}desc
else
desc=$shells
${j}
fi
eval "echo ' - '$j') '$desc'.'"
test "$j" -lt "$i" || break
done

  • true
  • j=1
    ( eval 'echo "${shells_'${j}'_desc+y}"' )
    ++ eval 'echo "${shells_1_desc+y}"'
    echo "${shells_1_desc+y}"
    +++ echo ''
  • test x '!=' x
  • desc='$shells_1'
  • eval 'echo ''' - '''1''') '''$shells_1'''.''''
    echo ' - '1') '$shells_1'.'
    ++ echo ' - 1) byobu.'
      1. byobu.
  • test 1 -lt 3
  • true
  • j=2
    ( eval 'echo "${shells_'${j}'_desc+y}"' )
    ++ eval 'echo "${shells_2_desc+y}"'
    echo "${shells_2_desc+y}"
    +++ echo ''
  • test x '!=' x
  • desc='$shells_2'
  • eval 'echo ''' - '''2''') '''$shells_2'''.''''
    echo ' - '2') '$shells_2'.'
    ++ echo ' - 2) bash.'
      1. bash.
  • test 2 -lt 3
  • true
  • j=3
    ( eval 'echo "${shells_'${j}'_desc+y}"' )
    ++ eval 'echo "${shells_3_desc+y}"'
    echo "${shells_3_desc+y}"
    +++ echo y
  • test x '!=' xy
  • desc='$shells_3_desc'
  • eval 'echo ''' - '''3''') '''$shells_3_desc'''.''''
    echo ' - '3') '$shells_3_desc'.'
    ++ echo ' - 3) bash' without history.
      1. bash without history.
  • test 3 -lt 3
  • break
    echo ' - ''q'') ''exit without changes''(default).'
  • echo ' - q) exit without changes(default).'
    • q) exit without changes(default).
      echo -n 'Press the identifier of your choice: '
  • echo -n 'Press the identifier of your choice: '
    Press the identifier of your choice: #chosen_shell=$( while ! head -c 1 ; do true ; done )

    http://stackoverflow.com/a/32270158/165330 .

    readchr () {
    old_stty_cfg=$( stty -g )
    stty -echo raw min 0 time ${timeout_milliseconds}
    printf '%s' $(dd bs=1 count=1 2>/dev/null)
    stty $old_stty_cfg
    }
    chosen_shell=$( readchr )
    readchr
    ++ readchr
    stty -g
    +++ stty -g
    ++ old_stty_cfg=gfmt1:cflag=4b00:iflag=6b02:lflag=200005cb:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=9600:ospeed=9600
    ++ stty -echo raw min 0 time 15
    dd bs=1 count=1 2>/dev/null
    +++ dd bs=1 count=1
    ++ printf %s
    ++ stty gfmt1:cflag=4b00:iflag=6b02:lflag=200005cb:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=9600:ospeed=9600
    + chosen_shell=
    if [ "x" = "x$chosen_shell" ]
    then
    #echo 'setting default choice('$chosen_shell').'
    chosen_shell=$default_choice
    fi
  • '[' x = x ']'
  • chosen_shell=2
    if test "1" != "$chosen_shell" && test "2" != "$chosen_shell" && test "3" != "$chosen_shell"
    then
    exit 1
    fi
  • test 1 '!=' 2
  • test 2 '!=' 2
    eval "shell_choice=$shells_${chosen_shell}"
  • eval 'shell_choice=$shells_2'
    shell_choice=$shells_2
    ++ shell_choice=bash
    if command -v wmctrl >/dev/null 2>&1 && test "byobu" = "$shell_choice"
    then
    $( wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz )
    fi
  • command -v wmctrl
  • test byobu = bash
    if test "bash" = "$shell_choice"
    then
    $( wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz )
    clear
    fi
  • test bash = bash
    wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz
    ++ wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz
  • clear

exec $shell_choice

  • exec bash
    bash-3.2$

@imme-emosol
Copy link
Author

thnX :)

de output zonder -vx is wel een stukkie fraaier ..
En ik ben ook benieuwd naar de output vanuit een andere shell omgeving dan bash .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment