shell-base/modules/utility/file.lib

95 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/bash
# export BASH_DEBUG=true
2022-03-28 10:02:17 -07:00
module_load debug
module_load iecho
isFile() {
if [[ -f $1 ]]
then
echo "true"
return 0
else
return 1
fi
}
2024-02-12 13:11:40 -08:00
# find in any file
fif() {
grep -rnswl $1 -e $2 | more
}
build_file () {
# local file
# echo build file $1 $2
# todo merge no cr
[[ $1 == "-f" ]] && file=true && shift 1
[[ -f "$2" ]] || { iecho "output file $2 does not exist"; return 1; }
if [[ -f "$1" ]]; then
# echo adding file $1 to $2
[[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2
# remove comment lines, remove blank last line
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
[[ ! $file ]] && echo -e "\n" >> $2
else iecho "no such file $1 to append to $2"
fi
}
# TODO need to test
function lines_2_str () {
[[ ! -f "$1" ]] && return 1
local str=''
# echo the lines: $lines >&2
# lines="$(cat "$1")"
while read line; do
[[ "$line" =~ ^[[:space:]]*# ]] && continue
# echo line: "${line}" >&2
str="$str ${line}"
# echo str: $str >&2
done < $1
# for line in $lines ; do
# str+='"'$line'" '
# done
echo $str
}
prepend_file () {
local tmp_fn
# ----------------------------------------------------------------------------------------------------------------------
# usage prepend_file <somefile> <anotherfile>
# ----------------------------------------------------------------------------------------------------------------------
# Prepend the contents of <somefile> [$1], to <anotherfile> [$2], leaving the result in <anotherfile> [$2].
# insert a newline at the end of <somefile> [$1] if necessary
# ----------------------------------------------------------------------------------------------------------------------
debug prepend $1 to $2
[[ -f $1 ]] || return 1
[[ -f $2 ]] || return 2
#todo remove comments with flag
tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX )
chmod 600 "$tmp_fn"
awk '{print}' $1 $2 > $tmp_fn
\mv "$tmp_fn" "$2"
# remove temporary file
rm -f "$tmp_fn"
return 0
2022-03-28 10:02:17 -07:00
}
# change-ext() {
# local depth
# local d
# d=${3:-0}
# let d+=1
# [ $d -lt 1 ] && depth="" || depth=" -maxdepth $d "
# find . $depth -name '*.'$1 -exec rename -n 's/\.'$1'/.'$2'/' \{} \;
# read -p "do REALLY want to change the extensions? " -n 1 -r
# echo
# [[ ! $REPLY =~ ^[Yy]$ ]] && return 1
# find . $depth -name '*.'$1 -exec rename 's/\.'$1'/.'$2'/' \{} \;
# }
# alias chext=change-ext
2024-02-12 13:11:40 -08:00