OS _ Bashrc - dwisianto/dwisianto GitHub Wiki

  • rc
  • rc_name
  • time_now
    • time_now=$(date +"%m_%d_%Y_%M_%S")
  • date date_str
    • emacs -nw $rc/os.sh
    • export date_str='+"%Y_%M_%S_%m_%d"'
    • echo ${date_str}
    • echo $(date ${date_str})
  • date_val & date_str
    • export date_val='%Y%m%d_%H%S'
    • alias date_str='date +${date_val}'
  • disk size of the home directory
    • du -sh .[^.]*

zshrc

  • f(){echo $funcstack[1];}
  • in bash
    • $funcname[0]
# Print the name of the function calling me
function func_name () {
  if [ -n $BASH_VERSION ](/dwisianto/dwisianto/wiki/--n-$BASH_VERSION-); then
    printf "%s\n" "${FUNCNAME[1]}"
  else  # zsh
    # Use offset:length as array indexing may start at 1 or 0
    printf "%s\n" "${funcstack[@]:1:1}"
  fi
}

bashrc

rc setup

source_scripts(){
  arg_list=( "$@" )
  for (( c=1; c<$#; c++)); do
    full=${arg_list[0]}/${arg_list[$c]}.sh
    echo -n "$full" && [ -f ${full} ](/dwisianto/dwisianto/wiki/--f-${full}-) || echo " [x] " && echo " [o] " && source ${full}
  done

}

export on_loc="~/"
export on_list=( on_pi_ana on_jv_neo )
source_scripts "${on_loc}" "${on_list[@]}"

Loop

export rc="~/d2/s2/m6/rc/rc23"
export rc_list=( d23 ac gt ac_path jp pg air )
for name in ${rc_list[@]} ; do
    full=${rc}/${name}
    if [ -f ${full} ]; then
        echo "[ok]: $full"
        source $full
    else
        echo "[err]: $full"
    fi
done

Associative Array


dev_ver_dir=../../dev/ver3
declare -A cfg=(
    [bundle_full]=../bundle/gb123456
    [dev_ver_dir]=${dev_ver_dir}
    [bare_dir]=${dev_ver_dir}/bare
    [master_dir]=${dev_ver_dir}/master
    [rc_id23]=rc_rc23
    [rc_id22]=rc_rc22
    [jn_j10]=jn_j10
    [ql_a]=ql_a
)
declare -A cmd=(
    [_1]='rm -rf ${cfg[dev_ver_dir]}'
    [_2]='git clone --mirror ${cfg[bundle_full]} ${cfg[bare_dir]}'
    [_3]='git clone ${cfg[bare_dir]} ${cfg[master_dir]}'
    [_4]='pushd $PWD'
    [_5]='cd ${cfg[master_dir]}'
    [_6]='git branch -a'
    [_7]='git worktree add ../${cfg[rc_id22]} ${cfg[rc_id22]}'
    [_8]='git worktree add ../${cfg[rc_id23]} ${cfg[rc_id23]}'
    [_9]='git worktree add ../${cfg[jn_j10]} ${cfg[jn_j10]}'
    [_10]='git worktree add ../${cfg[ql_a]} ${cfg[ql_a]}'
    [_11]='git worktree list'
    [_12]='popd'
)
cmd_len=${#cmd[@]}

act(){
    id=${FUNCNAME[0]}
    case "$1" in
	${id} )
	    for x in "${!cfg[@]}"; do printf "[%s]=%s\n" "$x" "${cfg[$x]}"; done | sort
	    for (( x=1; x<=$cmd_len; x++ )) ; do printf "[_%s]=%s\n" "$x" "${cmd[_$x]}"; done 
	    ;;
	${id}- )
	    for (( i=1; i<=$cmd_len; i++ )) ; do 
		printf "[_%s]=%s\n" "$i" "${cmd[_$i]}"
		eval ${cmd[_$i]}
	    done
	    ;;
	${id}-bak-b )
	    set -x
	    rm -rf ${cfg[dev_ver_dir]}
	    git clone --mirror ${cfg[bundle_full]} ${cfg[bare_dir]}
	    git clone ${cfg[bare_dir]} ${cfg[master_dir]}
	    cd ${cfg[master_dir]}; git worktree add ../${cfg[rc_id]} ${cfg[rc_id]}
	    git worktree list
	    set +x
	    ;;
	* )
	    echo "# USAGE: bash bundle.sh act"
	    ;;
    esac
}
act "$@"

Associative Array

https://kb.novaordis.com/index.php/Bash_Return_Multiple_Values_from_a_Function_using_an_Associative_Array

Associative Array1

declare -A CONFIG
load-config
echo ${CONFIG["SOMETHING"]}

Associative Array2

function load-config() {
  declare -A | grep -q "declare -A CONFIG" || fail "no CONFIG associative array declared"
  local key="SOMETHING"
  local value="BLAH"
  CONFIG[${key}]=${value}
}

Conda

#!/bin/bash

echo "This is my test bash script"
echo "Activating conda environment"
eval "$($(which conda) 'shell.bash' 'hook')"
conda activate eg_env
echo "Running python script"
python eg.py