ln -s wrapper, with more useful features
#!/bin/bash
spath=$(realpath "$0")
swd=${spath%/*}
source "$swd"/lns.bashlib
main "$@"
#!/bin/bash
# "We're all the roaming orphans in the immense space
# though huddling in a big spaceship called the Earth."
# -- The Martians
# if expanded_arg_num == 1
# create a link to current dir
# if expanded_arg_num == 2
# create a link to the specified path/dir
# if expanded_arg_num >= 3
# if the final arg is a dir
# create links to the dir specified by the final arg
get_fpath_list(){
mapfile -t arr_fpath_list < <(realpath "$@")
}
lns_1_arg(){
fpath_target=`realpath "$1"`
ln -s "$fpath_target"
}
lns_2_args(){
fpath_target=`realpath "$1"`
# path of link or path of parent dir
fpath_link=`realpath "$2"`
echo ""
ln -s "$fpath_target" "$fpath_link"
}
lns_3_or_more_args(){
length_all=${#arr_fpath_list[@]}
length_targets=$((length_all -1))
echo "length_targets: $length_targets"
dpath_link="${arr_fpath_list[$length_targets]}"
if [ ! -d $dpath_link ]
then
echo "ERR:: Destinated path is not dir: $dpath_link"". Stop."
return 1
fi
for fpath in "${arr_fpath_list[@]:0:$length_targets}"
do
echo $fpath
ln -s "$fpath" "$dpath_link"
done
}
main(){
get_fpath_list "$@"
length=${#arr_fpath_list[@]}
if [ 0 -eq $length ]
then
echo "ERR:: Found no link target. Stop."
return 1
fi
if [ 1 -eq $length ]
then
lns_1_arg "${arr_fpath_list[@]}"
fi
if [ 2 -eq $length ]
then
lns_2_args "${arr_fpath_list[@]}"
fi
if [ 3 -le $length ]
then
lns_3_or_more_args "${arr_fpath_list[@]}"
fi
}
#!/bin/bash
spath=$(realpath "$0")
swd=${spath%/*}
source "$swd"/testf.bashlib
source "$swd"/lns.bashlib
init_test_extra(){
[ -d $dpath_test/d_l ] && rm -r $dpath_test/{d_l,d_t}
mkdir $dpath_test/{d_l,d_t}
cd $dpath_test
}
test_lns_1_arg(){
echo "h" > $dpath_test/d_t/f_f1
lns_1_arg "$dpath_test/d_t/f_f1"
rslt=$(cat f_f1)
assert_equal_str "h" "$rslt"
}
test_lns_2_args(){
echo "h" > $dpath_test/d_t/f_f1
lns_2_args "$dpath_test/d_t/f_f1" "$dpath_test/d_l/f_f1"
rslt=$(cat $dpath_test/d_l/f_f1)
assert_equal_str "h" "$rslt"
echo "h" > $dpath_test/d_t/f_f2
lns_2_args "$dpath_test/d_t/f_f2" "$dpath_test/d_l"
rslt=$(cat $dpath_test/d_l/f_f2)
assert_equal_str "h" "$rslt"
}
test_lns_3_or_more_args(){
arr_fpath_list=("$dpath_test/d_t/f_f3" "$dpath_test/d_t/f_f4" "$dpath_test/d_l")
#dpath_link=$(realpath "$dpath_test/d_l")
lns_3_or_more_args
ls -l "$dpath_link"/*
fpath_f3_target=$(readlink -m "$dpath_link"/f_f3)
actual="${fpath_f3_target#$dpath_test/d_t/}"
assert_equal_str "f_f3" "$actual"
}
test_main_no_arg(){
rslt=$(main)
retval=$?
assert_equal_num 1 $retval
}
test_main_1_arg(){
main "$dpath_test/d_t/f_f5"
echo "h" > $dpath_test/d_t/f_f5
rslt=$(cat f_f5)
assert_equal_str "h" "$rslt"
}
test_main_2_args(){
echo "h" > $dpath_test/d_t/f_f6
main "$dpath_test/d_t/f_f6" "$dpath_test/d_l/f_f6"
rslt=$(cat $dpath_test/d_l/f_f6)
assert_equal_str "h" "$rslt"
echo "h" > $dpath_test/d_t/f_f7
main "$dpath_test/d_t/f_f7" "$dpath_test/d_l"
rslt=$(cat $dpath_test/d_l/f_f7)
assert_equal_str "h" "$rslt"
}
test_main_3_args(){
echo "h" > $dpath_test/d_t/f_f8
echo "h" > $dpath_test/d_t/f_f9
main "$dpath_test/d_t/f_f8" "$dpath_test/d_t/f_f9" "$dpath_test/d_l"
rslt1=$(cat $dpath_test/d_l/f_f8)
rslt2=$(cat $dpath_test/d_l/f_f9)
assert_equal_str "h" "$rslt1"
assert_equal_str "h" "$rslt2"
rslt=$(main "$dpath_test/d_t/f_f10" "$dpath_test/d_t/f_f11" "$dpath_test/d_l/f_12")
retval=$?
assert_equal_num 1 $retval
}
test_main_4_args(){
echo "h" > $dpath_test/d_t/f_f13
echo "h" > $dpath_test/d_t/f_f15
main "$dpath_test/d_t/f_f13" "$dpath_test/d_t/f_f14" "$dpath_test/d_t/f_f15" "$dpath_test/d_l"
rslt1=$(cat $dpath_test/d_l/f_f13)
rslt2=$(cat $dpath_test/d_l/f_f15)
assert_equal_str "h" "$rslt1"
assert_equal_str "h" "$rslt2"
}
opt="-o"
run_tests "test_lns_1_arg" "test_lns_2_args" "test_lns_3_or_more_args"
run_tests "test_main_no_arg" "test_main_1_arg" "test_main_2_args" "test_main_3_args" "test_main_4_args"
#!/bin/bash
# ####test_demo_testf.bash
# #!/bin/bash
#
# # Usage:
# # ./test_demo_testf.bash
#
# # Use the -o to override the check on the test dir
# # ./test_demo_testf.bash -o
#
# source ./testf.bash
#
# test1(){
# echo "hello"
# }
#
# test2(){
# echo "world"
# }
#
# opt="$1"
# run_tests "test1" "test2"
echo_start(){
echo "$1 ============================="
}
get_bwd(){
# bwd: base working dir
# cwd: current working dir
cwd=$(realpath .)
: ${bwd=$cwd}
}
init_time_log(){
fpath_log="$bwd"/time.log
if [ -f $fpath_log ]
then
if [ "-o" != "$opt" ]
then
echo "Cannot create file:: File $fpath_log exists. Exit."
exit 1
fi
rm $fpath_log
fi
touch $fpath_log
}
log_datetime(){
datef=`date '+%Y%m%d.%H%M%S'`
echo "$1$datef" >> $fpath_log
}
init_dir(){
dpath_test="$bwd"/test9
if [ -e $dpath_test ]
then
if [ "-o" != "$opt" ]
then
echo "Cannot create dir:: Dir (or file) $dpath_test exists. Exit."
exit 1
fi
rm -r $dpath_test
fi
mkdir $dpath_test
cd $dpath_test
}
init_script_dir(){
# to be overwritten by the calling script
init_dir
}
init_script_extra(){
# to be overwritten by the calling script
:
}
finish_script_extra(){
# to be overwritten by the calling script
:
}
init_script(){
echo_start "Start script $0"
get_bwd
init_time_log
log_datetime "Start script $0 : "
init_script_dir
init_script_extra
}
finish_script(){
finish_script_extra
echo_start "Finish script $0"
log_datetime "Finish script $0 : "
}
init_test_extra(){
# to be overwritten by the calling script
:
}
finish_test_extra(){
# to be overwritten by the calling script
:
}
init_test(){
echo_start "Start $1 : "
log_datetime "Start $1 : "
init_test_extra
}
finish_test(){
finish_test_extra
log_datetime "Finish $1 : "
}
run_tests(){
init_script
for test in "$@"
do
init_test "$test"
$test
finish_test "$test"
done
finish_script
}
get_first_n_words_by_space(){
line="$1"
n="$2"
echo $(printf "$line" | cut -d " " -f 1-"$n")
}
assert_equal_str(){
expected="$1"
actual="$2"
if [ "$expected" == "$actual" ]
then
echo Pass
return 0
else
echo "Fail: $expected != $actual"
return 1
fi
}
assert_equal_num(){
expected="$1"
actual="$2"
if [[ "y" == "y$expected" && "y"=="y$actual" ]]
then
echo "Fail: arg1:expected is '', arg2:actual is ''"
return 1
fi
if [ "y" == "y$expected" ]
then
echo "Fail: arg1:expected is ''"
return 1
fi
if [ "y" == "y$actual" ]
then
echo "Fail: arg2:actual is ''"
return 1
fi
if [ $expected -eq $actual ]
then
echo Pass
else
echo "Fail: $expected != $actual"
return 0
fi
}
test1(){
# call the tested script
:
}