-
-
Save gvlx/0adfb1137937ad443df2eeaa73dc0ba7 to your computer and use it in GitHub Desktop.
Revisions
-
Gerardo Lisboa revised this gist
Dec 18, 2017 . 1 changed file with 86 additions and 55 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,13 @@ #!/bin/bash # Following stackoverflow discussion, from https://gist.github.com/tvlooy/cbfbdb111a4ebad8b93e (rev7) function test(){ local __MESSAGE__="$1" local __RECEIVED__="$2" local __EXPECTED__="$3" if [ "${__RECEIVED__}" == "${__EXPECTED__}" ]; then # shellcheck disable=SC1117 echo -e "\033[32m✔ Tested ${__MESSAGE__}" else @@ -19,47 +21,77 @@ function test(){ } function testSuite(){ test 'absolute call' "$(bash ${__TEMPDIR__}/1234/test.sh)" "${__TEMPDIR__}/1234" test 'via symlinked dir' "$(bash ${__TEMPDIR__}/current/test.sh)" "${__TEMPDIR__}/1234" test 'via symlinked file' "$(bash ${__TEMPDIR__}/test.sh)" "${__TEMPDIR__}/1234" test 'via multiple symlinked dirs' "$(bash ${__TEMPDIR__}/current/loop/test.sh)" "${__TEMPDIR__}/1234" pushd "${__TEMPDIR__}" >/dev/null || exit 1 test 'relative call' "$(bash 1234/test.sh)" "${__TEMPDIR__}/1234" popd >/dev/null || exit 1 test 'with space in dir' "$(bash ${__TEMPDIR__}/12\ 34/test.sh)" "${__TEMPDIR__}/1234" test 'with space in file' "$(bash ${__TEMPDIR__}/1234/te\ st.sh)" "${__TEMPDIR__}/1234" echo } function readlink_exists(){ if [[ ! $(which readlink 2> /dev/null) ]] then # shellcheck disable=SC1117 echo -e "\033[31m✘ readlink not available, test skipped" # shellcheck disable=SC1117 echo -e "\033[0m" (exit 1) fi; } function rm_tempdir(){ [[ -n "${__TEMPDIR__}" && -d "${__TEMPDIR__}" ]]\ && rm -fr "${__TEMPDIR__}"; } function set_tempdir(){ rm_tempdir; while [[ -z "${__TEMPDIR__}" || -d "${__TEMPDIR__}" ]]; do __TEMPDIR__="/tmp/${BASH_SOURCE[0]}_temp.${USER}.${RANDOM}"; done; mkdir -p "${__TEMPDIR__}" } function teardown(){ # remove symlinks before files local __DIR__="${__TEMPDIR__}/1234"; local __FILE__="test.sh"; local __DIR2__="${__TEMPDIR__}/12 34"; local __FILE2__="te st.sh"; [[ -e "${__DIR2__}" ]] && rm -rf "${__DIR2__:?}"; [[ -e "${__DIR__}/${__FILE2__}" ]] && rm -f "${__DIR__:?}/${__FILE2__}"; [[ -e "${__DIR2__}/${__FILE__}" ]] && rm -f "${__DIR2__:?}/${__FILE__}"; [[ -e "${__DIR2__}/${__FILE2__}" ]] && rm -f "${__DIR2__:?}/${__FILE2__}"; [[ -e ${__TEMPDIR__}/"${__FILE__}" ]] && rm -f "${__TEMPDIR__}/${__FILE__}"; [[ -e ${__TEMPDIR__}/current ]] && rm -rf "${__TEMPDIR__}/current"; [[ -e ${__TEMPDIR__}/current/loop ]] && rm -rf "${__TEMPDIR__}/current/loop"; [[ -e "${__DIR__}" ]] && rm -rf "${__DIR__:?}"; [[ -e "${__DIR__}/${__FILE__}" ]] && rm -f "${__DIR__:?}/${__FILE__}"; rm_tempdir; } function setup(){ set_tempdir; local __DIR__="${__TEMPDIR__}/1234"; local __FILE__="test.sh"; local __DIR2__="${__TEMPDIR__}/12 34"; local __FILE2__="te st.sh"; mkdir "${__DIR__}" touch "${__DIR__}/${__FILE__}" ln -s "${__DIR__}/${__FILE__}" "${__TEMPDIR__}" ln -s "${__DIR__}" "${__TEMPDIR__}/current" ln -s "${__DIR__}" "${__TEMPDIR__}/current/loop" mkdir "${__DIR2__}" ln -s "${__DIR__}/${__FILE__}" "${__DIR__}/${__FILE2__}" ln -s "${__DIR__}/${__FILE__}" "${__DIR2__}/${__FILE__}" @@ -68,23 +100,21 @@ function setup(){ function test1(){ echo 'Test 1: via dirname' cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" echo \`dirname \$0\` EOF } function test2(){ echo 'Test 2: via pwd' cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" echo \$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) EOF } function test3(){ echo 'Test 3: overcomplicated stackoverflow solution' cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" __SOURCE__="\${BASH_SOURCE[0]}" while [ -h "\$__SOURCE__" ]; do __DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )" @@ -94,62 +124,63 @@ function test3(){ __DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )" echo \$__DIR__ EOF } function test4(){ echo 'Test 4: via readlink' cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" echo \`dirname \$(readlink -f \$0 2> /dev/null)\` EOF } function test5(){ echo 'Test 5: via readlink with space' cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" echo \`dirname \$(readlink -f "\$0" 2> /dev/null)\` EOF } function test6(){ echo 'Test 6: as Test 2 but with cd -P'; cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" echo \$( cd -P "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) EOF } function test7(){ echo 'Test 7: via cd -P and pwd, testing for symlinked file first'; cat <<- EOF >"${__TEMPDIR__}/1234/test.sh" __SOURCE__="\${BASH_SOURCE[0]}" while [[ -h "\${__SOURCE__}" ]]; do __SOURCE__=\$(find "\${__SOURCE__}" -type l -ls | sed -n 's/^.* -> \(.*\)/\1/p'); done; echo \$(cd -P "\$( dirname "\${__SOURCE__}" )" && pwd) EOF } main(){ local __TEMPDIR__=""; echo; teardown; # sanity check (in case last run was interrupted) setup; if [[ -n "${1}" ]]; then "${1}" && testSuite; else test1 && testSuite; test2 && testSuite; test3 && testSuite; test4 && testSuite; test5 && testSuite; test6 && testSuite; test7 && testSuite; fi teardown; } main "$@"; -
Gerardo Lisboa revised this gist
Oct 13, 2017 . 1 changed file with 43 additions and 44 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,18 @@ #!/bin/bash function test(){ local __MESSAGE__="$1" local __RECEIVED__="$2" local __EXPECTED__="$3" if [ "${__RECEIVED__}" = "${__EXPECTED__}" ]; then # shellcheck disable=SC1117 echo -e "\033[32m✔ Tested ${__MESSAGE__}" else # shellcheck disable=SC1117 echo -e "\033[31m✘ Tested ${__MESSAGE__}" echo -e " Received: ${__RECEIVED__}" echo -e " Expected: ${__EXPECTED__}" fi # shellcheck disable=SC1117 echo -en "\033[0m" @@ -33,37 +33,37 @@ function testSuite(){ function teardown(){ # remove symlinks before files local __DIR__="/tmp/1234"; local __FILE__="test.sh"; local __DIR2__="/tmp/12 34"; local __FILE2__="te st.sh"; [[ -e "${__DIR2__}" ]] && rm -rf "${__DIR2__:?}"; [[ -e "${__DIR__}/${__FILE2__}" ]] && rm -f "${__DIR__:?}/${__FILE2__}"; [[ -e "${__DIR2__}/${__FILE__}" ]] && rm -f "${__DIR2__:?}/${__FILE__}"; [[ -e "${__DIR2__}/${__FILE2__}" ]] && rm -f "${__DIR2__:?}/${__FILE2__}"; [[ -e /tmp/"${__FILE__}" ]] && rm -f "/tmp/${__FILE__}"; [[ -e /tmp/current ]] && rm -rf /tmp/current; [[ -e /tmp/current/loop ]] && rm -rf /tmp/current/loop; [[ -e "${__DIR__}" ]] && rm -rf "${__DIR__:?}"; [[ -e "${__DIR__}/${__FILE__}" ]] && rm -f "${__DIR__:?}/${__FILE__}"; } function setup(){ local __DIR__="/tmp/1234"; local __FILE__="test.sh"; local __DIR2__="/tmp/12 34"; local __FILE2__="te st.sh"; mkdir "${__DIR__}" touch "${__DIR__}/${__FILE__}" ln -s "${__DIR__}/${__FILE__}" /tmp ln -s "${__DIR__}" /tmp/current ln -s "${__DIR__}" /tmp/current/loop mkdir "${__DIR2__}" ln -s "${__DIR__}/${__FILE__}" "${__DIR__}/${__FILE2__}" ln -s "${__DIR__}/${__FILE__}" "${__DIR2__}/${__FILE__}" ln -s "${__DIR__}/${__FILE__}" "${__DIR2__}/${__FILE2__}" } function test1(){ @@ -85,14 +85,14 @@ function test2(){ function test3(){ echo 'Test 3: overcomplicated stackoverflow solution' cat <<- EOF >/tmp/1234/test.sh __SOURCE__="\${BASH_SOURCE[0]}" while [ -h "\$__SOURCE__" ]; do __DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )" __SOURCE__="\$(readlink "\$__SOURCE__" 2> /dev/null)" [[ \$__SOURCE__ != /* ]] && __SOURCE__="\$__DIR__/\$__SOURCE__" done __DIR__="\$( cd -P "\$( dirname "\$__SOURCE__" )" && pwd )" echo \$__DIR__ EOF testSuite } @@ -126,7 +126,6 @@ function test7(){ cat <<- EOF >/tmp/1234/test.sh __SOURCE__="\${BASH_SOURCE[0]}" while [[ -h "\${__SOURCE__}" ]]; do __SOURCE__=\$(find "\${__SOURCE__}" -type l -ls | sed -n 's/^.* -> \(.*\)/\1/p'); done; -
Gerardo Lisboa revised this gist
Oct 13, 2017 . 1 changed file with 80 additions and 55 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,107 +1,127 @@ #!/bin/bash function test(){ local MESSAGE="$1" local RECEIVED="$2" local EXPECTED="$3" if [ "$RECEIVED" = "$EXPECTED" ]; then # shellcheck disable=SC1117 echo -e "\033[32m✔ Tested $MESSAGE" else # shellcheck disable=SC1117 echo -e "\033[31m✘ Tested $MESSAGE" echo -e " Received: $RECEIVED" echo -e " Expected: $EXPECTED" fi # shellcheck disable=SC1117 echo -en "\033[0m" } function testSuite(){ test 'absolute call' "$(bash /tmp/1234/test.sh)" /tmp/1234 test 'via symlinked dir' "$(bash /tmp/current/test.sh)" /tmp/1234 test 'via symlinked file' "$(bash /tmp/test.sh)" /tmp/1234 test 'via multiple symlinked dirs' "$(bash /tmp/current/loop/test.sh)" /tmp/1234 pushd /tmp >/dev/null || exit 1 test 'relative call' "$(bash 1234/test.sh)" /tmp/1234 popd >/dev/null || exit 1 test 'with space in dir' "$(bash /tmp/12\ 34/test.sh)" /tmp/1234 test 'with space in file' "$(bash /tmp/1234/te\ st.sh)" /tmp/1234 echo } function teardown(){ # remove symlinks before files local DIR="/tmp/1234"; local FILE="test.sh"; local DIR2="/tmp/12 34"; local FILE2="te st.sh"; [[ -e "${DIR2}" ]] && rm -rf "${DIR2:?}"; [[ -e "${DIR}/${FILE2}" ]] && rm -f "${DIR:?}/${FILE2}"; [[ -e "${DIR2}/${FILE}" ]] && rm -f "${DIR2:?}/${FILE}"; [[ -e "${DIR2}/${FILE2}" ]] && rm -f "${DIR2:?}/${FILE2}"; [[ -e /tmp/"${FILE}" ]] && rm -f "/tmp/${FILE}"; [[ -e /tmp/current ]] && rm -rf /tmp/current; [[ -e /tmp/current/loop ]] && rm -rf /tmp/current/loop; [[ -e "${DIR}" ]] && rm -rf "${DIR:?}"; [[ -e "${DIR}/${FILE}" ]] && rm -f "${DIR:?}/${FILE}"; } function setup(){ local DIR=/tmp/1234 local FILE=test.sh local DIR2="/tmp/12 34" local FILE2="te st.sh" mkdir "${DIR}" touch "${DIR}/${FILE}" ln -s "${DIR}/${FILE}" /tmp ln -s "${DIR}" /tmp/current ln -s "${DIR}" /tmp/current/loop mkdir "${DIR2}" ln -s "${DIR}/${FILE}" "${DIR}/${FILE2}" ln -s "${DIR}/${FILE}" "${DIR2}/${FILE}" ln -s "${DIR}/${FILE}" "${DIR2}/${FILE2}" } function test1(){ echo 'Test 1: via dirname' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$0\` EOF testSuite } function test2(){ echo 'Test 2: via pwd' cat <<- EOF >/tmp/1234/test.sh echo \$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) EOF testSuite } function test3(){ echo 'Test 3: overcomplicated stackoverflow solution' cat <<- EOF >/tmp/1234/test.sh SOURCE="\${BASH_SOURCE[0]}" while [ -h "\$SOURCE" ]; do DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" SOURCE="\$(readlink "\$SOURCE" 2> /dev/null)" [[ \$SOURCE != /* ]] && SOURCE="\$DIR/\$SOURCE" done DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" echo \$DIR EOF testSuite } function test4(){ echo 'Test 4: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0 2> /dev/null)\` EOF testSuite } function test5(){ echo 'Test 5: via readlink with space' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f "\$0" 2> /dev/null)\` EOF testSuite } function test6(){ echo 'Test 6: as Test 2 but with cd -P'; cat <<- EOF >/tmp/1234/test.sh echo \$( cd -P "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) EOF testSuite } function test7(){ echo 'Test 7: via cd -P and pwd, testing for symlinked file first'; cat <<- EOF >/tmp/1234/test.sh __SOURCE__="\${BASH_SOURCE[0]}" @@ -116,10 +136,13 @@ function test7 { } echo; teardown; # sanity check (in case last run was interrupted) setup; if [ "$1" != "" ]; then $1; else test1 test2 @@ -128,4 +151,6 @@ else test5 test6 test7 fi teardown; -
Gerardo Lisboa revised this gist
Oct 13, 2017 . 1 changed file with 31 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ function testSuite { pushd /tmp >/dev/null test 'relative call' `bash 1234/test.sh` /tmp/1234 popd >/dev/null test 'with space in dir' "`bash /tmp/12\ 34/test.sh`" /tmp/1234 test 'with space in file' `bash /tmp/1234/te\ st.sh` /tmp/1234 echo } @@ -67,7 +67,7 @@ function test3 { SOURCE="\${BASH_SOURCE[0]}" while [ -h "\$SOURCE" ]; do DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" SOURCE="\$(readlink "\$SOURCE" 2> /dev/null)" [[ \$SOURCE != /* ]] && SOURCE="\$DIR/\$SOURCE" done DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" @@ -79,18 +79,43 @@ function test3 { function test4 { echo 'Test 4: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0 2> /dev/null)\` EOF testSuite } function test5 { echo 'Test 5: via readlink with space' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f "\$0" 2> /dev/null)\` EOF testSuite } function test6 { echo 'Test 6: as Test 2 but with cd -P'; cat <<- EOF >/tmp/1234/test.sh CACHE_DIR=\$( cd -P "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) echo \$CACHE_DIR EOF testSuite } function test7 { echo 'Test 7: via cd -P and pwd, testing for symlinked file first'; cat <<- EOF >/tmp/1234/test.sh __SOURCE__="\${BASH_SOURCE[0]}" while [[ -h "\${__SOURCE__}" ]]; do # shellcheck disable=SC1117 __SOURCE__=\$(find "\${__SOURCE__}" -type l -ls | sed -n 's/^.* -> \(.*\)/\1/p'); done; echo \$(cd -P "\$( dirname "\${__SOURCE__}" )" && pwd) EOF testSuite } echo setup if [ "$1" != "" ]; then @@ -101,4 +126,6 @@ else test3 test4 test5 test6 test7 fi -
tvlooy revised this gist
Oct 29, 2015 . 1 changed file with 25 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ function test { RECEIVED=$2 EXPECTED=$3 if [ "$RECEIVED" = "$EXPECTED" ]; then echo -e "\033[32m✔︎ Tested $MESSAGE" else echo -e "\033[31m✘ Tested $MESSAGE" @@ -23,6 +23,8 @@ function testSuite { pushd /tmp >/dev/null test 'relative call' `bash 1234/test.sh` /tmp/1234 popd >/dev/null test 'with space in dir' `bash /tmp/12\ 34/test.sh` /tmp/1234 test 'with space in file' `bash /tmp/1234/te\ st.sh` /tmp/1234 echo } @@ -34,6 +36,12 @@ function setup { if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current if [ -f /tmp/current/loop ]; then rm /tmp/current/loop; fi; ln -s $DIR /tmp/current/loop DIR2="/tmp/12 34" FILE2="te st.sh" if [ -e "$DIR2" ]; then rm -rf "$DIR2"; fi; mkdir "$DIR2" if [ -f "$DIR/$FILE2" ]; then rm -rf "$DIR/$FILE2"; fi; ln -s $DIR/$FILE "$DIR/$FILE2" if [ -f "$DIR2/$FILE" ]; then rm -rf "$DIR2/$FILE"; fi; ln -s $DIR/$FILE "$DIR2/$FILE" if [ -f "$DIR2/$FILE2" ]; then rm -rf "$DIR2/$FILE2"; fi; ln -s $DIR/$FILE "$DIR2/$FILE2" } function test1 { @@ -76,9 +84,21 @@ function test4 { testSuite } function test5 { echo 'Test 5: via readlink with space' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f "\$0")\` EOF testSuite } echo setup if [ "$1" != "" ]; then $1 else test1 test2 test3 test4 test5 fi -
tvlooy revised this gist
Jun 9, 2015 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,7 @@ function testSuite { pushd /tmp >/dev/null test 'relative call' `bash 1234/test.sh` /tmp/1234 popd >/dev/null echo } function setup { @@ -36,15 +37,15 @@ function setup { } function test1 { echo 'Test 1: via dirname' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$0\` EOF testSuite } function test2 { echo 'Test 2: via pwd' cat <<- EOF >/tmp/1234/test.sh CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) echo \$CACHE_DIR @@ -53,7 +54,7 @@ function test2 { } function test3 { echo 'Test 3: overcomplicated stackoverflow solution' cat <<- EOF >/tmp/1234/test.sh SOURCE="\${BASH_SOURCE[0]}" while [ -h "\$SOURCE" ]; do @@ -68,13 +69,14 @@ function test3 { } function test4 { echo 'Test 4: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` EOF testSuite } echo setup test1 test2 -
tvlooy revised this gist
Jun 9, 2015 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,15 +36,15 @@ function setup { } function test1 { echo -e "\nTest 1: via dirname" cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$0\` EOF testSuite } function test2 { echo -e "\nTest 2: via pwd" cat <<- EOF >/tmp/1234/test.sh CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) echo \$CACHE_DIR @@ -53,7 +53,7 @@ function test2 { } function test3 { echo -e "\nTest 3: overcomplicated stackoverflow solution" cat <<- EOF >/tmp/1234/test.sh SOURCE="\${BASH_SOURCE[0]}" while [ -h "\$SOURCE" ]; do @@ -68,7 +68,7 @@ function test3 { } function test4 { echo -e "\nTest 4: via readlink" cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` EOF -
tvlooy revised this gist
Jun 9, 2015 . 1 changed file with 20 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,7 @@ function testSuite { test 'absolute call' `bash /tmp/1234/test.sh` /tmp/1234 test 'via symlinked dir' `bash /tmp/current/test.sh` /tmp/1234 test 'via symlinked file' `bash /tmp/test.sh` /tmp/1234 test 'via multiple symlinked dirs' `bash /tmp/current/loop/test.sh` /tmp/1234 pushd /tmp >/dev/null test 'relative call' `bash 1234/test.sh` /tmp/1234 popd >/dev/null @@ -31,6 +32,7 @@ function setup { if [ -f $DIR/$FILE ]; then rm -rf $DIR/$FILE; fi; touch $DIR/$FILE if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current if [ -f /tmp/current/loop ]; then rm /tmp/current/loop; fi; ln -s $DIR /tmp/current/loop } function test1 { @@ -42,7 +44,7 @@ function test1 { } function test2 { echo 'Test 2: via pwd' cat <<- EOF >/tmp/1234/test.sh CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) echo \$CACHE_DIR @@ -51,7 +53,22 @@ echo 'Test 2: via pwd' } function test3 { echo "Test 3: overcomplicated stackoverflow solution" cat <<- EOF >/tmp/1234/test.sh SOURCE="\${BASH_SOURCE[0]}" while [ -h "\$SOURCE" ]; do DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" SOURCE="\$(readlink "\$SOURCE")" [[ \$SOURCE != /* ]] && SOURCE="\$DIR/\$SOURCE" done DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )" echo \$DIR EOF testSuite } function test4 { echo 'Test 4: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` EOF @@ -62,3 +79,4 @@ setup test1 test2 test3 test4 -
tvlooy revised this gist
Jun 9, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ echo 'Test 2: via pwd' } function test3 { echo 'Test 3: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` EOF -
tvlooy revised this gist
Jun 9, 2015 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,6 +42,15 @@ function test1 { } function test2 { echo 'Test 2: via pwd' cat <<- EOF >/tmp/1234/test.sh CACHE_DIR=\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd ) echo \$CACHE_DIR EOF testSuite } function test3 { echo 'Test 2: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` @@ -51,4 +60,5 @@ function test2 { setup test1 test2 test3 -
tvlooy created this gist
Jun 9, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ #!/bin/bash function test { MESSAGE=$1 RECEIVED=$2 EXPECTED=$3 if [ $RECEIVED = $EXPECTED ]; then echo -e "\033[32m✔︎ Tested $MESSAGE" else echo -e "\033[31m✘ Tested $MESSAGE" echo -e " Received: $RECEIVED" echo -e " Expected: $EXPECTED" fi echo -en "\033[0m" } function testSuite { test 'absolute call' `bash /tmp/1234/test.sh` /tmp/1234 test 'via symlinked dir' `bash /tmp/current/test.sh` /tmp/1234 test 'via symlinked file' `bash /tmp/test.sh` /tmp/1234 pushd /tmp >/dev/null test 'relative call' `bash 1234/test.sh` /tmp/1234 popd >/dev/null } function setup { DIR=/tmp/1234 FILE=test.sh if [ -e $DIR ]; then rm -rf $DIR; fi; mkdir $DIR if [ -f $DIR/$FILE ]; then rm -rf $DIR/$FILE; fi; touch $DIR/$FILE if [ -f /tmp/$FILE ]; then rm /tmp/$FILE; fi; ln -s $DIR/$FILE /tmp if [ -f /tmp/current ]; then rm /tmp/current; fi; ln -s $DIR /tmp/current } function test1 { echo 'Test 1: via dirname' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$0\` EOF testSuite } function test2 { echo 'Test 2: via readlink' cat <<- EOF >/tmp/1234/test.sh echo \`dirname \$(readlink -f \$0)\` EOF testSuite } setup test1 test2