Skip to content

Instantly share code, notes, and snippets.

@photonxp
photonxp / lns
Last active November 7, 2018 05:26
ln -s wrapper, with more useful features
#!/bin/bash
spath=$(realpath "$0")
swd=${spath%/*}
source "$swd"/lns.bashlib
main "$@"
@photonxp
photonxp / test_testf.bash
Last active October 28, 2018 08:35
demo unit test for bash, just personal usage
#!/bin/bash
source ./testf.bashlib
test1(){
echo "hello world"
}
test_init_dir_unexist(){
[ -e ./test9 ] && rm -r ./test9
#!/bin/bash
import_note_by_fpath_data(){
import_note_tag_by_fpath_data(){
get_note_taglist_by_fpath_data(){
line_tag=$(grep -m 1 '^tag::' "$fpath_data")
taglist="$(echo ${line_tag##*:})"
}
fpath_data="$1"
@photonxp
photonxp / add_tagkey_in_dir.bash
Last active October 23, 2018 02:29
add tagkey tag:: to data file in dir
#!/bin/bash
get_line_tag(){
tagval="$1"
fpath="$2"
# unable to check $? for this gawk
line_tag=$(gawk -v ptn="$tagval([[:space:]]|$)" '$0~ptn && NR >= 3 && NR <= 5 {print NR":"$0;exit;}' "$fpath")
#echo "line_tag: $line_tag"
}
@photonxp
photonxp / check_is_tag_line.bash
Last active November 17, 2018 00:58
check if a line of tag values is the targeted tag line
#!/bin/bash
tagval="$1"
fpath="$2"
# unable to check $? for this gawk
line_tag=$(gawk -v ptn="$tagval([[:space:]]|$)" '$0~ptn && NR >= 3 && NR <= 5 {print NR":"$0;exit;}' "$fpath")
echo "line_tag: $line_tag"
fpath_taglist=./tag_list.txt
@photonxp
photonxp / bash2sh.bash
Last active October 7, 2018 11:04
convert sh file to bash or vice versa
#!/bin/bash
# Once the global warmth reaches a tipping point,
# the chain-reaction could be irreversible,
# so are we ready for the ticking bomb?
# Usage:
# ./bash2sh.bash ./test/b.bash c.bash [...]
# ./bash2sh.bash " " ./test/a ./test/{b,c}.bash ./test/dir/{.,d.bash}
#
@photonxp
photonxp / mv_files2dir.bash
Last active October 3, 2018 07:21
move files to destination directory in command
#!/bin/bash
# Usage:
# move files to dir_destination
# ./mv_files2dir.bash /path/to/file dir_destination
# ./mv_files2dir.bash dir_a ./{b,c} dir_destination
# test_mv_files2dir.bash:
# #!/bin/bash
#
@photonxp
photonxp / rename_linkets.bash
Last active October 11, 2018 02:31
Rename pairs of link files in the shell. Rename by multiple pairs of link file names.
#!/bin/bash
# The bubbles in the asset markets
# are equivalent to various taxes,
# payed by each of us in the long term,
# whether a winner or loser in the game.
# Features:
# Change a pair of link files
# from
@photonxp
photonxp / rename_files.bash
Last active October 6, 2018 03:57
Make the job of renaming files in the shell a little bit easier
#!/bin/bash
# Features:
# Renaming file or files by batch with the shell script.
# Rename by multiple pairs of file names.
# Use the `mv` command along with `grep` etc. underlyingly.
# A rather simplified alternative to the `rename` shipped by some linux systems.
# Usage:
# 1. Rename a file by one pair of file names:
@photonxp
photonxp / sed_tagkey_in_dir.bash
Last active October 7, 2018 10:58
tag line editing, tag key editing
#!/bin/bash
# Tag line editing
# Change the line
# from
# vocabulary grammar
# to
# tag:: vocabulary grammar
# Change by a given tag value such as "vocabulary" or "grammar"
# Omit the tag line if they already have the key word of "tag"